blob: 688376ca28e69f92c6336cbb91f0644b132e11d7 [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:
Hamza Sood81fe14e2017-11-21 09:42:42 +000084 if (getLangOpts().Modules || getLangOpts().DebuggerSupport) {
85 SingleDecl = ParseModuleImport(AtLoc);
86 break;
87 }
Manman Rendfcf1cb2017-01-20 20:03:00 +000088 Diag(AtLoc, diag::err_atimport);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000089 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000090 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000091 default:
92 Diag(AtLoc, diag::err_unexpected_at);
93 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000094 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000095 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000096 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000097 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098}
99
Richard Smith3df3f1d2015-11-03 01:19:56 +0000100/// Class to handle popping type parameters when leaving the scope.
101class Parser::ObjCTypeParamListScope {
102 Sema &Actions;
103 Scope *S;
104 ObjCTypeParamList *Params;
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000105
Richard Smith3df3f1d2015-11-03 01:19:56 +0000106public:
107 ObjCTypeParamListScope(Sema &Actions, Scope *S)
108 : Actions(Actions), S(S), Params(nullptr) {}
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000109
Richard Smith3df3f1d2015-11-03 01:19:56 +0000110 ~ObjCTypeParamListScope() {
111 leave();
112 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000113
Richard Smith3df3f1d2015-11-03 01:19:56 +0000114 void enter(ObjCTypeParamList *P) {
115 assert(!Params);
116 Params = P;
117 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000118
Richard Smith3df3f1d2015-11-03 01:19:56 +0000119 void leave() {
120 if (Params)
121 Actions.popObjCTypeParamList(S, Params);
122 Params = nullptr;
123 }
124};
125
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000126///
Mike Stump11289f42009-09-09 15:08:12 +0000127/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000128/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
129///
130/// objc-class-forward-decl:
131/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000132///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000133Parser::DeclGroupPtrTy
134Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000135 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000136 SmallVector<IdentifierInfo *, 8> ClassNames;
137 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000138 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000139
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000140 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000141 MaybeSkipAttributes(tok::objc_class);
Alex Lorenzf1278212017-04-11 15:01:53 +0000142 if (expectIdentifier()) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000143 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000144 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000145 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000146 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000147 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000148 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Douglas Gregor85f3f952015-07-07 03:57:15 +0000150 // Parse the optional objc-type-parameter-list.
151 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000152 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000153 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000154 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000155 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000156 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000159 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000160 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000161 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Ted Kremeneka26da852009-11-17 23:12:20 +0000163 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
164 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000165 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000166 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000167}
168
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000169void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
170{
171 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
172 if (ock == Sema::OCK_None)
173 return;
174
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000175 Decl *Decl = Actions.getObjCDeclContext();
176 if (CurParsedObjCImpl) {
177 CurParsedObjCImpl->finish(AtLoc);
178 } else {
179 Actions.ActOnAtEnd(getCurScope(), AtLoc);
180 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000181 Diag(AtLoc, diag::err_objc_missing_end)
182 << FixItHint::CreateInsertion(AtLoc, "@end\n");
183 if (Decl)
184 Diag(Decl->getLocStart(), diag::note_objc_container_start)
185 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000186}
187
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000188///
189/// objc-interface:
190/// objc-class-interface-attributes[opt] objc-class-interface
191/// objc-category-interface
192///
193/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000194/// '@' 'interface' identifier objc-type-parameter-list[opt]
195/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000196/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000197/// objc-interface-decl-list
198/// @end
199///
200/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000201/// '@' 'interface' identifier objc-type-parameter-list[opt]
202/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203/// objc-interface-decl-list
204/// @end
205///
206/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000207/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000208///
209/// objc-class-interface-attributes:
210/// __attribute__((visibility("default")))
211/// __attribute__((visibility("hidden")))
212/// __attribute__((deprecated))
213/// __attribute__((unavailable))
214/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000215/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000216///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000217Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000218 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000219 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000220 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000221 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000222 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000223
Douglas Gregor49c22a72009-11-18 16:26:39 +0000224 // Code completion after '@interface'.
225 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000226 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000227 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000228 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000229 }
230
Nico Weber69a79142013-04-04 00:15:10 +0000231 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000232
Alex Lorenzf1278212017-04-11 15:01:53 +0000233 if (expectIdentifier())
234 return nullptr; // missing class or category name.
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000235
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000237 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000238 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000239
240 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
241 // case, LAngleLoc will be valid and ProtocolIdents will capture the
242 // protocol references (that have not yet been resolved).
243 SourceLocation LAngleLoc, EndProtoLoc;
244 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
245 ObjCTypeParamList *typeParameterList = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000246 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
247 if (Tok.is(tok::less))
248 typeParameterList = parseObjCTypeParamListOrProtocolRefs(
249 typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000250
251 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000252 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000253
254 BalancedDelimiterTracker T(*this, tok::l_paren);
255 T.consumeOpen();
256
257 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000258 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000259 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000260 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000261 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000262 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000263 }
264
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000265 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000266 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000267 categoryId = Tok.getIdentifierInfo();
268 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000269 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000270 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000271 Diag(Tok, diag::err_expected)
272 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000273 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000274 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000275
276 T.consumeClose();
277 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000278 return nullptr;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000279
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000280 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000281 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000282 SmallVector<Decl *, 8> ProtocolRefs;
283 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000284 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000285 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000286 LAngleLoc, EndProtoLoc,
287 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000288 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000289
Alex Lorenzf9371392017-03-23 11:44:25 +0000290 Decl *CategoryType = Actions.ActOnStartCategoryInterface(
291 AtLoc, nameId, nameLoc, typeParameterList, categoryId, categoryLoc,
292 ProtocolRefs.data(), ProtocolRefs.size(), ProtocolLocs.data(),
293 EndProtoLoc, attrs.getList());
294
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000295 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000296 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000297
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000298 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000299
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000300 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000301 }
302 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000303 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000304 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000305 SourceLocation typeArgsLAngleLoc;
306 SmallVector<ParsedType, 4> typeArgs;
307 SourceLocation typeArgsRAngleLoc;
308 SmallVector<Decl *, 4> protocols;
309 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000310 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000311 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000312
313 // Code completion of superclass names.
314 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000315 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000316 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000317 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000318 }
319
Alex Lorenzf1278212017-04-11 15:01:53 +0000320 if (expectIdentifier())
321 return nullptr; // missing super class name.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000322 superClassId = Tok.getIdentifierInfo();
323 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000324
325 // Type arguments for the superclass or protocol conformances.
326 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000327 parseObjCTypeArgsOrProtocolQualifiers(
328 nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
329 protocols, protocolLocs, EndProtoLoc,
330 /*consumeLastToken=*/true,
331 /*warnOnIncompleteProtocols=*/true);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000332 if (Tok.is(tok::eof))
333 return nullptr;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000334 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000335 }
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000336
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000337 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000338 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000339 if (!ProtocolIdents.empty()) {
340 // We already parsed the protocols named when we thought we had a
341 // type parameter list. Translate them into actual protocol references.
342 for (const auto &pair : ProtocolIdents) {
343 protocolLocs.push_back(pair.second);
344 }
345 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
346 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000347 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000348 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000349 } else if (protocols.empty() && Tok.is(tok::less) &&
350 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
351 LAngleLoc, EndProtoLoc,
352 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000353 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000356 if (Tok.isNot(tok::less))
Argyrios Kyrtzidisf95a0002016-11-09 02:47:07 +0000357 Actions.ActOnTypedefedProtocols(protocols, protocolLocs,
358 superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000359
John McCall48871652010-08-21 09:40:31 +0000360 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000361 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
362 typeParameterList, superClassId,
363 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000364 typeArgs,
365 SourceRange(typeArgsLAngleLoc,
366 typeArgsRAngleLoc),
367 protocols.data(), protocols.size(),
368 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000369 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000370
Chris Lattner0ef13522007-10-09 17:51:17 +0000371 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000372 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000373
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000374 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000375
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000376 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000377}
378
Douglas Gregor813a0662015-06-19 18:14:38 +0000379/// Add an attribute for a context-sensitive type nullability to the given
380/// declarator.
381static void addContextSensitiveTypeNullability(Parser &P,
382 Declarator &D,
383 NullabilityKind nullability,
384 SourceLocation nullabilityLoc,
385 bool &addedToDeclSpec) {
386 // Create the attribute.
387 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000388 return D.getAttributePool().create(
389 P.getNullabilityKeyword(nullability),
390 SourceRange(nullabilityLoc),
391 nullptr, SourceLocation(),
392 nullptr, 0,
393 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000394 };
395
396 if (D.getNumTypeObjects() > 0) {
397 // Add the attribute to the declarator chunk nearest the declarator.
398 auto nullabilityAttr = getNullabilityAttr();
399 DeclaratorChunk &chunk = D.getTypeObject(0);
400 nullabilityAttr->setNext(chunk.getAttrListRef());
401 chunk.getAttrListRef() = nullabilityAttr;
402 } else if (!addedToDeclSpec) {
403 // Otherwise, just put it on the declaration specifiers (if one
404 // isn't there already).
405 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
406 addedToDeclSpec = true;
407 }
408}
409
Douglas Gregor85f3f952015-07-07 03:57:15 +0000410/// Parse an Objective-C type parameter list, if present, or capture
411/// the locations of the protocol identifiers for a list of protocol
412/// references.
413///
414/// objc-type-parameter-list:
415/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
416///
417/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000418/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000419///
420/// objc-type-parameter-bound:
421/// ':' type-name
422///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000423/// objc-type-parameter-variance:
424/// '__covariant'
425/// '__contravariant'
426///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000427/// \param lAngleLoc The location of the starting '<'.
428///
429/// \param protocolIdents Will capture the list of identifiers, if the
430/// angle brackets contain a list of protocol references rather than a
431/// type parameter list.
432///
433/// \param rAngleLoc The location of the ending '>'.
434ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000435 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
436 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
437 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000438 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
439
440 // Within the type parameter list, don't treat '>' as an operator.
441 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
442
443 // Local function to "flush" the protocol identifiers, turning them into
444 // type parameters.
445 SmallVector<Decl *, 4> typeParams;
446 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000447 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000448 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000449 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000450 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
451 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000452 if (typeParam.isUsable())
453 typeParams.push_back(typeParam.get());
454 }
455
456 protocolIdents.clear();
457 mayBeProtocolList = false;
458 };
459
460 bool invalid = false;
461 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000462
Douglas Gregor85f3f952015-07-07 03:57:15 +0000463 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000464 // Parse the variance, if any.
465 SourceLocation varianceLoc;
466 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
467 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
468 variance = Tok.is(tok::kw___covariant)
469 ? ObjCTypeParamVariance::Covariant
470 : ObjCTypeParamVariance::Contravariant;
471 varianceLoc = ConsumeToken();
472
473 // Once we've seen a variance specific , we know this is not a
474 // list of protocol references.
475 if (mayBeProtocolList) {
476 // Up until now, we have been queuing up parameters because they
477 // might be protocol references. Turn them into parameters now.
478 makeProtocolIdentsIntoTypeParameters();
479 }
480 }
481
Douglas Gregor85f3f952015-07-07 03:57:15 +0000482 // Parse the identifier.
483 if (!Tok.is(tok::identifier)) {
484 // Code completion.
485 if (Tok.is(tok::code_completion)) {
486 // FIXME: If these aren't protocol references, we'll need different
487 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000488 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000489 cutOffParsing();
490
491 // FIXME: Better recovery here?.
492 return nullptr;
493 }
494
495 Diag(Tok, diag::err_objc_expected_type_parameter);
496 invalid = true;
497 break;
498 }
499
500 IdentifierInfo *paramName = Tok.getIdentifierInfo();
501 SourceLocation paramLoc = ConsumeToken();
502
503 // If there is a bound, parse it.
504 SourceLocation colonLoc;
505 TypeResult boundType;
506 if (TryConsumeToken(tok::colon, colonLoc)) {
507 // Once we've seen a bound, we know this is not a list of protocol
508 // references.
509 if (mayBeProtocolList) {
510 // Up until now, we have been queuing up parameters because they
511 // might be protocol references. Turn them into parameters now.
512 makeProtocolIdentsIntoTypeParameters();
513 }
514
515 // type-name
516 boundType = ParseTypeName();
517 if (boundType.isInvalid())
518 invalid = true;
519 } else if (mayBeProtocolList) {
520 // If this could still be a protocol list, just capture the identifier.
521 // We don't want to turn it into a parameter.
522 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
523 continue;
524 }
525
526 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000527 DeclResult typeParam = Actions.actOnObjCTypeParam(
528 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
529 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000530 if (typeParam.isUsable())
531 typeParams.push_back(typeParam.get());
532 } while (TryConsumeToken(tok::comma));
533
534 // Parse the '>'.
535 if (invalid) {
536 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
537 if (Tok.is(tok::greater))
538 ConsumeToken();
539 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
540 /*ConsumeLastToken=*/true,
541 /*ObjCGenericList=*/true)) {
542 Diag(lAngleLoc, diag::note_matching) << "'<'";
543 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
544 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
545 tok::comma, tok::semi },
546 StopBeforeMatch);
547 if (Tok.is(tok::greater))
548 ConsumeToken();
549 }
550
551 if (mayBeProtocolList) {
552 // A type parameter list must be followed by either a ':' (indicating the
553 // presence of a superclass) or a '(' (indicating that this is a category
554 // or extension). This disambiguates between an objc-type-parameter-list
555 // and a objc-protocol-refs.
556 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
557 // Returning null indicates that we don't have a type parameter list.
558 // The results the caller needs to handle the protocol references are
559 // captured in the reference parameters already.
560 return nullptr;
561 }
562
563 // We have a type parameter list that looks like a list of protocol
564 // references. Turn that parameter list into type parameters.
565 makeProtocolIdentsIntoTypeParameters();
566 }
567
Richard Smith3df3f1d2015-11-03 01:19:56 +0000568 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000569 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
570 getCurScope(),
571 lAngleLoc,
572 typeParams,
573 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000574 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000575
576 // Clear out the angle locations; they're used by the caller to indicate
577 // whether there are any protocol references.
578 lAngleLoc = SourceLocation();
579 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000580 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000581}
582
583/// Parse an objc-type-parameter-list.
584ObjCTypeParamList *Parser::parseObjCTypeParamList() {
585 SourceLocation lAngleLoc;
586 SmallVector<IdentifierLocPair, 1> protocolIdents;
587 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000588
589 ObjCTypeParamListScope Scope(Actions, getCurScope());
590 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
591 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000592 /*mayBeProtocolList=*/false);
593}
594
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000595/// objc-interface-decl-list:
596/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000597/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000598/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000599/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000600/// objc-interface-decl-list declaration
601/// objc-interface-decl-list ';'
602///
Steve Naroff99264b42007-08-22 16:35:03 +0000603/// objc-method-requirement: [OBJC2]
604/// @required
605/// @optional
606///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000607void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
608 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000609 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000610 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000611 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Ted Kremenekc7c64312010-01-07 01:20:12 +0000613 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000614
Steve Naroff99264b42007-08-22 16:35:03 +0000615 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000616 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000617 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000618 if (Decl *methodPrototype =
619 ParseObjCMethodPrototype(MethodImplKind, false))
620 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000621 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
622 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000623 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
624 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000625 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000626 if (Tok.is(tok::semi))
627 ConsumeToken();
628 }
Steve Naroff99264b42007-08-22 16:35:03 +0000629 continue;
630 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000631 if (Tok.is(tok::l_paren)) {
632 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000633 ParseObjCMethodDecl(Tok.getLocation(),
634 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000635 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000636 continue;
637 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000638 // Ignore excess semicolons.
639 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000640 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000641 continue;
642 }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Chris Lattnerda9fb152008-10-20 06:10:06 +0000644 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000645 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000646 break;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Douglas Gregorf1934162010-01-13 21:24:21 +0000648 // Code completion within an Objective-C interface.
649 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000650 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000651 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000652 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000653 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000654 }
655
Chris Lattner038a3e32008-10-20 05:46:22 +0000656 // If we don't have an @ directive, parse it as a function definition.
657 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000658 // The code below does not consume '}'s because it is afraid of eating the
659 // end of a namespace. Because of the way this code is structured, an
660 // erroneous r_brace would cause an infinite loop if not handled here.
661 if (Tok.is(tok::r_brace))
662 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000663 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000664 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000665 continue;
666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattner038a3e32008-10-20 05:46:22 +0000668 // Otherwise, we have an @ directive, eat the @.
669 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000670 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000671 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000672 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000673 }
674
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000675 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000676
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000677 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000678 AtEnd.setBegin(AtLoc);
679 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000680 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000681 } else if (DirectiveKind == tok::objc_not_keyword) {
682 Diag(Tok, diag::err_objc_unknown_at);
683 SkipUntil(tok::semi);
684 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000685 }
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattnerda9fb152008-10-20 06:10:06 +0000687 // Eat the identifier.
688 ConsumeToken();
689
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000690 switch (DirectiveKind) {
691 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000692 // FIXME: If someone forgets an @end on a protocol, this loop will
693 // continue to eat up tons of stuff and spew lots of nonsense errors. It
694 // would probably be better to bail out if we saw an @class or @interface
695 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000696 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000697 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000698 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000699 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000700
701 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000702 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000703 Diag(AtLoc, diag::err_objc_missing_end)
704 << FixItHint::CreateInsertion(AtLoc, "@end\n");
705 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
706 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000707 ConsumeToken();
708 break;
709
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000710 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000711 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000712 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000713 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000714 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000715 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000716 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000717 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000718 break;
Mike Stump11289f42009-09-09 15:08:12 +0000719
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000720 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000721 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000722 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000723
Chris Lattner038a3e32008-10-20 05:46:22 +0000724 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000725 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000726 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000727 if (Tok.is(tok::l_paren)) {
728 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000729 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000730 }
Mike Stump11289f42009-09-09 15:08:12 +0000731
Douglas Gregor813a0662015-06-19 18:14:38 +0000732 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000733 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
734 if (FD.D.getIdentifier() == nullptr) {
735 Diag(AtLoc, diag::err_objc_property_requires_field_name)
736 << FD.D.getSourceRange();
737 return;
738 }
739 if (FD.BitfieldSize) {
740 Diag(AtLoc, diag::err_objc_property_bitfield)
741 << FD.D.getSourceRange();
742 return;
743 }
744
Douglas Gregor813a0662015-06-19 18:14:38 +0000745 // Map a nullability property attribute to a context-sensitive keyword
746 // attribute.
747 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
748 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
749 OCDS.getNullabilityLoc(),
750 addedToDeclSpec);
751
Benjamin Kramera39beb92014-09-03 11:06:10 +0000752 // Install the property declarator into interfaceDecl.
753 IdentifierInfo *SelName =
754 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
755
756 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
757 IdentifierInfo *SetterName = OCDS.getSetterName();
758 Selector SetterSel;
759 if (SetterName)
760 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
761 else
762 SetterSel = SelectorTable::constructSetterSelector(
763 PP.getIdentifierTable(), PP.getSelectorTable(),
764 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000765 Decl *Property = Actions.ActOnProperty(
766 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000767 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000768
769 FD.complete(Property);
770 };
John McCallcfefb6d2009-11-03 02:38:08 +0000771
Chris Lattner038a3e32008-10-20 05:46:22 +0000772 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000773 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000774 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000775
John McCall405988b2011-03-26 01:53:26 +0000776 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000777 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000778 }
Steve Naroff99264b42007-08-22 16:35:03 +0000779 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000780
781 // We break out of the big loop in two cases: when we see @end or when we see
782 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000783 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000784 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000785 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000786 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000787 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000788 } else {
789 Diag(Tok, diag::err_objc_missing_end)
790 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
791 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
792 << (int) Actions.getObjCContainerKind();
793 AtEnd.setBegin(Tok.getLocation());
794 AtEnd.setEnd(Tok.getLocation());
795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000797 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000798 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000799 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000800}
801
Douglas Gregor813a0662015-06-19 18:14:38 +0000802/// Diagnose redundant or conflicting nullability information.
803static void diagnoseRedundantPropertyNullability(Parser &P,
804 ObjCDeclSpec &DS,
805 NullabilityKind nullability,
806 SourceLocation nullabilityLoc){
807 if (DS.getNullability() == nullability) {
808 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000809 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000810 << SourceRange(DS.getNullabilityLoc());
811 return;
812 }
813
814 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000815 << DiagNullabilityKind(nullability, true)
816 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000817 << SourceRange(DS.getNullabilityLoc());
818}
819
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000820/// Parse property attribute declarations.
821///
822/// property-attr-decl: '(' property-attrlist ')'
823/// property-attrlist:
824/// property-attribute
825/// property-attrlist ',' property-attribute
826/// property-attribute:
827/// getter '=' identifier
828/// setter '=' identifier ':'
829/// readonly
830/// readwrite
831/// assign
832/// retain
833/// copy
834/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000835/// atomic
836/// strong
837/// weak
838/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000839/// nonnull
840/// nullable
841/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000842/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000843/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000844///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000845void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000846 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000847 BalancedDelimiterTracker T(*this, tok::l_paren);
848 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000850 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000851 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000852 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000853 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000854 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000855 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner76619232008-10-20 07:22:18 +0000857 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000858 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000859 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000860 return;
861 }
Mike Stump11289f42009-09-09 15:08:12 +0000862
Chris Lattner1db33542008-10-20 07:37:22 +0000863 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner68e48682008-11-20 04:42:34 +0000865 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000866 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000867 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000868 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000869 else if (II->isStr("unsafe_unretained"))
870 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000871 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000872 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000873 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000874 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000875 else if (II->isStr("strong"))
876 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000877 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000878 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000879 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000880 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000881 else if (II->isStr("atomic"))
882 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000883 else if (II->isStr("weak"))
884 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000885 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000886 bool IsSetter = II->getNameStart()[0] == 's';
887
Chris Lattner825bca12008-10-20 07:39:53 +0000888 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000889 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000890 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000891
Alp Toker383d2c42014-01-01 03:08:43 +0000892 if (ExpectAndConsume(tok::equal, DiagID)) {
893 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000894 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Douglas Gregorc8537c52009-11-19 07:41:15 +0000897 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000898 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000899 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000900 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000901 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000902 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000903 }
904
Anders Carlssonfe15a782010-10-02 17:45:21 +0000905 SourceLocation SelLoc;
906 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
907
908 if (!SelIdent) {
909 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
910 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000911 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000912 return;
913 }
Mike Stump11289f42009-09-09 15:08:12 +0000914
Anders Carlssonfe15a782010-10-02 17:45:21 +0000915 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000916 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000917 DS.setSetterName(SelIdent, SelLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000918
Alp Toker383d2c42014-01-01 03:08:43 +0000919 if (ExpectAndConsume(tok::colon,
920 diag::err_expected_colon_after_setter_name)) {
921 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000922 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000923 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000924 } else {
925 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000926 DS.setGetterName(SelIdent, SelLoc);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000927 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000928 } else if (II->isStr("nonnull")) {
929 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
930 diagnoseRedundantPropertyNullability(*this, DS,
931 NullabilityKind::NonNull,
932 Tok.getLocation());
933 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
934 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
935 } else if (II->isStr("nullable")) {
936 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
937 diagnoseRedundantPropertyNullability(*this, DS,
938 NullabilityKind::Nullable,
939 Tok.getLocation());
940 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
941 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
942 } else if (II->isStr("null_unspecified")) {
943 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
944 diagnoseRedundantPropertyNullability(*this, DS,
945 NullabilityKind::Unspecified,
946 Tok.getLocation());
947 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
948 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000949 } else if (II->isStr("null_resettable")) {
950 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
951 diagnoseRedundantPropertyNullability(*this, DS,
952 NullabilityKind::Unspecified,
953 Tok.getLocation());
954 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
955 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
956
957 // Also set the null_resettable bit.
958 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000959 } else if (II->isStr("class")) {
960 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000961 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000962 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000963 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000964 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000965 }
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattner1db33542008-10-20 07:37:22 +0000967 if (Tok.isNot(tok::comma))
968 break;
Mike Stump11289f42009-09-09 15:08:12 +0000969
Chris Lattner1db33542008-10-20 07:37:22 +0000970 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000971 }
Mike Stump11289f42009-09-09 15:08:12 +0000972
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000973 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000974}
975
Steve Naroff09bf8152007-09-06 21:24:23 +0000976/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000977/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000978/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000979///
980/// objc-instance-method: '-'
981/// objc-class-method: '+'
982///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000983/// objc-method-attributes: [OBJC2]
984/// __attribute__((deprecated))
985///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000986Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000987 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000988 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000989
Mike Stump11289f42009-09-09 15:08:12 +0000990 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000991 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000992 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000993 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000994 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000995 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000996 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000997}
998
999/// objc-selector:
1000/// identifier
1001/// one of
1002/// enum struct union if else while do for switch case default
1003/// break continue return goto asm sizeof typeof __alignof
1004/// unsigned long const short volatile signed restrict _Complex
1005/// in out inout bycopy byref oneway int char float double void _Bool
1006///
Chris Lattner4f472a32009-04-11 18:13:45 +00001007IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001008
Chris Lattner5700fab2007-10-07 02:00:24 +00001009 switch (Tok.getKind()) {
1010 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001011 return nullptr;
Alex Lorenz9b9188d2017-07-13 10:50:21 +00001012 case tok::colon:
1013 // Empty selector piece uses the location of the ':'.
1014 SelectorLoc = Tok.getLocation();
1015 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001016 case tok::ampamp:
1017 case tok::ampequal:
1018 case tok::amp:
1019 case tok::pipe:
1020 case tok::tilde:
1021 case tok::exclaim:
1022 case tok::exclaimequal:
1023 case tok::pipepipe:
1024 case tok::pipeequal:
1025 case tok::caret:
1026 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001027 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001028 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001029 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001030 Tok.setKind(tok::identifier);
1031 SelectorLoc = ConsumeToken();
1032 return II;
1033 }
Craig Topper161e4db2014-05-21 06:02:52 +00001034 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001035 }
1036
Chris Lattner5700fab2007-10-07 02:00:24 +00001037 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001038 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001039 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001040 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001041 case tok::kw_break:
1042 case tok::kw_case:
1043 case tok::kw_catch:
1044 case tok::kw_char:
1045 case tok::kw_class:
1046 case tok::kw_const:
1047 case tok::kw_const_cast:
1048 case tok::kw_continue:
1049 case tok::kw_default:
1050 case tok::kw_delete:
1051 case tok::kw_do:
1052 case tok::kw_double:
1053 case tok::kw_dynamic_cast:
1054 case tok::kw_else:
1055 case tok::kw_enum:
1056 case tok::kw_explicit:
1057 case tok::kw_export:
1058 case tok::kw_extern:
1059 case tok::kw_false:
1060 case tok::kw_float:
1061 case tok::kw_for:
1062 case tok::kw_friend:
1063 case tok::kw_goto:
1064 case tok::kw_if:
1065 case tok::kw_inline:
1066 case tok::kw_int:
1067 case tok::kw_long:
1068 case tok::kw_mutable:
1069 case tok::kw_namespace:
1070 case tok::kw_new:
1071 case tok::kw_operator:
1072 case tok::kw_private:
1073 case tok::kw_protected:
1074 case tok::kw_public:
1075 case tok::kw_register:
1076 case tok::kw_reinterpret_cast:
1077 case tok::kw_restrict:
1078 case tok::kw_return:
1079 case tok::kw_short:
1080 case tok::kw_signed:
1081 case tok::kw_sizeof:
1082 case tok::kw_static:
1083 case tok::kw_static_cast:
1084 case tok::kw_struct:
1085 case tok::kw_switch:
1086 case tok::kw_template:
1087 case tok::kw_this:
1088 case tok::kw_throw:
1089 case tok::kw_true:
1090 case tok::kw_try:
1091 case tok::kw_typedef:
1092 case tok::kw_typeid:
1093 case tok::kw_typename:
1094 case tok::kw_typeof:
1095 case tok::kw_union:
1096 case tok::kw_unsigned:
1097 case tok::kw_using:
1098 case tok::kw_virtual:
1099 case tok::kw_void:
1100 case tok::kw_volatile:
1101 case tok::kw_wchar_t:
1102 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001103 case tok::kw__Bool:
1104 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001105 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001106 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001107 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001108 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001109 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001110 }
Steve Naroff99264b42007-08-22 16:35:03 +00001111}
1112
Fariborz Jahanian83615522008-01-02 22:54:34 +00001113/// objc-for-collection-in: 'in'
1114///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001115bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001116 // FIXME: May have to do additional look-ahead to only allow for
1117 // valid tokens following an 'in'; such as an identifier, unary operators,
1118 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001119 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001120 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001121}
1122
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001123/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001124/// qualifier list and builds their bitmask representation in the input
1125/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001126///
1127/// objc-type-qualifiers:
1128/// objc-type-qualifier
1129/// objc-type-qualifiers objc-type-qualifier
1130///
Douglas Gregor813a0662015-06-19 18:14:38 +00001131/// objc-type-qualifier:
1132/// 'in'
1133/// 'out'
1134/// 'inout'
1135/// 'oneway'
1136/// 'bycopy'
1137/// 'byref'
1138/// 'nonnull'
1139/// 'nullable'
1140/// 'null_unspecified'
1141///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001142void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001143 DeclaratorContext Context) {
1144 assert(Context == DeclaratorContext::ObjCParameterContext ||
1145 Context == DeclaratorContext::ObjCResultContext);
John McCalla55902b2011-10-01 09:56:14 +00001146
Chris Lattner61511e12007-12-12 06:56:32 +00001147 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001148 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001149 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001150 Context == DeclaratorContext::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001151 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001152 }
1153
Chris Lattner5e530bc2007-12-27 19:57:00 +00001154 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001155 return;
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattner61511e12007-12-12 06:56:32 +00001157 const IdentifierInfo *II = Tok.getIdentifierInfo();
1158 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001159 if (II != ObjCTypeQuals[i] ||
1160 NextToken().is(tok::less) ||
1161 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001162 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001163
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001164 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001165 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001166 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001167 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001168 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1169 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1170 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1171 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1172 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1173 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001174
1175 case objc_nonnull:
1176 Qual = ObjCDeclSpec::DQ_CSNullability;
1177 Nullability = NullabilityKind::NonNull;
1178 break;
1179
1180 case objc_nullable:
1181 Qual = ObjCDeclSpec::DQ_CSNullability;
1182 Nullability = NullabilityKind::Nullable;
1183 break;
1184
1185 case objc_null_unspecified:
1186 Qual = ObjCDeclSpec::DQ_CSNullability;
1187 Nullability = NullabilityKind::Unspecified;
1188 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001189 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001190
1191 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001192 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001193 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1194 DS.setNullability(Tok.getLocation(), Nullability);
1195
Chris Lattner61511e12007-12-12 06:56:32 +00001196 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001197 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001198 break;
1199 }
Mike Stump11289f42009-09-09 15:08:12 +00001200
Chris Lattner61511e12007-12-12 06:56:32 +00001201 // If this wasn't a recognized qualifier, bail out.
1202 if (II) return;
1203 }
1204}
1205
John McCalla55902b2011-10-01 09:56:14 +00001206/// Take all the decl attributes out of the given list and add
1207/// them to the given attribute set.
1208static void takeDeclAttributes(ParsedAttributes &attrs,
1209 AttributeList *list) {
1210 while (list) {
1211 AttributeList *cur = list;
1212 list = cur->getNext();
1213
1214 if (!cur->isUsedAsTypeAttr()) {
1215 // Clear out the next pointer. We're really completely
1216 // destroying the internal invariants of the declarator here,
1217 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001218 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001219 attrs.add(cur);
1220 }
1221 }
1222}
1223
1224/// takeDeclAttributes - Take all the decl attributes from the given
1225/// declarator and add them to the given list.
1226static void takeDeclAttributes(ParsedAttributes &attrs,
1227 Declarator &D) {
1228 // First, take ownership of all attributes.
1229 attrs.getPool().takeAllFrom(D.getAttributePool());
1230 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1231
1232 // Now actually move the attributes over.
1233 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1234 takeDeclAttributes(attrs, D.getAttributes());
1235 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1236 takeDeclAttributes(attrs,
1237 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1238}
1239
Chris Lattner61511e12007-12-12 06:56:32 +00001240/// objc-type-name:
1241/// '(' objc-type-qualifiers[opt] type-name ')'
1242/// '(' objc-type-qualifiers[opt] ')'
1243///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001244ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001245 DeclaratorContext context,
John McCalla55902b2011-10-01 09:56:14 +00001246 ParsedAttributes *paramAttrs) {
Faisal Vali421b2d12017-12-29 05:41:00 +00001247 assert(context == DeclaratorContext::ObjCParameterContext ||
1248 context == DeclaratorContext::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001249 assert((paramAttrs != nullptr) ==
Faisal Vali421b2d12017-12-29 05:41:00 +00001250 (context == DeclaratorContext::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001251
Chris Lattner0ef13522007-10-09 17:51:17 +00001252 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001253
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001254 BalancedDelimiterTracker T(*this, tok::l_paren);
1255 T.consumeOpen();
1256
Chris Lattner2ebb1782008-08-23 01:48:03 +00001257 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001258 ObjCDeclContextSwitch ObjCDC(*this);
1259
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001260 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001261 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001262
John McCallba7bf592010-08-24 05:47:05 +00001263 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001264 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001265 // Parse an abstract declarator.
1266 DeclSpec declSpec(AttrFactory);
1267 declSpec.setObjCQualifiers(&DS);
Faisal Vali7db85c52017-12-31 00:06:40 +00001268 DeclSpecContext dsContext = DeclSpecContext::DSC_normal;
Faisal Vali421b2d12017-12-29 05:41:00 +00001269 if (context == DeclaratorContext::ObjCResultContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00001270 dsContext = DeclSpecContext::DSC_objc_method_result;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001271 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001272 Declarator declarator(declSpec, context);
1273 ParseDeclarator(declarator);
1274
1275 // If that's not invalid, extract a type.
1276 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001277 // Map a nullability specifier to a context-sensitive keyword attribute.
1278 bool addedToDeclSpec = false;
1279 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1280 addContextSensitiveTypeNullability(*this, declarator,
1281 DS.getNullability(),
1282 DS.getNullabilityLoc(),
1283 addedToDeclSpec);
1284
John McCalla55902b2011-10-01 09:56:14 +00001285 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1286 if (!type.isInvalid())
1287 Ty = type.get();
1288
1289 // If we're parsing a parameter, steal all the decl attributes
1290 // and add them to the decl spec.
Faisal Vali421b2d12017-12-29 05:41:00 +00001291 if (context == DeclaratorContext::ObjCParameterContext)
John McCalla55902b2011-10-01 09:56:14 +00001292 takeDeclAttributes(*paramAttrs, declarator);
1293 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001294 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001295
Steve Naroff90255b42008-10-21 14:15:04 +00001296 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001297 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001298 else if (Tok.getLocation() == TypeStartLoc) {
1299 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001300 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001301 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001302 } else {
1303 // Otherwise, we found *something*, but didn't get a ')' in the right
1304 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001305 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001306 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001307 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001308}
1309
1310/// objc-method-decl:
1311/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001312/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001313/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001314/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001315///
1316/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001317/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001318/// objc-keyword-selector objc-keyword-decl
1319///
1320/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001321/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1322/// objc-selector ':' objc-keyword-attributes[opt] identifier
1323/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1324/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001325///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001326/// objc-parmlist:
1327/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001328///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001329/// objc-parms:
1330/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001331///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001332/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001333/// , ...
1334///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001335/// objc-keyword-attributes: [OBJC2]
1336/// __attribute__((unused))
1337///
John McCall48871652010-08-21 09:40:31 +00001338Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001339 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001340 tok::ObjCKeywordKind MethodImplKind,
1341 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001342 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001343
Douglas Gregor636a61e2010-04-07 00:21:17 +00001344 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001345 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1346 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001347 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001348 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001349 }
1350
Chris Lattner2ebb1782008-08-23 01:48:03 +00001351 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001352 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001353 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001354 if (Tok.is(tok::l_paren))
Faisal Vali421b2d12017-12-29 05:41:00 +00001355 ReturnType = ParseObjCTypeName(DSRet, DeclaratorContext::ObjCResultContext,
Craig Topper161e4db2014-05-21 06:02:52 +00001356 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001357
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001358 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001359 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001360 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001361 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001362
Douglas Gregor636a61e2010-04-07 00:21:17 +00001363 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001364 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001365 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001366 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001367 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001368 }
1369
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001370 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001371 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001372 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001373
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001374 // An unnamed colon is valid.
1375 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001376 Diag(Tok, diag::err_expected_selector_for_method)
1377 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001378 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001379 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001380 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001381 }
Mike Stump11289f42009-09-09 15:08:12 +00001382
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001383 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001384 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001385 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001386 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001387 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001388
Chris Lattner5700fab2007-10-07 02:00:24 +00001389 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001390 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001391 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001392 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001393 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001394 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001395 methodAttrs.getList(), MethodImplKind,
1396 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001397 PD.complete(Result);
1398 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001399 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001400
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001401 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001402 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001403 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001404 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1405 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001406
1407 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001408 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001409 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001410 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001411
Chris Lattner5700fab2007-10-07 02:00:24 +00001412 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001413 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001414 break;
Mike Stump11289f42009-09-09 15:08:12 +00001415
David Blaikieefdccaa2016-01-15 23:43:34 +00001416 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001417 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001418 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
Faisal Vali421b2d12017-12-29 05:41:00 +00001419 DeclaratorContext::ObjCParameterContext,
John McCalla55902b2011-10-01 09:56:14 +00001420 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001421
Chris Lattner5700fab2007-10-07 02:00:24 +00001422 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001423 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001424 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001425 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001426 MaybeParseGNUAttributes(paramAttrs);
1427 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001428 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001429
Douglas Gregor45879692010-07-08 23:37:41 +00001430 // Code completion for the next piece of the selector.
1431 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001432 KeyIdents.push_back(SelIdent);
1433 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1434 mType == tok::minus,
1435 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001436 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001437 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001438 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001439 }
Alex Lorenzf1278212017-04-11 15:01:53 +00001440
1441 if (expectIdentifier())
1442 break; // missing argument name.
Mike Stump11289f42009-09-09 15:08:12 +00001443
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001444 ArgInfo.Name = Tok.getIdentifierInfo();
1445 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001446 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001447
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001448 ArgInfos.push_back(ArgInfo);
1449 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001450 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001451
John McCall084e83d2011-03-24 11:26:52 +00001452 // Make sure the attributes persist.
1453 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1454
Douglas Gregor95887f92010-07-08 23:20:03 +00001455 // Code completion for the next piece of the selector.
1456 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001457 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1458 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001459 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001460 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001461 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001462 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001463 }
1464
Chris Lattner5700fab2007-10-07 02:00:24 +00001465 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001466 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001467 if (!SelIdent && Tok.isNot(tok::colon))
1468 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001469 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001470 SourceLocation ColonLoc = Tok.getLocation();
1471 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001472 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1473 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1474 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001475 }
1476 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001477 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001478 }
Mike Stump11289f42009-09-09 15:08:12 +00001479
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001480 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001481 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001482 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001483 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001484 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001485 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001486 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001487 ConsumeToken();
1488 break;
1489 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001490 if (!cStyleParamWarned) {
1491 Diag(Tok, diag::warn_cstyle_param);
1492 cStyleParamWarned = true;
1493 }
John McCall084e83d2011-03-24 11:26:52 +00001494 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001495 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001496 // Parse the declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001497 Declarator ParmDecl(DS, DeclaratorContext::PrototypeContext);
Chris Lattner5700fab2007-10-07 02:00:24 +00001498 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001499 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001500 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001501 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1502 ParmDecl.getIdentifierLoc(),
1503 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001504 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001505 }
Mike Stump11289f42009-09-09 15:08:12 +00001506
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001507 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001508 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001509 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001510 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001511
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001512 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001513 return nullptr;
1514
Chris Lattner5700fab2007-10-07 02:00:24 +00001515 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1516 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001517 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001518 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001519 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001520 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001521 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001522 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001523 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001524
John McCall28a6aea2009-11-04 02:18:39 +00001525 PD.complete(Result);
1526 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001527}
1528
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001529/// objc-protocol-refs:
1530/// '<' identifier-list '>'
1531///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001532bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001533ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1534 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001535 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001536 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1537 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001538 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001539
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001540 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001541
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001542 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001543
Chris Lattner3bbae002008-07-26 04:03:38 +00001544 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001545 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001546 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001547 cutOffParsing();
1548 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001549 }
1550
Alex Lorenzf1278212017-04-11 15:01:53 +00001551 if (expectIdentifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001552 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001553 return true;
1554 }
1555 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1556 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001557 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001559
Alp Toker383d2c42014-01-01 03:08:43 +00001560 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001561 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001562 }
Mike Stump11289f42009-09-09 15:08:12 +00001563
Chris Lattner3bbae002008-07-26 04:03:38 +00001564 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001565 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001566 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001567 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001568
Chris Lattner3bbae002008-07-26 04:03:38 +00001569 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001570 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001571 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001572 return false;
1573}
1574
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001575TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001576 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001577 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001578
1579 SourceLocation lAngleLoc;
1580 SmallVector<Decl *, 8> protocols;
1581 SmallVector<SourceLocation, 8> protocolLocs;
1582 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1583 lAngleLoc, rAngleLoc,
1584 /*consumeLastToken=*/true);
1585 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1586 protocols,
1587 protocolLocs,
1588 rAngleLoc);
1589 if (result.isUsable()) {
1590 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1591 << FixItHint::CreateInsertion(lAngleLoc, "id")
1592 << SourceRange(lAngleLoc, rAngleLoc);
1593 }
1594
1595 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001596}
1597
Douglas Gregore9d95f12015-07-07 03:57:35 +00001598/// Parse Objective-C type arguments or protocol qualifiers.
1599///
1600/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001601/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001602///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001603void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001604 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001605 SourceLocation &typeArgsLAngleLoc,
1606 SmallVectorImpl<ParsedType> &typeArgs,
1607 SourceLocation &typeArgsRAngleLoc,
1608 SourceLocation &protocolLAngleLoc,
1609 SmallVectorImpl<Decl *> &protocols,
1610 SmallVectorImpl<SourceLocation> &protocolLocs,
1611 SourceLocation &protocolRAngleLoc,
1612 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001613 bool warnOnIncompleteProtocols) {
1614 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1615 SourceLocation lAngleLoc = ConsumeToken();
1616
1617 // Whether all of the elements we've parsed thus far are single
1618 // identifiers, which might be types or might be protocols.
1619 bool allSingleIdentifiers = true;
1620 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001621 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001622
1623 // Parse a list of comma-separated identifiers, bailing out if we
1624 // see something different.
1625 do {
1626 // Parse a single identifier.
1627 if (Tok.is(tok::identifier) &&
1628 (NextToken().is(tok::comma) ||
1629 NextToken().is(tok::greater) ||
1630 NextToken().is(tok::greatergreater))) {
1631 identifiers.push_back(Tok.getIdentifierInfo());
1632 identifierLocs.push_back(ConsumeToken());
1633 continue;
1634 }
1635
1636 if (Tok.is(tok::code_completion)) {
1637 // FIXME: Also include types here.
1638 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1639 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1640 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1641 identifierLocs[i]));
1642 }
1643
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001644 QualType BaseT = Actions.GetTypeFromParser(baseType);
1645 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1646 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1647 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001648 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001649 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001650 cutOffParsing();
1651 return;
1652 }
1653
1654 allSingleIdentifiers = false;
1655 break;
1656 } while (TryConsumeToken(tok::comma));
1657
1658 // If we parsed an identifier list, semantic analysis sorts out
1659 // whether it refers to protocols or to type arguments.
1660 if (allSingleIdentifiers) {
1661 // Parse the closing '>'.
1662 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001663 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001664 /*ObjCGenericList=*/true);
1665
1666 // Let Sema figure out what we parsed.
1667 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001668 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001669 lAngleLoc,
1670 identifiers,
1671 identifierLocs,
1672 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001673 typeArgsLAngleLoc,
1674 typeArgs,
1675 typeArgsRAngleLoc,
1676 protocolLAngleLoc,
1677 protocols,
1678 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001679 warnOnIncompleteProtocols);
1680 return;
1681 }
1682
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001683 // We parsed an identifier list but stumbled into non single identifiers, this
1684 // means we might (a) check that what we already parsed is a legitimate type
1685 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1686 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001687
1688 // Convert the identifiers into type arguments.
1689 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001690 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1691 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1692 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1693 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1694
Douglas Gregore9d95f12015-07-07 03:57:35 +00001695 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1696 ParsedType typeArg
1697 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1698 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001699 DeclSpec DS(AttrFactory);
1700 const char *prevSpec = nullptr;
1701 unsigned diagID;
Faisal Vali090da2d2018-01-01 18:23:28 +00001702 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1703 typeArg, Actions.getASTContext().getPrintingPolicy());
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001704
1705 // Form a declarator to turn this into a type.
Faisal Vali421b2d12017-12-29 05:41:00 +00001706 Declarator D(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001707 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001708 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001709 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001710 if (!foundValidTypeId) {
1711 foundValidTypeId = identifiers[i];
1712 foundValidTypeSrcLoc = identifierLocs[i];
1713 }
1714 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001715 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001716 unknownTypeArgs.push_back(identifiers[i]);
1717 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1718 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001719 } else {
1720 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001721 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1722 unknownTypeArgs.push_back(identifiers[i]);
1723 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1724 } else if (!foundProtocolId) {
1725 foundProtocolId = identifiers[i];
1726 foundProtocolSrcLoc = identifierLocs[i];
1727 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001728 }
1729 }
1730
1731 // Continue parsing type-names.
1732 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001733 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001734 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001735
1736 // Consume the '...' for a pack expansion.
1737 SourceLocation ellipsisLoc;
1738 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1739 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1740 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1741 }
1742
Douglas Gregore9d95f12015-07-07 03:57:35 +00001743 if (typeArg.isUsable()) {
1744 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001745 if (!foundValidTypeId) {
1746 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1747 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1748 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001749 } else {
1750 invalid = true;
1751 }
1752 } while (TryConsumeToken(tok::comma));
1753
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001754 // Diagnose the mix between type args and protocols.
1755 if (foundProtocolId && foundValidTypeId)
1756 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1757 foundValidTypeId,
1758 foundValidTypeSrcLoc);
1759
1760 // Diagnose unknown arg types.
1761 ParsedType T;
1762 if (unknownTypeArgs.size())
1763 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1764 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1765 getCurScope(), nullptr, T);
1766
Douglas Gregore9d95f12015-07-07 03:57:35 +00001767 // Parse the closing '>'.
1768 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001769 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001770 /*ObjCGenericList=*/true);
1771
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001772 if (invalid) {
1773 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001774 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001775 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001776
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001777 // Record left/right angle locations.
1778 typeArgsLAngleLoc = lAngleLoc;
1779 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001780}
1781
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001782void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001783 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001784 SourceLocation &typeArgsLAngleLoc,
1785 SmallVectorImpl<ParsedType> &typeArgs,
1786 SourceLocation &typeArgsRAngleLoc,
1787 SourceLocation &protocolLAngleLoc,
1788 SmallVectorImpl<Decl *> &protocols,
1789 SmallVectorImpl<SourceLocation> &protocolLocs,
1790 SourceLocation &protocolRAngleLoc,
1791 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001792 assert(Tok.is(tok::less));
1793
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001794 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001795 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1796 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001797 typeArgs,
1798 typeArgsRAngleLoc,
1799 protocolLAngleLoc,
1800 protocols,
1801 protocolLocs,
1802 protocolRAngleLoc,
1803 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001804 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001805 if (Tok.is(tok::eof)) // Nothing else to do here...
1806 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001807
1808 // An Objective-C object pointer followed by type arguments
1809 // can then be followed again by a set of protocol references, e.g.,
1810 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001811 if ((consumeLastToken && Tok.is(tok::less)) ||
1812 (!consumeLastToken && NextToken().is(tok::less))) {
1813 // If we aren't consuming the last token, the prior '>' is still hanging
1814 // there. Consume it before we parse the protocol qualifiers.
1815 if (!consumeLastToken)
1816 ConsumeToken();
1817
1818 if (!protocols.empty()) {
1819 SkipUntilFlags skipFlags = SkipUntilFlags();
1820 if (!consumeLastToken)
1821 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001822 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001823 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1824 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001825 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001826 ParseObjCProtocolReferences(protocols, protocolLocs,
1827 /*WarnOnDeclarations=*/false,
1828 /*ForObjCContainer=*/false,
1829 protocolLAngleLoc, protocolRAngleLoc,
1830 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001831 }
1832 }
1833}
1834
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001835TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1836 SourceLocation loc,
1837 ParsedType type,
1838 bool consumeLastToken,
1839 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001840 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001841 SourceLocation typeArgsLAngleLoc;
1842 SmallVector<ParsedType, 4> typeArgs;
1843 SourceLocation typeArgsRAngleLoc;
1844 SourceLocation protocolLAngleLoc;
1845 SmallVector<Decl *, 4> protocols;
1846 SmallVector<SourceLocation, 4> protocolLocs;
1847 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001848
1849 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001850 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001851 typeArgsRAngleLoc, protocolLAngleLoc,
1852 protocols, protocolLocs,
1853 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001854
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001855 if (Tok.is(tok::eof))
1856 return true; // Invalid type result.
1857
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001858 // Compute the location of the last token.
1859 if (consumeLastToken)
1860 endLoc = PrevTokLocation;
1861 else
1862 endLoc = Tok.getLocation();
1863
1864 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1865 getCurScope(),
1866 loc,
1867 type,
1868 typeArgsLAngleLoc,
1869 typeArgs,
1870 typeArgsRAngleLoc,
1871 protocolLAngleLoc,
1872 protocols,
1873 protocolLocs,
1874 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001875}
1876
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001877void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1878 BalancedDelimiterTracker &T,
1879 SmallVectorImpl<Decl *> &AllIvarDecls,
1880 bool RBraceMissing) {
1881 if (!RBraceMissing)
1882 T.consumeClose();
1883
1884 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1885 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1886 Actions.ActOnObjCContainerFinishDefinition();
1887 // Call ActOnFields() even if we don't have any decls. This is useful
1888 // for code rewriting tools that need to be aware of the empty list.
1889 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1890 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001891 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001892}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001893
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001894/// objc-class-instance-variables:
1895/// '{' objc-instance-variable-decl-list[opt] '}'
1896///
1897/// objc-instance-variable-decl-list:
1898/// objc-visibility-spec
1899/// objc-instance-variable-decl ';'
1900/// ';'
1901/// objc-instance-variable-decl-list objc-visibility-spec
1902/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1903/// objc-instance-variable-decl-list ';'
1904///
1905/// objc-visibility-spec:
1906/// @private
1907/// @protected
1908/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001909/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001910///
1911/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001912/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001913///
John McCall48871652010-08-21 09:40:31 +00001914void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001915 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001916 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001917 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001918 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001919
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001920 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001921 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001922
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001923 BalancedDelimiterTracker T(*this, tok::l_brace);
1924 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001925 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001926 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001927 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001928
Steve Naroff00433d32007-08-21 21:17:12 +00001929 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001930 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001931 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001932 continue;
1933 }
Mike Stump11289f42009-09-09 15:08:12 +00001934
Steve Naroff00433d32007-08-21 21:17:12 +00001935 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001936 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001937 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001938 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001939 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001940 }
1941
Steve Naroff7c348172007-08-23 18:16:40 +00001942 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001943 case tok::objc_private:
1944 case tok::objc_public:
1945 case tok::objc_protected:
1946 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001947 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001948 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001949 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001950
1951 case tok::objc_end:
1952 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001953 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1954 Tok.setKind(tok::at);
1955 Tok.setLength(1);
1956 PP.EnterToken(Tok);
1957 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1958 T, AllIvarDecls, true);
1959 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001960
1961 default:
1962 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1963 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001964 }
1965 }
Mike Stump11289f42009-09-09 15:08:12 +00001966
Douglas Gregor48d46252010-01-13 21:54:15 +00001967 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001968 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001969 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001970 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001971 }
John McCallcfefb6d2009-11-03 02:38:08 +00001972
Benjamin Kramera39beb92014-09-03 11:06:10 +00001973 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1974 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1975 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001976 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001977 Decl *Field = Actions.ActOnIvar(
1978 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1979 FD.BitfieldSize, visibility);
1980 Actions.ActOnObjCContainerFinishDefinition();
1981 if (Field)
1982 AllIvarDecls.push_back(Field);
1983 FD.complete(Field);
1984 };
John McCallcfefb6d2009-11-03 02:38:08 +00001985
Chris Lattnera12405b2008-04-10 06:46:29 +00001986 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001987 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001988 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001989
Chris Lattner0ef13522007-10-09 17:51:17 +00001990 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001991 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001992 } else {
1993 Diag(Tok, diag::err_expected_semi_decl_list);
1994 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001995 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001996 }
1997 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001998 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1999 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002000}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002001
2002/// objc-protocol-declaration:
2003/// objc-protocol-definition
2004/// objc-protocol-forward-reference
2005///
2006/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002007/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002008/// objc-protocol-refs[opt]
2009/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002010/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002011///
2012/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002013/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002014///
James Dennett1355bd12012-06-11 06:19:40 +00002015/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002016/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002017/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002018Parser::DeclGroupPtrTy
2019Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2020 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002021 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002022 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2023 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002024
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002025 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002026 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002027 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002028 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002029 }
2030
Nico Weber69a79142013-04-04 00:15:10 +00002031 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002032
Alex Lorenzf1278212017-04-11 15:01:53 +00002033 if (expectIdentifier())
2034 return nullptr; // missing protocol name.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002035 // Save the protocol name, then consume it.
2036 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2037 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002038
Alp Toker383d2c42014-01-01 03:08:43 +00002039 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002040 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002041 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002042 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002043 }
Mike Stump11289f42009-09-09 15:08:12 +00002044
Erik Verbruggenf9887852011-12-08 09:58:43 +00002045 CheckNestedObjCContexts(AtLoc);
2046
Chris Lattner0ef13522007-10-09 17:51:17 +00002047 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002048 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002049 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2050
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002051 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002052 while (1) {
2053 ConsumeToken(); // the ','
Alex Lorenzf1278212017-04-11 15:01:53 +00002054 if (expectIdentifier()) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002055 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002056 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002057 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002058 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2059 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002060 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002061
Chris Lattner0ef13522007-10-09 17:51:17 +00002062 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002063 break;
2064 }
2065 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002066 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002067 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002068
Craig Topper0f723bb2015-10-22 05:00:01 +00002069 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002070 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002071 }
Mike Stump11289f42009-09-09 15:08:12 +00002072
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002073 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002074 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002075
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002076 SmallVector<Decl *, 8> ProtocolRefs;
2077 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002078 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002079 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002080 LAngleLoc, EndProtoLoc,
2081 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002082 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002083
John McCall48871652010-08-21 09:40:31 +00002084 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002085 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002086 ProtocolRefs.data(),
2087 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002088 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002089 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002090
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002091 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002092 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002093}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002094
2095/// objc-implementation:
2096/// objc-class-implementation-prologue
2097/// objc-category-implementation-prologue
2098///
2099/// objc-class-implementation-prologue:
2100/// @implementation identifier objc-superclass[opt]
2101/// objc-class-instance-variables[opt]
2102///
2103/// objc-category-implementation-prologue:
2104/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002105Parser::DeclGroupPtrTy
2106Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002107 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2108 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002109 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002110 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002111
Douglas Gregor49c22a72009-11-18 16:26:39 +00002112 // Code completion after '@implementation'.
2113 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002114 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002115 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002116 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002117 }
2118
Nico Weber69a79142013-04-04 00:15:10 +00002119 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002120
Alex Lorenzf1278212017-04-11 15:01:53 +00002121 if (expectIdentifier())
2122 return nullptr; // missing class or category name.
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002123 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002124 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002125 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002126 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002127
Douglas Gregor85f3f952015-07-07 03:57:15 +00002128 // Neither a type parameter list nor a list of protocol references is
2129 // permitted here. Parse and diagnose them.
2130 if (Tok.is(tok::less)) {
2131 SourceLocation lAngleLoc, rAngleLoc;
2132 SmallVector<IdentifierLocPair, 8> protocolIdents;
2133 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002134 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2135 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2136 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002137 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2138 << SourceRange(diagLoc, PrevTokLocation);
2139 } else if (lAngleLoc.isValid()) {
2140 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2141 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2142 }
2143 }
2144
Mike Stump11289f42009-09-09 15:08:12 +00002145 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002146 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002147 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002148 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002149 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002150
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002151 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002152 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002153 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002154 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002155 }
2156
Chris Lattner0ef13522007-10-09 17:51:17 +00002157 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002158 categoryId = Tok.getIdentifierInfo();
2159 categoryLoc = ConsumeToken();
2160 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002161 Diag(Tok, diag::err_expected)
2162 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002163 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002164 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002165 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002166 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002167 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002168 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002169 }
2170 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002171 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2172 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002173 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2174 SmallVector<Decl *, 4> protocols;
2175 SmallVector<SourceLocation, 4> protocolLocs;
2176 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2177 /*warnOnIncompleteProtocols=*/false,
2178 /*ForObjCContainer=*/false,
2179 protocolLAngleLoc, protocolRAngleLoc,
2180 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002181 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002182 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002183 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002184 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002185
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002186 } else {
2187 // We have a class implementation
2188 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002189 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002190 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002191 // We have a super class
Alex Lorenzf1278212017-04-11 15:01:53 +00002192 if (expectIdentifier())
2193 return nullptr; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002194 superClassId = Tok.getIdentifierInfo();
2195 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002196 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002197 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2198 AtLoc, nameId, nameLoc,
2199 superClassId, superClassLoc);
2200
2201 if (Tok.is(tok::l_brace)) // we have ivars
2202 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002203 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2204 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002205
2206 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2207 SmallVector<Decl *, 4> protocols;
2208 SmallVector<SourceLocation, 4> protocolLocs;
2209 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2210 /*warnOnIncompleteProtocols=*/false,
2211 /*ForObjCContainer=*/false,
2212 protocolLAngleLoc, protocolRAngleLoc,
2213 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002214 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002215 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002216 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002217
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002218 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002219
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002220 {
2221 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002222 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002223 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002224 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002225 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2226 DeclGroupRef DG = DGP.get();
2227 DeclsInGroup.append(DG.begin(), DG.end());
2228 }
2229 }
2230 }
2231
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002232 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002233}
Steve Naroff33a1e802007-10-29 21:38:07 +00002234
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002235Parser::DeclGroupPtrTy
2236Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002237 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2238 "ParseObjCAtEndDeclaration(): Expected @end");
2239 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002240 if (CurParsedObjCImpl)
2241 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002242 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002243 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002244 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002245 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002246}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002247
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002248Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2249 if (!Finished) {
2250 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002251 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002252 P.Diag(P.Tok, diag::err_objc_missing_end)
2253 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2254 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2255 << Sema::OCK_Implementation;
2256 }
2257 }
Craig Topper161e4db2014-05-21 06:02:52 +00002258 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002259 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002260}
2261
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002262void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2263 assert(!Finished);
Alex Lorenz6c9af502017-07-03 10:12:24 +00002264 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl, AtEnd.getBegin());
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002265 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002266 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2267 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002268
2269 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2270
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002271 if (HasCFunction)
2272 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2273 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2274 false/*c-functions*/);
2275
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002276 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002277 for (LateParsedObjCMethodContainer::iterator
2278 I = LateParsedObjCMethods.begin(),
2279 E = LateParsedObjCMethods.end(); I != E; ++I)
2280 delete *I;
2281 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002282
2283 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002284}
2285
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002286/// compatibility-alias-decl:
2287/// @compatibility_alias alias-name class-name ';'
2288///
John McCall48871652010-08-21 09:40:31 +00002289Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002290 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2291 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2292 ConsumeToken(); // consume compatibility_alias
Alex Lorenzf1278212017-04-11 15:01:53 +00002293 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002294 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002295 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2296 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Alex Lorenzf1278212017-04-11 15:01:53 +00002297 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002298 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002299 IdentifierInfo *classId = Tok.getIdentifierInfo();
2300 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002301 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002302 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2303 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002304}
2305
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002306/// property-synthesis:
2307/// @synthesize property-ivar-list ';'
2308///
2309/// property-ivar-list:
2310/// property-ivar
2311/// property-ivar-list ',' property-ivar
2312///
2313/// property-ivar:
2314/// identifier
2315/// identifier '=' identifier
2316///
John McCall48871652010-08-21 09:40:31 +00002317Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002318 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002319 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002320 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002321
Douglas Gregor88e72a02009-11-18 19:45:45 +00002322 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002323 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002324 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002325 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002326 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002327 }
2328
Douglas Gregor88e72a02009-11-18 19:45:45 +00002329 if (Tok.isNot(tok::identifier)) {
2330 Diag(Tok, diag::err_synthesized_property_name);
2331 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002332 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002333 }
Craig Topper161e4db2014-05-21 06:02:52 +00002334
2335 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002336 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2337 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002338 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002339 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002340 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002341 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002342 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002343 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002344 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002345 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002346
2347 if (expectIdentifier())
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002348 break;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002349 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002350 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002351 }
Manman Ren5b786402016-01-28 18:49:28 +00002352 Actions.ActOnPropertyImplDecl(
2353 getCurScope(), atLoc, propertyLoc, true,
2354 propertyId, propertyIvar, propertyIvarLoc,
2355 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002356 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002357 break;
2358 ConsumeToken(); // consume ','
2359 }
Alp Toker383d2c42014-01-01 03:08:43 +00002360 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002361 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002362}
2363
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002364/// property-dynamic:
2365/// @dynamic property-list
2366///
2367/// property-list:
2368/// identifier
2369/// property-list ',' identifier
2370///
John McCall48871652010-08-21 09:40:31 +00002371Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002372 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2373 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002374 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002375
2376 bool isClassProperty = false;
2377 if (Tok.is(tok::l_paren)) {
2378 ConsumeParen();
2379 const IdentifierInfo *II = Tok.getIdentifierInfo();
2380
2381 if (!II) {
2382 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2383 SkipUntil(tok::r_paren, StopAtSemi);
2384 } else {
2385 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2386 if (II->isStr("class")) {
2387 isClassProperty = true;
2388 if (Tok.isNot(tok::r_paren)) {
2389 Diag(Tok, diag::err_expected) << tok::r_paren;
2390 SkipUntil(tok::r_paren, StopAtSemi);
2391 } else
2392 ConsumeParen();
2393 } else {
2394 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2395 SkipUntil(tok::r_paren, StopAtSemi);
2396 }
2397 }
2398 }
2399
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002400 while (true) {
2401 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002402 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002403 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002404 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002405 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002406
2407 if (expectIdentifier()) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002408 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002409 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002410 }
2411
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002412 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2413 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002414 Actions.ActOnPropertyImplDecl(
2415 getCurScope(), atLoc, propertyLoc, false,
2416 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002417 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002418 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002419
Chris Lattner0ef13522007-10-09 17:51:17 +00002420 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002421 break;
2422 ConsumeToken(); // consume ','
2423 }
Alp Toker383d2c42014-01-01 03:08:43 +00002424 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002425 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002426}
Mike Stump11289f42009-09-09 15:08:12 +00002427
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002428/// objc-throw-statement:
2429/// throw expression[opt];
2430///
John McCalldadc5752010-08-24 06:29:42 +00002431StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2432 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002433 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002434 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002435 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002436 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002437 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002438 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002439 }
2440 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002441 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002442 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002443 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002444}
2445
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002446/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002447/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002448///
John McCalldadc5752010-08-24 06:29:42 +00002449StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002450Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002451 ConsumeToken(); // consume synchronized
2452 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002453 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002454 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002455 }
John McCalld9bb7432011-07-27 21:50:02 +00002456
2457 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002458 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002459 ExprResult operand(ParseExpression());
2460
2461 if (Tok.is(tok::r_paren)) {
2462 ConsumeParen(); // ')'
2463 } else {
2464 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002465 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002466
2467 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002468 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002469 }
John McCalld9bb7432011-07-27 21:50:02 +00002470
2471 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002472 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002473 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002474 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002475 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002476 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002477
John McCalld9bb7432011-07-27 21:50:02 +00002478 // Check the @synchronized operand now.
2479 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002480 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002481
John McCalld9bb7432011-07-27 21:50:02 +00002482 // Parse the compound statement within a new scope.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002483 ParseScope bodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalld9bb7432011-07-27 21:50:02 +00002484 StmtResult body(ParseCompoundStatementBody());
2485 bodyScope.Exit();
2486
2487 // If there was a semantic or parse error earlier with the
2488 // operand, fail now.
2489 if (operand.isInvalid())
2490 return StmtError();
2491
2492 if (body.isInvalid())
2493 body = Actions.ActOnNullStmt(Tok.getLocation());
2494
2495 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002496}
2497
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002498/// objc-try-catch-statement:
2499/// @try compound-statement objc-catch-list[opt]
2500/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2501///
2502/// objc-catch-list:
2503/// @catch ( parameter-declaration ) compound-statement
2504/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2505/// catch-parameter-declaration:
2506/// parameter-declaration
2507/// '...' [OBJC2]
2508///
John McCalldadc5752010-08-24 06:29:42 +00002509StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002510 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002511
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002512 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002513 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002514 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002515 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002516 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002517 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002518 StmtResult FinallyStmt;
Momchil Velikov57c681f2017-08-10 15:43:06 +00002519 ParseScope TryScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalldadc5752010-08-24 06:29:42 +00002520 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002521 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002522 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002523 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002524
Chris Lattner0ef13522007-10-09 17:51:17 +00002525 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002526 // At this point, we need to lookahead to determine if this @ is the start
2527 // of an @catch or @finally. We don't want to consume the @ token if this
2528 // is an @try or @encode or something else.
2529 Token AfterAt = GetLookAheadToken(1);
2530 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2531 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2532 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002533
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002534 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002535 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002536 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002537 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002538 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002539 ConsumeParen();
Momchil Velikov57c681f2017-08-10 15:43:06 +00002540 ParseScope CatchScope(this, Scope::DeclScope |
2541 Scope::CompoundStmtScope |
2542 Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002543 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002544 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002545 ParseDeclarationSpecifiers(DS);
Faisal Vali421b2d12017-12-29 05:41:00 +00002546 Declarator ParmDecl(DS, DeclaratorContext::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002547 ParseDeclarator(ParmDecl);
2548
Douglas Gregore11ee112010-04-23 23:01:43 +00002549 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002550 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002551 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002552 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002553 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002554
Steve Naroff65a00892009-04-07 22:56:58 +00002555 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002556
Steve Naroff65a00892009-04-07 22:56:58 +00002557 if (Tok.is(tok::r_paren))
2558 RParenLoc = ConsumeParen();
2559 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002560 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002561
John McCalldadc5752010-08-24 06:29:42 +00002562 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002563 if (Tok.is(tok::l_brace))
2564 CatchBody = ParseCompoundStatementBody();
2565 else
Alp Tokerec543272013-12-24 09:48:30 +00002566 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002567 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002568 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002569
John McCalldadc5752010-08-24 06:29:42 +00002570 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002571 RParenLoc,
2572 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002573 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002574 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002575 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002576
Steve Naroffe6016792008-02-05 21:27:35 +00002577 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002578 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2579 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002580 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002581 }
2582 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002583 } else {
2584 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002585 ConsumeToken(); // consume finally
Momchil Velikov57c681f2017-08-10 15:43:06 +00002586 ParseScope FinallyScope(this,
2587 Scope::DeclScope | Scope::CompoundStmtScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002588
John McCalldadc5752010-08-24 06:29:42 +00002589 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002590 if (Tok.is(tok::l_brace))
2591 FinallyBody = ParseCompoundStatementBody();
2592 else
Alp Tokerec543272013-12-24 09:48:30 +00002593 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002594 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002595 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002596 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002597 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002598 catch_or_finally_seen = true;
2599 break;
2600 }
2601 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002602 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002603 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002604 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002605 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002606
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002607 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002608 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002609 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002610}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002611
John McCall31168b02011-06-15 23:02:42 +00002612/// objc-autoreleasepool-statement:
2613/// @autoreleasepool compound-statement
2614///
2615StmtResult
2616Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2617 ConsumeToken(); // consume autoreleasepool
2618 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002619 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002620 return StmtError();
2621 }
2622 // Enter a scope to hold everything within the compound stmt. Compound
2623 // statements can always hold declarations.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002624 ParseScope BodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCall31168b02011-06-15 23:02:42 +00002625
2626 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2627
2628 BodyScope.Exit();
2629 if (AutoreleasePoolBody.isInvalid())
2630 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2631 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002632 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002633}
2634
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002635/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2636/// for later parsing.
2637void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002638 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2639 trySkippingFunctionBody()) {
2640 Actions.ActOnSkippedFunctionBody(MDecl);
2641 return;
2642 }
2643
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002644 LexedMethod* LM = new LexedMethod(this, MDecl);
2645 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2646 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002647 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002648 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002649 if (Tok.is(tok::kw_try)) {
2650 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002651 if (Tok.is(tok::colon)) {
2652 Toks.push_back(Tok);
2653 ConsumeToken();
2654 while (Tok.isNot(tok::l_brace)) {
2655 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2656 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2657 }
2658 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002659 Toks.push_back(Tok); // also store '{'
2660 }
2661 else if (Tok.is(tok::colon)) {
2662 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002663 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002664 while (Tok.isNot(tok::l_brace)) {
2665 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2666 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2667 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002668 Toks.push_back(Tok); // also store '{'
2669 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002670 ConsumeBrace();
2671 // Consume everything up to (and including) the matching right brace.
2672 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002673 while (Tok.is(tok::kw_catch)) {
2674 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2675 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2676 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002677}
2678
Steve Naroff09bf8152007-09-06 21:24:23 +00002679/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002680///
John McCall48871652010-08-21 09:40:31 +00002681Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002682 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002683
John McCallfaf5fb42010-08-26 23:41:50 +00002684 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2685 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002686
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002687 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002688 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002689 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002690 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002691 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002692 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002693 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002694 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002695
Steve Naroffbb875722007-11-11 19:54:21 +00002696 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002697 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002698 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002699
Steve Naroffbb875722007-11-11 19:54:21 +00002700 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002701 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002702
Steve Naroffbb875722007-11-11 19:54:21 +00002703 // If we didn't find the '{', bail out.
2704 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002705 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002706 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002707
2708 if (!MDecl) {
2709 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002710 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002711 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002712 }
2713
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002714 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002715 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002716 assert (CurParsedObjCImpl
2717 && "ParseObjCMethodDefinition - Method out of @implementation");
2718 // Consume the tokens and store them for later parsing.
2719 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002720 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002721}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002722
John McCalldadc5752010-08-24 06:29:42 +00002723StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002724 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002725 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002726 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002727 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002728 }
2729
2730 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002731 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002732
2733 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002734 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002735
2736 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002737 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002738
2739 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2740 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002741
2742 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2743 getLangOpts().DebuggerSupport) {
2744 SkipUntil(tok::semi);
2745 return Actions.ActOnNullStmt(Tok.getLocation());
2746 }
2747
Alex Lorenza589abc2016-12-01 12:14:38 +00002748 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002749 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002750 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002751 // If the expression is invalid, skip ahead to the next semicolon. Not
2752 // doing this opens us up to the possibility of infinite loops if
2753 // ParseExpression does not consume any tokens.
2754 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002755 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002756 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002757
Steve Naroffe6016792008-02-05 21:27:35 +00002758 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002759 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002760 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002761}
2762
John McCalldadc5752010-08-24 06:29:42 +00002763ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002764 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002765 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002766 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002767 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002768 return ExprError();
2769
Ted Kremeneke65b0862012-03-06 20:05:56 +00002770 case tok::minus:
2771 case tok::plus: {
2772 tok::TokenKind Kind = Tok.getKind();
2773 SourceLocation OpLoc = ConsumeToken();
2774
2775 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002776 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002777 switch (Kind) {
2778 case tok::minus: Symbol = "-"; break;
2779 case tok::plus: Symbol = "+"; break;
2780 default: llvm_unreachable("missing unary operator case");
2781 }
2782 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2783 << Symbol;
2784 return ExprError();
2785 }
2786
2787 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2788 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002789 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002790 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002791 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002792
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002793 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002794 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002795 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002796
2797 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002798 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002799 }
2800
Chris Lattnere002fbe2007-12-12 01:04:12 +00002801 case tok::string_literal: // primary-expression: string-literal
2802 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002803 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002804
2805 case tok::char_constant:
2806 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2807
2808 case tok::numeric_constant:
2809 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2810
2811 case tok::kw_true: // Objective-C++, etc.
2812 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2813 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2814 case tok::kw_false: // Objective-C++, etc.
2815 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2816 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2817
2818 case tok::l_square:
2819 // Objective-C array literal
2820 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2821
2822 case tok::l_brace:
2823 // Objective-C dictionary literal
2824 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2825
Patrick Beard0caa3942012-04-19 00:25:12 +00002826 case tok::l_paren:
2827 // Objective-C boxed expression
2828 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2829
Chris Lattnere002fbe2007-12-12 01:04:12 +00002830 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002831 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002832 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002833
Chris Lattner197a3012008-08-05 06:19:09 +00002834 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2835 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002836 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002837 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002838 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002839 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002840 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002841 case tok::objc_available:
2842 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002843 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002844 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002845 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2846 // that this is a proper statement where such directives could actually
2847 // occur.
2848 if (GetLookAheadToken(1).is(tok::l_brace) &&
2849 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002850 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2851 str =
2852 ch == 't' ? "try"
2853 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002854 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002855 }
2856 if (str) {
2857 SourceLocation kwLoc = Tok.getLocation();
2858 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2859 FixItHint::CreateReplacement(kwLoc, str));
2860 }
2861 else
2862 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2863 }
Chris Lattner197a3012008-08-05 06:19:09 +00002864 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002865 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002866}
2867
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002868/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002869///
2870/// This routine parses the receiver of a message send in
2871/// Objective-C++ either as a type or as an expression. Note that this
2872/// routine must not be called to parse a send to 'super', since it
2873/// has no way to return such a result.
2874///
2875/// \param IsExpr Whether the receiver was parsed as an expression.
2876///
2877/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2878/// IsExpr is true), the parsed expression. If the receiver was parsed
2879/// as a type (\c IsExpr is false), the parsed type.
2880///
2881/// \returns True if an error occurred during parsing or semantic
2882/// analysis, in which case the arguments do not have valid
2883/// values. Otherwise, returns false for a successful parse.
2884///
2885/// objc-receiver: [C++]
2886/// 'super' [not parsed here]
2887/// expression
2888/// simple-type-specifier
2889/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002890bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002891 InMessageExpressionRAIIObject InMessage(*this, true);
2892
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002893 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2894 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002895 TryAnnotateTypeOrScopeToken();
2896
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002897 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002898 // objc-receiver:
2899 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002900 // Make sure any typos in the receiver are corrected or diagnosed, so that
2901 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2902 // only the things that are valid ObjC receivers?
2903 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002904 if (Receiver.isInvalid())
2905 return true;
2906
2907 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002908 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002909 return false;
2910 }
2911
2912 // objc-receiver:
2913 // typename-specifier
2914 // simple-type-specifier
2915 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002916 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002917 ParseCXXSimpleTypeSpecifier(DS);
2918
2919 if (Tok.is(tok::l_paren)) {
2920 // If we see an opening parentheses at this point, we are
2921 // actually parsing an expression that starts with a
2922 // function-style cast, e.g.,
2923 //
2924 // postfix-expression:
2925 // simple-type-specifier ( expression-list [opt] )
2926 // typename-specifier ( expression-list [opt] )
2927 //
2928 // Parse the remainder of this case, then the (optional)
2929 // postfix-expression suffix, followed by the (optional)
2930 // right-hand side of the binary expression. We have an
2931 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002932 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002933 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002934 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002935 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002936 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002937 if (Receiver.isInvalid())
2938 return true;
2939
2940 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002941 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002942 return false;
2943 }
2944
2945 // We have a class message. Turn the simple-type-specifier or
2946 // typename-specifier we parsed into a type and parse the
2947 // remainder of the class message.
Faisal Vali421b2d12017-12-29 05:41:00 +00002948 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002949 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002950 if (Type.isInvalid())
2951 return true;
2952
2953 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002954 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002955 return false;
2956}
2957
Douglas Gregor990ccac2010-05-31 14:40:22 +00002958/// \brief Determine whether the parser is currently referring to a an
2959/// Objective-C message send, using a simplified heuristic to avoid overhead.
2960///
2961/// This routine will only return true for a subset of valid message-send
2962/// expressions.
2963bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002964 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002965 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002966 return GetLookAheadToken(1).is(tok::identifier) &&
2967 GetLookAheadToken(2).is(tok::identifier);
2968}
2969
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002970bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002971 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002972 InMessageExpression)
2973 return false;
2974
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002975 ParsedType Type;
2976
2977 if (Tok.is(tok::annot_typename))
2978 Type = getTypeAnnotation(Tok);
2979 else if (Tok.is(tok::identifier))
2980 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2981 getCurScope());
2982 else
2983 return false;
2984
2985 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2986 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002987 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002988 if (Tok.is(tok::identifier))
2989 TryAnnotateTypeOrScopeToken();
2990
2991 return Tok.is(tok::annot_typename);
2992 }
2993 }
2994
2995 return false;
2996}
2997
Mike Stump11289f42009-09-09 15:08:12 +00002998/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002999/// '[' objc-receiver objc-message-args ']'
3000///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003001/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003002/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003003/// expression
3004/// class-name
3005/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003006///
John McCalldadc5752010-08-24 06:29:42 +00003007ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003008 assert(Tok.is(tok::l_square) && "'[' expected");
3009 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3010
Douglas Gregora817a192010-05-27 23:06:34 +00003011 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003012 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003013 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003014 return ExprError();
3015 }
3016
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003017 InMessageExpressionRAIIObject InMessage(*this, true);
3018
David Blaikiebbafb8a2012-03-11 07:00:24 +00003019 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003020 // We completely separate the C and C++ cases because C++ requires
3021 // more complicated (read: slower) parsing.
3022
3023 // Handle send to super.
3024 // FIXME: This doesn't benefit from the same typo-correction we
3025 // get in Objective-C.
3026 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003027 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003028 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3029 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003030
3031 // Parse the receiver, which is either a type or an expression.
3032 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003033 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003034 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003035 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003036 return ExprError();
3037 }
3038
3039 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003040 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3041 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003042
3043 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003044 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003045 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003046 }
3047
3048 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003049 IdentifierInfo *Name = Tok.getIdentifierInfo();
3050 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003051 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003052 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003053 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003054 NextToken().is(tok::period),
3055 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003056 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003057 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3058 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003059
John McCallfaf5fb42010-08-26 23:41:50 +00003060 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003061 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003062 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003063 return ExprError();
3064 }
3065
Douglas Gregore5798dc2010-04-21 20:38:13 +00003066 ConsumeToken(); // the type name
3067
Douglas Gregore83b9562015-07-07 03:57:53 +00003068 // Parse type arguments and protocol qualifiers.
3069 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003070 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003071 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003072 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3073 /*consumeLastToken=*/true,
3074 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003075 if (!NewReceiverType.isUsable()) {
3076 SkipUntil(tok::r_square, StopAtSemi);
3077 return ExprError();
3078 }
3079
3080 ReceiverType = NewReceiverType.get();
3081 }
3082
Douglas Gregore5798dc2010-04-21 20:38:13 +00003083 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003084 ReceiverType, nullptr);
3085
John McCallfaf5fb42010-08-26 23:41:50 +00003086 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003087 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003088 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003089 }
Chris Lattner8f697062008-01-25 18:59:06 +00003090 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003091
3092 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003093 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003094 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003095 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003096 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003097 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003098
David Blaikieefdccaa2016-01-15 23:43:34 +00003099 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3100 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003101}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003102
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003103/// \brief Parse the remainder of an Objective-C message following the
3104/// '[' objc-receiver.
3105///
3106/// This routine handles sends to super, class messages (sent to a
3107/// class name), and instance messages (sent to an object), and the
3108/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3109/// ReceiverExpr, respectively. Only one of these parameters may have
3110/// a valid value.
3111///
3112/// \param LBracLoc The location of the opening '['.
3113///
3114/// \param SuperLoc If this is a send to 'super', the location of the
3115/// 'super' keyword that indicates a send to the superclass.
3116///
3117/// \param ReceiverType If this is a class message, the type of the
3118/// class we are sending a message to.
3119///
3120/// \param ReceiverExpr If this is an instance message, the expression
3121/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003122///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003123/// objc-message-args:
3124/// objc-selector
3125/// objc-keywordarg-list
3126///
3127/// objc-keywordarg-list:
3128/// objc-keywordarg
3129/// objc-keywordarg-list objc-keywordarg
3130///
Mike Stump11289f42009-09-09 15:08:12 +00003131/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003132/// selector-name[opt] ':' objc-keywordexpr
3133///
3134/// objc-keywordexpr:
3135/// nonempty-expr-list
3136///
3137/// nonempty-expr-list:
3138/// assignment-expression
3139/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003140///
John McCalldadc5752010-08-24 06:29:42 +00003141ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003142Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003143 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003144 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003145 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003146 InMessageExpressionRAIIObject InMessage(*this, true);
3147
Steve Naroffeae65032009-11-07 02:08:14 +00003148 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003149 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003150 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003151 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003152 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003153 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003154 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003155 else
John McCallb268a282010-08-23 23:25:46 +00003156 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003157 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003158 cutOffParsing();
3159 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003160 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003161
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003162 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003163 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003164 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003165
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003166 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003167 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003168 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003169
Chris Lattner0ef13522007-10-09 17:51:17 +00003170 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003171 while (1) {
3172 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003173 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003174 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003175
Alp Toker383d2c42014-01-01 03:08:43 +00003176 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003177 // We must manually skip to a ']', otherwise the expression skipper will
3178 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3179 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003180 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003181 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003182 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003183
Mike Stump11289f42009-09-09 15:08:12 +00003184 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003185
3186 if (Tok.is(tok::code_completion)) {
3187 if (SuperLoc.isValid())
3188 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003189 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003190 /*AtArgumentEpression=*/true);
3191 else if (ReceiverType)
3192 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003193 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003194 /*AtArgumentEpression=*/true);
3195 else
3196 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003197 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003198 /*AtArgumentEpression=*/true);
3199
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003200 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003201 return ExprError();
3202 }
3203
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003204 ExprResult Expr;
3205 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3206 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3207 Expr = ParseBraceInitializer();
3208 } else
3209 Expr = ParseAssignmentExpression();
3210
3211 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003212 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003213 // We must manually skip to a ']', otherwise the expression skipper will
3214 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3215 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003216 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003217 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003218 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003219
Steve Naroff486760a2007-09-17 20:25:27 +00003220 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003221 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003222
Douglas Gregor1b605f72009-11-19 01:08:35 +00003223 // Code completion after each argument.
3224 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003225 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003226 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003227 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003228 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003229 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003230 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003231 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003232 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003233 else
John McCallb268a282010-08-23 23:25:46 +00003234 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003235 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003236 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003237 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003238 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003239 }
3240
Steve Naroff486760a2007-09-17 20:25:27 +00003241 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003242 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003243 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003244 break;
3245 // We have a selector or a colon, continue parsing.
3246 }
3247 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003248 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003249 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003250 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003251 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003252 if (Tok.is(tok::colon))
3253 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003254 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003255 if (Tok.is(tok::colon)) {
3256 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3257 FixItHint::CreateRemoval(commaLoc);
3258 }
Chris Lattner197a3012008-08-05 06:19:09 +00003259 // We must manually skip to a ']', otherwise the expression skipper will
3260 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3261 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003262 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003263 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003264 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003265
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003266 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003267 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003268 }
3269 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003270 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003271
Chris Lattner197a3012008-08-05 06:19:09 +00003272 // We must manually skip to a ']', otherwise the expression skipper will
3273 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3274 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003275 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003276 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003277 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003278
Chris Lattner0ef13522007-10-09 17:51:17 +00003279 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003280 Diag(Tok, diag::err_expected)
3281 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003282 // We must manually skip to a ']', otherwise the expression skipper will
3283 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3284 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003285 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003286 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003287 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003288
Chris Lattner8f697062008-01-25 18:59:06 +00003289 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003290
Steve Naroffe61bfa82007-10-05 18:42:47 +00003291 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003292 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003293 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003294 KeyLocs.push_back(Loc);
3295 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003296 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003297
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003298 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003299 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003300 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003301 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003302 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003303 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003304 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003305 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003306}
3307
John McCalldadc5752010-08-24 06:29:42 +00003308ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3309 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003310 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003311
Chris Lattnere002fbe2007-12-12 01:04:12 +00003312 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3313 // expressions. At this point, we know that the only valid thing that starts
3314 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003315 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003316 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003317 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003318 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003319
Chris Lattnere002fbe2007-12-12 01:04:12 +00003320 while (Tok.is(tok::at)) {
3321 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003322
Sebastian Redlc13f2682008-12-09 20:22:58 +00003323 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003324 if (!isTokenStringLiteral())
3325 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003326
John McCalldadc5752010-08-24 06:29:42 +00003327 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003328 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003329 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003330
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003331 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003332 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003333
Craig Topper883dd332015-12-24 23:58:11 +00003334 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003335}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003336
Ted Kremeneke65b0862012-03-06 20:05:56 +00003337/// ParseObjCBooleanLiteral -
3338/// objc-scalar-literal : '@' boolean-keyword
3339/// ;
3340/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3341/// ;
3342ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3343 bool ArgValue) {
3344 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3345 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3346}
3347
3348/// ParseObjCCharacterLiteral -
3349/// objc-scalar-literal : '@' character-literal
3350/// ;
3351ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3352 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3353 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003354 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003355 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003356 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003357 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003358}
3359
3360/// ParseObjCNumericLiteral -
3361/// objc-scalar-literal : '@' scalar-literal
3362/// ;
3363/// scalar-literal : | numeric-constant /* any numeric constant. */
3364/// ;
3365ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3366 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3367 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003368 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003369 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003370 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003371 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003372}
3373
Patrick Beard0caa3942012-04-19 00:25:12 +00003374/// ParseObjCBoxedExpr -
3375/// objc-box-expression:
3376/// @( assignment-expression )
3377ExprResult
3378Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3379 if (Tok.isNot(tok::l_paren))
3380 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3381
3382 BalancedDelimiterTracker T(*this, tok::l_paren);
3383 T.consumeOpen();
3384 ExprResult ValueExpr(ParseAssignmentExpression());
3385 if (T.consumeClose())
3386 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003387
3388 if (ValueExpr.isInvalid())
3389 return ExprError();
3390
Patrick Beard0caa3942012-04-19 00:25:12 +00003391 // Wrap the sub-expression in a parenthesized expression, to distinguish
3392 // a boxed expression from a literal.
3393 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003394 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003395 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003396 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003397}
3398
Ted Kremeneke65b0862012-03-06 20:05:56 +00003399ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003400 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003401 ConsumeBracket(); // consume the l_square.
3402
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003403 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003404 while (Tok.isNot(tok::r_square)) {
3405 // Parse list of array element expressions (all must be id types).
3406 ExprResult Res(ParseAssignmentExpression());
3407 if (Res.isInvalid()) {
3408 // We must manually skip to a ']', otherwise the expression skipper will
3409 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3410 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003411 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003412 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003413 }
3414
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003415 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3416 if (Res.isInvalid())
3417 HasInvalidEltExpr = true;
3418
Ted Kremeneke65b0862012-03-06 20:05:56 +00003419 // Parse the ellipsis that indicates a pack expansion.
3420 if (Tok.is(tok::ellipsis))
3421 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3422 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003423 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003424
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003425 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003426
3427 if (Tok.is(tok::comma))
3428 ConsumeToken(); // Eat the ','.
3429 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003430 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3431 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003432 }
3433 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003434
3435 if (HasInvalidEltExpr)
3436 return ExprError();
3437
Benjamin Kramerf0623432012-08-23 22:51:59 +00003438 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003439 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003440}
3441
3442ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3443 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3444 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003445 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003446 while (Tok.isNot(tok::r_brace)) {
3447 // Parse the comma separated key : value expressions.
3448 ExprResult KeyExpr;
3449 {
3450 ColonProtectionRAIIObject X(*this);
3451 KeyExpr = ParseAssignmentExpression();
3452 if (KeyExpr.isInvalid()) {
3453 // We must manually skip to a '}', otherwise the expression skipper will
3454 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3455 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003456 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003457 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003458 }
3459 }
3460
Alp Toker383d2c42014-01-01 03:08:43 +00003461 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003462 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003463 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003464 }
3465
3466 ExprResult ValueExpr(ParseAssignmentExpression());
3467 if (ValueExpr.isInvalid()) {
3468 // We must manually skip to a '}', otherwise the expression skipper will
3469 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3470 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003471 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003472 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003473 }
3474
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003475 // Check the key and value for possible typos
3476 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3477 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3478 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3479 HasInvalidEltExpr = true;
3480
3481 // Parse the ellipsis that designates this as a pack expansion. Do not
3482 // ActOnPackExpansion here, leave it to template instantiation time where
3483 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003484 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003485 if (getLangOpts().CPlusPlus)
3486 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3487
Ted Kremeneke65b0862012-03-06 20:05:56 +00003488 // We have a valid expression. Collect it in a vector so we can
3489 // build the argument list.
3490 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003491 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003492 };
3493 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003494
3495 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003496 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3497 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003498 }
3499 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003500
3501 if (HasInvalidEltExpr)
3502 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003503
3504 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003505 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003506 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003507}
3508
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003509/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003510/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003511ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003512Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003513 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003514
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003515 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003516
Chris Lattner197a3012008-08-05 06:19:09 +00003517 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003518 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3519
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003520 BalancedDelimiterTracker T(*this, tok::l_paren);
3521 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003522
Douglas Gregor220cac52009-02-18 17:45:20 +00003523 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003524
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003525 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003526
Douglas Gregor220cac52009-02-18 17:45:20 +00003527 if (Ty.isInvalid())
3528 return ExprError();
3529
Nico Webera7c7e602012-12-31 00:28:03 +00003530 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3531 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003532}
Anders Carlssone01493d2007-08-23 15:25:28 +00003533
3534/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003535/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003536ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003537Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003538 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003539
Chris Lattner197a3012008-08-05 06:19:09 +00003540 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003541 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3542
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003543 BalancedDelimiterTracker T(*this, tok::l_paren);
3544 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003545
Alex Lorenzf1278212017-04-11 15:01:53 +00003546 if (expectIdentifier())
3547 return ExprError();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003548
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003549 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003550 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003551
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003552 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003553
Nico Webera7c7e602012-12-31 00:28:03 +00003554 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3555 T.getOpenLocation(), ProtoIdLoc,
3556 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003557}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003558
3559/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003560/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003561ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003562 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003563
Chris Lattner197a3012008-08-05 06:19:09 +00003564 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003565 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3566
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003567 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003568 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003569
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003570 BalancedDelimiterTracker T(*this, tok::l_paren);
3571 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003572 bool HasOptionalParen = Tok.is(tok::l_paren);
3573 if (HasOptionalParen)
3574 ConsumeParen();
3575
Douglas Gregor67c692c2010-08-26 15:07:07 +00003576 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003577 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003578 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003579 return ExprError();
3580 }
3581
Chris Lattner4f472a32009-04-11 18:13:45 +00003582 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003583 if (!SelIdent && // missing selector name.
3584 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003585 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003586
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003587 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003588
Steve Naroff152dd812007-12-05 22:21:29 +00003589 unsigned nColons = 0;
3590 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003591 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003592 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003593 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003594 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003595 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3596 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003597 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003598
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003599 if (Tok.is(tok::r_paren))
3600 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003601
3602 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003603 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003604 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003605 return ExprError();
3606 }
3607
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003608 // Check for another keyword selector.
3609 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003610 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003611 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003612 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003613 break;
3614 }
Steve Naroff152dd812007-12-05 22:21:29 +00003615 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003616 if (HasOptionalParen && Tok.is(tok::r_paren))
3617 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003618 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003619 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003620 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3621 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003622 T.getCloseLocation(),
3623 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003624}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003625
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003626void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3627 // MCDecl might be null due to error in method or c-function prototype, etc.
3628 Decl *MCDecl = LM.D;
3629 bool skip = MCDecl &&
3630 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3631 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3632 if (skip)
3633 return;
3634
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003635 // Save the current token position.
3636 SourceLocation OrigLoc = Tok.getLocation();
3637
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003638 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
Alex Lorenz812012f2017-06-19 17:53:21 +00003639 // Store an artificial EOF token to ensure that we don't run off the end of
3640 // the method's body when we come to parse it.
3641 Token Eof;
3642 Eof.startToken();
3643 Eof.setKind(tok::eof);
3644 Eof.setEofData(MCDecl);
3645 Eof.setLocation(OrigLoc);
3646 LM.Toks.push_back(Eof);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003647 // Append the current token at the end of the new token stream so that it
3648 // doesn't get lost.
3649 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003650 PP.EnterTokenStream(LM.Toks, true);
3651
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003652 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003653 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003654
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003655 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3656 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003657 // Enter a scope for the method or c-function body.
Momchil Velikov57c681f2017-08-10 15:43:06 +00003658 ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
3659 Scope::FnScope | Scope::DeclScope |
3660 Scope::CompoundStmtScope);
3661
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003662 // Tell the actions module that we have entered a method or c-function definition
3663 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003664 if (parseMethod)
3665 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3666 else
3667 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003668 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003669 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003670 else {
3671 if (Tok.is(tok::colon))
3672 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003673 else
3674 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003675 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003676 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003677
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003678 if (Tok.getLocation() != OrigLoc) {
3679 // Due to parsing error, we either went over the cached tokens or
3680 // there are still cached tokens left. If it's the latter case skip the
3681 // leftover tokens.
3682 // Since this is an uncommon situation that should be avoided, use the
3683 // expensive isBeforeInTranslationUnit call.
3684 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3685 OrigLoc))
3686 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3687 ConsumeAnyToken();
3688 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003689 // Clean up the remaining EOF token.
3690 ConsumeAnyToken();
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003691}