blob: 8937a0986c9562e708e8700457166bae4c078bbd [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerda59c2f2006-11-05 02:08:13 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Objective-C portions of the Parser interface.
10//
11//===----------------------------------------------------------------------===//
12
John McCall8b0666c2010-08-20 18:27:03 +000013#include "clang/Parse/Parser.h"
Douglas Gregor813a0662015-06-19 18:14:38 +000014#include "clang/AST/ASTContext.h"
Jordan Rose1e879d82018-03-23 00:07:18 +000015#include "clang/AST/PrettyDeclStackTrace.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"
20#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000021#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000022#include "llvm/ADT/StringExtras.h"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000023
Chris Lattnerda59c2f2006-11-05 02:08:13 +000024using namespace clang;
25
Nico Weber04e213b2013-04-03 17:36:11 +000026/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000027void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000028 ParsedAttributes attrs(AttrFactory);
29 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000030 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
31 Diag(Tok, diag::err_objc_postfix_attribute_hint)
32 << (Kind == tok::objc_protocol);
33 else
34 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000035 ParseGNUAttributes(attrs);
36 }
37}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000038
Chris Lattner3a907162008-12-08 21:53:24 +000039/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000040/// external-declaration: [C99 6.9]
41/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000042/// [OBJC] objc-class-declaration
43/// [OBJC] objc-alias-declaration
44/// [OBJC] objc-protocol-definition
45/// [OBJC] objc-method-definition
46/// [OBJC] '@' 'end'
Aaron Ballman1c606c22018-02-12 13:38:25 +000047Parser::DeclGroupPtrTy
48Parser::ParseObjCAtDirectives(ParsedAttributesWithRange &Attrs) {
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);
Aaron Ballman1c606c22018-02-12 13:38:25 +000061 case tok::objc_interface:
62 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, Attrs);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000063 break;
Aaron Ballman1c606c22018-02-12 13:38:25 +000064 case tok::objc_protocol:
65 return ParseObjCAtProtocolDeclaration(AtLoc, Attrs);
Chris Lattnerce90ef52008-08-23 02:02:23 +000066 case tok::objc_implementation:
Erik Pilkingtonc5a05832019-04-11 17:55:30 +000067 return ParseObjCAtImplementationDeclaration(AtLoc, Attrs);
Chris Lattnerce90ef52008-08-23 02:02:23 +000068 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000069 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000070 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000071 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
72 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000073 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000074 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
75 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000076 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
78 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000079 case tok::objc_import:
Hamza Sood81fe14e2017-11-21 09:42:42 +000080 if (getLangOpts().Modules || getLangOpts().DebuggerSupport) {
81 SingleDecl = ParseModuleImport(AtLoc);
82 break;
83 }
Manman Rendfcf1cb2017-01-20 20:03:00 +000084 Diag(AtLoc, diag::err_atimport);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000085 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000086 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000087 default:
88 Diag(AtLoc, diag::err_unexpected_at);
89 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000090 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000091 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000092 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000093 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094}
95
Richard Smith3df3f1d2015-11-03 01:19:56 +000096/// Class to handle popping type parameters when leaving the scope.
97class Parser::ObjCTypeParamListScope {
98 Sema &Actions;
99 Scope *S;
100 ObjCTypeParamList *Params;
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000101
Richard Smith3df3f1d2015-11-03 01:19:56 +0000102public:
103 ObjCTypeParamListScope(Sema &Actions, Scope *S)
104 : Actions(Actions), S(S), Params(nullptr) {}
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000105
Richard Smith3df3f1d2015-11-03 01:19:56 +0000106 ~ObjCTypeParamListScope() {
107 leave();
108 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000109
Richard Smith3df3f1d2015-11-03 01:19:56 +0000110 void enter(ObjCTypeParamList *P) {
111 assert(!Params);
112 Params = P;
113 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000114
Richard Smith3df3f1d2015-11-03 01:19:56 +0000115 void leave() {
116 if (Params)
117 Actions.popObjCTypeParamList(S, Params);
118 Params = nullptr;
119 }
120};
121
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000122///
Mike Stump11289f42009-09-09 15:08:12 +0000123/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000124/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
125///
126/// objc-class-forward-decl:
127/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000128///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000129Parser::DeclGroupPtrTy
130Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000131 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000132 SmallVector<IdentifierInfo *, 8> ClassNames;
133 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000134 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000135
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000136 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000137 MaybeSkipAttributes(tok::objc_class);
Alex Lorenzf1278212017-04-11 15:01:53 +0000138 if (expectIdentifier()) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000139 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000140 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000141 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000143 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000144 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Douglas Gregor85f3f952015-07-07 03:57:15 +0000146 // Parse the optional objc-type-parameter-list.
147 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000148 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000149 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000150 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000151 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000152 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000153 }
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000155 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000156 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000157 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000158
Ted Kremeneka26da852009-11-17 23:12:20 +0000159 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
160 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000161 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000162 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000163}
164
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000165void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
166{
167 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
168 if (ock == Sema::OCK_None)
169 return;
170
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000171 Decl *Decl = Actions.getObjCDeclContext();
172 if (CurParsedObjCImpl) {
173 CurParsedObjCImpl->finish(AtLoc);
174 } else {
175 Actions.ActOnAtEnd(getCurScope(), AtLoc);
176 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000177 Diag(AtLoc, diag::err_objc_missing_end)
178 << FixItHint::CreateInsertion(AtLoc, "@end\n");
179 if (Decl)
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000180 Diag(Decl->getBeginLoc(), diag::note_objc_container_start) << (int)ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000181}
182
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183///
184/// objc-interface:
185/// objc-class-interface-attributes[opt] objc-class-interface
186/// objc-category-interface
187///
188/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000189/// '@' 'interface' identifier objc-type-parameter-list[opt]
190/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000191/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000192/// objc-interface-decl-list
193/// @end
194///
195/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000196/// '@' 'interface' identifier objc-type-parameter-list[opt]
197/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000198/// objc-interface-decl-list
199/// @end
200///
201/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000202/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203///
204/// objc-class-interface-attributes:
205/// __attribute__((visibility("default")))
206/// __attribute__((visibility("hidden")))
207/// __attribute__((deprecated))
208/// __attribute__((unavailable))
209/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000210/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000211///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000212Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000213 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000214 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000215 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000216 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000217 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000218
Douglas Gregor49c22a72009-11-18 16:26:39 +0000219 // Code completion after '@interface'.
220 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000221 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000222 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000223 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000224 }
225
Nico Weber69a79142013-04-04 00:15:10 +0000226 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000227
Alex Lorenzf1278212017-04-11 15:01:53 +0000228 if (expectIdentifier())
229 return nullptr; // missing class or category name.
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000230
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000232 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000233 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000234
235 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
236 // case, LAngleLoc will be valid and ProtocolIdents will capture the
237 // protocol references (that have not yet been resolved).
238 SourceLocation LAngleLoc, EndProtoLoc;
239 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
240 ObjCTypeParamList *typeParameterList = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000241 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
242 if (Tok.is(tok::less))
243 typeParameterList = parseObjCTypeParamListOrProtocolRefs(
244 typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000245
246 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000247 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Fangrui Song6907ce22018-07-30 19:24:48 +0000248
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000249 BalancedDelimiterTracker T(*this, tok::l_paren);
250 T.consumeOpen();
251
252 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000253 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000254 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000255 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000256 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000257 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000258 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000259
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000260 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000261 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000262 categoryId = Tok.getIdentifierInfo();
263 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000264 }
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000265 else if (!getLangOpts().ObjC) {
Alp Tokerec543272013-12-24 09:48:30 +0000266 Diag(Tok, diag::err_expected)
267 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000268 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000269 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000270
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000271 T.consumeClose();
272 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000273 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +0000274
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000275 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000276 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000277 SmallVector<Decl *, 8> ProtocolRefs;
278 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000279 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000280 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000281 LAngleLoc, EndProtoLoc,
282 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000283 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Alex Lorenzf9371392017-03-23 11:44:25 +0000285 Decl *CategoryType = Actions.ActOnStartCategoryInterface(
286 AtLoc, nameId, nameLoc, typeParameterList, categoryId, categoryLoc,
287 ProtocolRefs.data(), ProtocolRefs.size(), ProtocolLocs.data(),
Erich Keanec480f302018-07-12 21:09:05 +0000288 EndProtoLoc, attrs);
Alex Lorenzf9371392017-03-23 11:44:25 +0000289
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000290 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000291 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +0000292
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000293 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000294
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000295 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000296 }
297 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000298 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000299 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000300 SourceLocation typeArgsLAngleLoc;
301 SmallVector<ParsedType, 4> typeArgs;
302 SourceLocation typeArgsRAngleLoc;
303 SmallVector<Decl *, 4> protocols;
304 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000305 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000306 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000307
308 // Code completion of superclass names.
309 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000310 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000311 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000312 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000313 }
314
Alex Lorenzf1278212017-04-11 15:01:53 +0000315 if (expectIdentifier())
316 return nullptr; // missing super class name.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000317 superClassId = Tok.getIdentifierInfo();
318 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000319
320 // Type arguments for the superclass or protocol conformances.
321 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000322 parseObjCTypeArgsOrProtocolQualifiers(
323 nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
324 protocols, protocolLocs, EndProtoLoc,
325 /*consumeLastToken=*/true,
326 /*warnOnIncompleteProtocols=*/true);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000327 if (Tok.is(tok::eof))
328 return nullptr;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000329 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000330 }
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000331
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000332 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000333 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000334 if (!ProtocolIdents.empty()) {
335 // We already parsed the protocols named when we thought we had a
336 // type parameter list. Translate them into actual protocol references.
337 for (const auto &pair : ProtocolIdents) {
338 protocolLocs.push_back(pair.second);
339 }
340 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
341 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000342 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000343 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000344 } else if (protocols.empty() && Tok.is(tok::less) &&
345 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
346 LAngleLoc, EndProtoLoc,
347 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000348 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000349 }
Mike Stump11289f42009-09-09 15:08:12 +0000350
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000351 if (Tok.isNot(tok::less))
Argyrios Kyrtzidisf95a0002016-11-09 02:47:07 +0000352 Actions.ActOnTypedefedProtocols(protocols, protocolLocs,
353 superClassId, superClassLoc);
Erich Keanec480f302018-07-12 21:09:05 +0000354
355 Decl *ClsType = Actions.ActOnStartClassInterface(
356 getCurScope(), AtLoc, nameId, nameLoc, typeParameterList, superClassId,
357 superClassLoc, typeArgs,
358 SourceRange(typeArgsLAngleLoc, typeArgsRAngleLoc), protocols.data(),
359 protocols.size(), protocolLocs.data(), EndProtoLoc, attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner0ef13522007-10-09 17:51:17 +0000361 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000362 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000363
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000364 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000365
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000366 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000367}
368
Douglas Gregor813a0662015-06-19 18:14:38 +0000369/// Add an attribute for a context-sensitive type nullability to the given
370/// declarator.
371static void addContextSensitiveTypeNullability(Parser &P,
372 Declarator &D,
373 NullabilityKind nullability,
374 SourceLocation nullabilityLoc,
375 bool &addedToDeclSpec) {
376 // Create the attribute.
Erich Keanee891aa92018-07-13 15:07:47 +0000377 auto getNullabilityAttr = [&](AttributePool &Pool) -> ParsedAttr * {
Erich Keane0aab7232018-07-03 20:30:34 +0000378 return Pool.create(P.getNullabilityKeyword(nullability),
379 SourceRange(nullabilityLoc), nullptr, SourceLocation(),
Erich Keanee891aa92018-07-13 15:07:47 +0000380 nullptr, 0, ParsedAttr::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000381 };
382
383 if (D.getNumTypeObjects() > 0) {
384 // Add the attribute to the declarator chunk nearest the declarator.
Michael Krusedc5ce722018-08-03 01:21:16 +0000385 D.getTypeObject(0).getAttrs().addAtEnd(
Erich Keanec480f302018-07-12 21:09:05 +0000386 getNullabilityAttr(D.getAttributePool()));
Douglas Gregor813a0662015-06-19 18:14:38 +0000387 } else if (!addedToDeclSpec) {
388 // Otherwise, just put it on the declaration specifiers (if one
389 // isn't there already).
Michael Krusedc5ce722018-08-03 01:21:16 +0000390 D.getMutableDeclSpec().getAttributes().addAtEnd(
Erich Keanec480f302018-07-12 21:09:05 +0000391 getNullabilityAttr(D.getMutableDeclSpec().getAttributes().getPool()));
Douglas Gregor813a0662015-06-19 18:14:38 +0000392 addedToDeclSpec = true;
393 }
394}
395
Douglas Gregor85f3f952015-07-07 03:57:15 +0000396/// Parse an Objective-C type parameter list, if present, or capture
397/// the locations of the protocol identifiers for a list of protocol
398/// references.
399///
400/// objc-type-parameter-list:
401/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
402///
403/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000404/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000405///
406/// objc-type-parameter-bound:
407/// ':' type-name
408///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000409/// objc-type-parameter-variance:
410/// '__covariant'
411/// '__contravariant'
412///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000413/// \param lAngleLoc The location of the starting '<'.
414///
415/// \param protocolIdents Will capture the list of identifiers, if the
416/// angle brackets contain a list of protocol references rather than a
417/// type parameter list.
418///
419/// \param rAngleLoc The location of the ending '>'.
420ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000421 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
422 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
423 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000424 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
425
426 // Within the type parameter list, don't treat '>' as an operator.
427 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
428
429 // Local function to "flush" the protocol identifiers, turning them into
430 // type parameters.
431 SmallVector<Decl *, 4> typeParams;
432 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000433 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000435 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000436 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
437 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000438 if (typeParam.isUsable())
439 typeParams.push_back(typeParam.get());
440 }
441
442 protocolIdents.clear();
443 mayBeProtocolList = false;
444 };
445
446 bool invalid = false;
447 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000448
Douglas Gregor85f3f952015-07-07 03:57:15 +0000449 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000450 // Parse the variance, if any.
451 SourceLocation varianceLoc;
452 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
453 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
454 variance = Tok.is(tok::kw___covariant)
455 ? ObjCTypeParamVariance::Covariant
456 : ObjCTypeParamVariance::Contravariant;
457 varianceLoc = ConsumeToken();
458
459 // Once we've seen a variance specific , we know this is not a
460 // list of protocol references.
461 if (mayBeProtocolList) {
462 // Up until now, we have been queuing up parameters because they
463 // might be protocol references. Turn them into parameters now.
464 makeProtocolIdentsIntoTypeParameters();
465 }
466 }
467
Douglas Gregor85f3f952015-07-07 03:57:15 +0000468 // Parse the identifier.
469 if (!Tok.is(tok::identifier)) {
470 // Code completion.
471 if (Tok.is(tok::code_completion)) {
472 // FIXME: If these aren't protocol references, we'll need different
473 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000474 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000475 cutOffParsing();
476
477 // FIXME: Better recovery here?.
478 return nullptr;
479 }
480
481 Diag(Tok, diag::err_objc_expected_type_parameter);
482 invalid = true;
483 break;
484 }
485
486 IdentifierInfo *paramName = Tok.getIdentifierInfo();
487 SourceLocation paramLoc = ConsumeToken();
488
489 // If there is a bound, parse it.
490 SourceLocation colonLoc;
491 TypeResult boundType;
492 if (TryConsumeToken(tok::colon, colonLoc)) {
493 // Once we've seen a bound, we know this is not a list of protocol
494 // references.
495 if (mayBeProtocolList) {
496 // Up until now, we have been queuing up parameters because they
497 // might be protocol references. Turn them into parameters now.
498 makeProtocolIdentsIntoTypeParameters();
499 }
500
501 // type-name
502 boundType = ParseTypeName();
503 if (boundType.isInvalid())
504 invalid = true;
505 } else if (mayBeProtocolList) {
506 // If this could still be a protocol list, just capture the identifier.
507 // We don't want to turn it into a parameter.
508 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
509 continue;
510 }
511
512 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000513 DeclResult typeParam = Actions.actOnObjCTypeParam(
514 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
515 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000516 if (typeParam.isUsable())
517 typeParams.push_back(typeParam.get());
518 } while (TryConsumeToken(tok::comma));
519
520 // Parse the '>'.
521 if (invalid) {
522 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
523 if (Tok.is(tok::greater))
524 ConsumeToken();
525 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
526 /*ConsumeLastToken=*/true,
527 /*ObjCGenericList=*/true)) {
528 Diag(lAngleLoc, diag::note_matching) << "'<'";
529 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
530 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
531 tok::comma, tok::semi },
532 StopBeforeMatch);
533 if (Tok.is(tok::greater))
534 ConsumeToken();
535 }
536
537 if (mayBeProtocolList) {
538 // A type parameter list must be followed by either a ':' (indicating the
539 // presence of a superclass) or a '(' (indicating that this is a category
540 // or extension). This disambiguates between an objc-type-parameter-list
541 // and a objc-protocol-refs.
542 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
543 // Returning null indicates that we don't have a type parameter list.
544 // The results the caller needs to handle the protocol references are
545 // captured in the reference parameters already.
546 return nullptr;
547 }
548
549 // We have a type parameter list that looks like a list of protocol
550 // references. Turn that parameter list into type parameters.
551 makeProtocolIdentsIntoTypeParameters();
552 }
553
Richard Smith3df3f1d2015-11-03 01:19:56 +0000554 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000555 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
556 getCurScope(),
557 lAngleLoc,
558 typeParams,
559 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000560 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000561
562 // Clear out the angle locations; they're used by the caller to indicate
563 // whether there are any protocol references.
564 lAngleLoc = SourceLocation();
565 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000566 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000567}
568
569/// Parse an objc-type-parameter-list.
570ObjCTypeParamList *Parser::parseObjCTypeParamList() {
571 SourceLocation lAngleLoc;
572 SmallVector<IdentifierLocPair, 1> protocolIdents;
573 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000574
575 ObjCTypeParamListScope Scope(Actions, getCurScope());
576 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
577 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000578 /*mayBeProtocolList=*/false);
579}
580
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000581/// objc-interface-decl-list:
582/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000583/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000584/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000585/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000586/// objc-interface-decl-list declaration
587/// objc-interface-decl-list ';'
588///
Steve Naroff99264b42007-08-22 16:35:03 +0000589/// objc-method-requirement: [OBJC2]
590/// @required
591/// @optional
592///
Fangrui Song6907ce22018-07-30 19:24:48 +0000593void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000594 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000595 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000596 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000597 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000598
Ted Kremenekc7c64312010-01-07 01:20:12 +0000599 SourceRange AtEnd;
Fangrui Song6907ce22018-07-30 19:24:48 +0000600
Steve Naroff99264b42007-08-22 16:35:03 +0000601 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000602 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000603 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000604 if (Decl *methodPrototype =
605 ParseObjCMethodPrototype(MethodImplKind, false))
606 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000607 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
608 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000609 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
610 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000611 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000612 if (Tok.is(tok::semi))
613 ConsumeToken();
614 }
Steve Naroff99264b42007-08-22 16:35:03 +0000615 continue;
616 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000617 if (Tok.is(tok::l_paren)) {
618 Diag(Tok, diag::err_expected_minus_or_plus);
Fangrui Song6907ce22018-07-30 19:24:48 +0000619 ParseObjCMethodDecl(Tok.getLocation(),
620 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000621 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000622 continue;
623 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000624 // Ignore excess semicolons.
625 if (Tok.is(tok::semi)) {
Nico Weber98dd0852019-03-14 14:18:56 +0000626 // FIXME: This should use ConsumeExtraSemi() for extraneous semicolons,
627 // to make -Wextra-semi diagnose them.
Steve Naroff99264b42007-08-22 16:35:03 +0000628 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000629 continue;
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattnerda9fb152008-10-20 06:10:06 +0000632 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000633 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000634 break;
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregorf1934162010-01-13 21:24:21 +0000636 // Code completion within an Objective-C interface.
637 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000638 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000639 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000640 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000641 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000642 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000643
Chris Lattner038a3e32008-10-20 05:46:22 +0000644 // If we don't have an @ directive, parse it as a function definition.
645 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000646 // The code below does not consume '}'s because it is afraid of eating the
647 // end of a namespace. Because of the way this code is structured, an
648 // erroneous r_brace would cause an infinite loop if not handled here.
649 if (Tok.is(tok::r_brace))
650 break;
Nico Weber98dd0852019-03-14 14:18:56 +0000651
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000652 ParsedAttributesWithRange attrs(AttrFactory);
Nico Weber98dd0852019-03-14 14:18:56 +0000653
654 // Since we call ParseDeclarationOrFunctionDefinition() instead of
655 // ParseExternalDeclaration() below (so that this doesn't parse nested
656 // @interfaces), this needs to duplicate some code from the latter.
657 if (Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
658 SourceLocation DeclEnd;
659 allTUVariables.push_back(
660 ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs));
661 continue;
662 }
663
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;
Fangrui Song6907ce22018-07-30 19:24:48 +0000700
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000701 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");
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000705 Diag(CDecl->getBeginLoc(), diag::note_objc_container_start)
706 << (int)Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000707 ConsumeToken();
708 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000709
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:
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000721 if (!getLangOpts().ObjC)
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");
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000791 Diag(CDecl->getBeginLoc(), diag::note_objc_container_start)
792 << (int)Actions.getObjCContainerKind();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000793 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 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001036
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.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001119 return (getLangOpts().ObjC && 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)) {
Fangrui Song6907ce22018-07-30 19:24:48 +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 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001153
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
Fangrui Song6907ce22018-07-30 19:24:48 +00001175 case objc_nonnull:
Douglas Gregor813a0662015-06-19 18:14:38 +00001176 Qual = ObjCDeclSpec::DQ_CSNullability;
1177 Nullability = NullabilityKind::NonNull;
1178 break;
1179
Fangrui Song6907ce22018-07-30 19:24:48 +00001180 case objc_nullable:
Douglas Gregor813a0662015-06-19 18:14:38 +00001181 Qual = ObjCDeclSpec::DQ_CSNullability;
1182 Nullability = NullabilityKind::Nullable;
1183 break;
1184
Fangrui Song6907ce22018-07-30 19:24:48 +00001185 case objc_null_unspecified:
Douglas Gregor813a0662015-06-19 18:14:38 +00001186 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.
Erich Keanec480f302018-07-12 21:09:05 +00001208static void takeDeclAttributes(ParsedAttributesView &attrs,
1209 ParsedAttributesView &from) {
1210 for (auto &AL : llvm::reverse(from)) {
1211 if (!AL.isUsedAsTypeAttr()) {
1212 from.remove(&AL);
Michael Krusedc5ce722018-08-03 01:21:16 +00001213 attrs.addAtEnd(&AL);
John McCalla55902b2011-10-01 09:56:14 +00001214 }
1215 }
1216}
1217
1218/// takeDeclAttributes - Take all the decl attributes from the given
1219/// declarator and add them to the given list.
1220static void takeDeclAttributes(ParsedAttributes &attrs,
1221 Declarator &D) {
1222 // First, take ownership of all attributes.
1223 attrs.getPool().takeAllFrom(D.getAttributePool());
1224 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1225
1226 // Now actually move the attributes over.
Erich Keanec480f302018-07-12 21:09:05 +00001227 takeDeclAttributes(attrs, D.getMutableDeclSpec().getAttributes());
John McCalla55902b2011-10-01 09:56:14 +00001228 takeDeclAttributes(attrs, D.getAttributes());
1229 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00001230 takeDeclAttributes(attrs, D.getTypeObject(i).getAttrs());
John McCalla55902b2011-10-01 09:56:14 +00001231}
1232
Chris Lattner61511e12007-12-12 06:56:32 +00001233/// objc-type-name:
1234/// '(' objc-type-qualifiers[opt] type-name ')'
1235/// '(' objc-type-qualifiers[opt] ')'
1236///
Fangrui Song6907ce22018-07-30 19:24:48 +00001237ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001238 DeclaratorContext context,
John McCalla55902b2011-10-01 09:56:14 +00001239 ParsedAttributes *paramAttrs) {
Faisal Vali421b2d12017-12-29 05:41:00 +00001240 assert(context == DeclaratorContext::ObjCParameterContext ||
1241 context == DeclaratorContext::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001242 assert((paramAttrs != nullptr) ==
Faisal Vali421b2d12017-12-29 05:41:00 +00001243 (context == DeclaratorContext::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001244
Chris Lattner0ef13522007-10-09 17:51:17 +00001245 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001247 BalancedDelimiterTracker T(*this, tok::l_paren);
1248 T.consumeOpen();
1249
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001250 ObjCDeclContextSwitch ObjCDC(*this);
1251
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001252 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001253 ParseObjCTypeQualifierList(DS, context);
Erik Pilkingtond7999cb2019-06-26 23:39:23 +00001254 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff7e901fd2007-08-22 23:18:22 +00001255
John McCallba7bf592010-08-24 05:47:05 +00001256 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001257 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001258 // Parse an abstract declarator.
1259 DeclSpec declSpec(AttrFactory);
1260 declSpec.setObjCQualifiers(&DS);
Faisal Vali7db85c52017-12-31 00:06:40 +00001261 DeclSpecContext dsContext = DeclSpecContext::DSC_normal;
Faisal Vali421b2d12017-12-29 05:41:00 +00001262 if (context == DeclaratorContext::ObjCResultContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00001263 dsContext = DeclSpecContext::DSC_objc_method_result;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001264 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001265 Declarator declarator(declSpec, context);
1266 ParseDeclarator(declarator);
1267
1268 // If that's not invalid, extract a type.
1269 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001270 // Map a nullability specifier to a context-sensitive keyword attribute.
1271 bool addedToDeclSpec = false;
1272 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1273 addContextSensitiveTypeNullability(*this, declarator,
1274 DS.getNullability(),
1275 DS.getNullabilityLoc(),
1276 addedToDeclSpec);
1277
John McCalla55902b2011-10-01 09:56:14 +00001278 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1279 if (!type.isInvalid())
1280 Ty = type.get();
1281
1282 // If we're parsing a parameter, steal all the decl attributes
1283 // and add them to the decl spec.
Faisal Vali421b2d12017-12-29 05:41:00 +00001284 if (context == DeclaratorContext::ObjCParameterContext)
John McCalla55902b2011-10-01 09:56:14 +00001285 takeDeclAttributes(*paramAttrs, declarator);
1286 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001287 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001288
Steve Naroff90255b42008-10-21 14:15:04 +00001289 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001290 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001291 else if (Tok.getLocation() == TypeStartLoc) {
1292 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001293 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001294 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001295 } else {
1296 // Otherwise, we found *something*, but didn't get a ')' in the right
1297 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001298 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001299 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001300 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001301}
1302
1303/// objc-method-decl:
1304/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001305/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001306/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001307/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001308///
1309/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001310/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001311/// objc-keyword-selector objc-keyword-decl
1312///
1313/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001314/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1315/// objc-selector ':' objc-keyword-attributes[opt] identifier
1316/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1317/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001318///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001319/// objc-parmlist:
1320/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001321///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001322/// objc-parms:
1323/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001324///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001325/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001326/// , ...
1327///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001328/// objc-keyword-attributes: [OBJC2]
1329/// __attribute__((unused))
1330///
John McCall48871652010-08-21 09:40:31 +00001331Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001332 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001333 tok::ObjCKeywordKind MethodImplKind,
1334 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001335 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001336
Douglas Gregor636a61e2010-04-07 00:21:17 +00001337 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001338 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1339 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001340 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001341 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001342 }
1343
Chris Lattner2ebb1782008-08-23 01:48:03 +00001344 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001345 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001346 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001347 if (Tok.is(tok::l_paren))
Faisal Vali421b2d12017-12-29 05:41:00 +00001348 ReturnType = ParseObjCTypeName(DSRet, DeclaratorContext::ObjCResultContext,
Craig Topper161e4db2014-05-21 06:02:52 +00001349 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001350
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001351 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001352 ParsedAttributes methodAttrs(AttrFactory);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001353 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001354 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001355 MaybeParseCXX11Attributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001356
Douglas Gregor636a61e2010-04-07 00:21:17 +00001357 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001358 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001359 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001360 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001361 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001362 }
1363
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001364 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001365 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001366 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001367
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001368 // An unnamed colon is valid.
1369 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001370 Diag(Tok, diag::err_expected_selector_for_method)
1371 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001372 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001373 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001374 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001375 }
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001377 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001378 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001379 // If attributes exist after the method, parse them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001380 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001381 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001382 MaybeParseCXX11Attributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner5700fab2007-10-07 02:00:24 +00001384 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Erich Keanec480f302018-07-12 21:09:05 +00001385 Decl *Result = Actions.ActOnMethodDeclaration(
1386 getCurScope(), mLoc, Tok.getLocation(), mType, DSRet, ReturnType,
1387 selLoc, Sel, nullptr, CParamInfo.data(), CParamInfo.size(), methodAttrs,
1388 MethodImplKind, false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001389 PD.complete(Result);
1390 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001391 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001392
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001393 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001394 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001395 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001396 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1397 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001398
1399 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001400 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001401 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001402 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001403
Chris Lattner5700fab2007-10-07 02:00:24 +00001404 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001405 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001406 break;
Mike Stump11289f42009-09-09 15:08:12 +00001407
David Blaikieefdccaa2016-01-15 23:43:34 +00001408 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001409 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001410 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
Faisal Vali421b2d12017-12-29 05:41:00 +00001411 DeclaratorContext::ObjCParameterContext,
John McCalla55902b2011-10-01 09:56:14 +00001412 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001413
Chris Lattner5700fab2007-10-07 02:00:24 +00001414 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001415 // Regardless, collect all the attributes we've parsed so far.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001416 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001417 MaybeParseGNUAttributes(paramAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001418 MaybeParseCXX11Attributes(paramAttrs);
Erich Keanec480f302018-07-12 21:09:05 +00001419 ArgInfo.ArgAttrs = paramAttrs;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001420
Douglas Gregor45879692010-07-08 23:37:41 +00001421 // Code completion for the next piece of the selector.
1422 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001423 KeyIdents.push_back(SelIdent);
Fangrui Song6907ce22018-07-30 19:24:48 +00001424 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
Douglas Gregor45879692010-07-08 23:37:41 +00001425 mType == tok::minus,
1426 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001427 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001428 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001429 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001430 }
Alex Lorenzf1278212017-04-11 15:01:53 +00001431
1432 if (expectIdentifier())
1433 break; // missing argument name.
Mike Stump11289f42009-09-09 15:08:12 +00001434
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001435 ArgInfo.Name = Tok.getIdentifierInfo();
1436 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001437 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001438
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001439 ArgInfos.push_back(ArgInfo);
1440 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001441 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001442
John McCall084e83d2011-03-24 11:26:52 +00001443 // Make sure the attributes persist.
1444 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1445
Douglas Gregor95887f92010-07-08 23:20:03 +00001446 // Code completion for the next piece of the selector.
1447 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001448 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
Douglas Gregor95887f92010-07-08 23:20:03 +00001449 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001450 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001451 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001452 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001453 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001454 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001455
Chris Lattner5700fab2007-10-07 02:00:24 +00001456 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001457 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001458 if (!SelIdent && Tok.isNot(tok::colon))
1459 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001460 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001461 SourceLocation ColonLoc = Tok.getLocation();
1462 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001463 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1464 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1465 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001466 }
1467 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001468 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001469 }
Mike Stump11289f42009-09-09 15:08:12 +00001470
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001471 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001472 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001473 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001474 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001475 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001476 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001477 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001478 ConsumeToken();
1479 break;
1480 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001481 if (!cStyleParamWarned) {
1482 Diag(Tok, diag::warn_cstyle_param);
1483 cStyleParamWarned = true;
1484 }
John McCall084e83d2011-03-24 11:26:52 +00001485 DeclSpec DS(AttrFactory);
Faisal Valia534f072018-04-26 00:42:40 +00001486 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001487 // Parse the declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001488 Declarator ParmDecl(DS, DeclaratorContext::PrototypeContext);
Chris Lattner5700fab2007-10-07 02:00:24 +00001489 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001490 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001491 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001492 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Fangrui Song6907ce22018-07-30 19:24:48 +00001493 ParmDecl.getIdentifierLoc(),
Fariborz Jahanian60462092010-04-08 00:30:06 +00001494 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001495 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001496 }
Mike Stump11289f42009-09-09 15:08:12 +00001497
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001498 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001499 // If attributes exist after the method, parse them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001500 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001501 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001502 MaybeParseCXX11Attributes(methodAttrs);
1503
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001504 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001505 return nullptr;
1506
Chris Lattner5700fab2007-10-07 02:00:24 +00001507 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1508 &KeyIdents[0]);
Erich Keanec480f302018-07-12 21:09:05 +00001509 Decl *Result = Actions.ActOnMethodDeclaration(
1510 getCurScope(), mLoc, Tok.getLocation(), mType, DSRet, ReturnType, KeyLocs,
1511 Sel, &ArgInfos[0], CParamInfo.data(), CParamInfo.size(), methodAttrs,
1512 MethodImplKind, isVariadic, MethodDefinition);
1513
John McCall28a6aea2009-11-04 02:18:39 +00001514 PD.complete(Result);
1515 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001516}
1517
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001518/// objc-protocol-refs:
1519/// '<' identifier-list '>'
1520///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001521bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001522ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1523 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001524 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001525 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1526 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001527 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001528
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001529 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001530
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001531 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001532
Chris Lattner3bbae002008-07-26 04:03:38 +00001533 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001534 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001535 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001536 cutOffParsing();
1537 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001538 }
1539
Alex Lorenzf1278212017-04-11 15:01:53 +00001540 if (expectIdentifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001541 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001542 return true;
1543 }
1544 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1545 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001546 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001547 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001548
Alp Toker383d2c42014-01-01 03:08:43 +00001549 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001550 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001551 }
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattner3bbae002008-07-26 04:03:38 +00001553 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001554 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001555 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001556 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001559 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001560 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001561 return false;
1562}
1563
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001564TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001565 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001566 assert(getLangOpts().ObjC && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001567
1568 SourceLocation lAngleLoc;
1569 SmallVector<Decl *, 8> protocols;
1570 SmallVector<SourceLocation, 8> protocolLocs;
1571 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1572 lAngleLoc, rAngleLoc,
1573 /*consumeLastToken=*/true);
1574 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1575 protocols,
1576 protocolLocs,
1577 rAngleLoc);
1578 if (result.isUsable()) {
1579 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1580 << FixItHint::CreateInsertion(lAngleLoc, "id")
1581 << SourceRange(lAngleLoc, rAngleLoc);
1582 }
1583
1584 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001585}
1586
Douglas Gregore9d95f12015-07-07 03:57:35 +00001587/// Parse Objective-C type arguments or protocol qualifiers.
1588///
1589/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001590/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001591///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001592void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001593 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001594 SourceLocation &typeArgsLAngleLoc,
1595 SmallVectorImpl<ParsedType> &typeArgs,
1596 SourceLocation &typeArgsRAngleLoc,
1597 SourceLocation &protocolLAngleLoc,
1598 SmallVectorImpl<Decl *> &protocols,
1599 SmallVectorImpl<SourceLocation> &protocolLocs,
1600 SourceLocation &protocolRAngleLoc,
1601 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001602 bool warnOnIncompleteProtocols) {
1603 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1604 SourceLocation lAngleLoc = ConsumeToken();
1605
1606 // Whether all of the elements we've parsed thus far are single
1607 // identifiers, which might be types or might be protocols.
1608 bool allSingleIdentifiers = true;
1609 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001610 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001611
1612 // Parse a list of comma-separated identifiers, bailing out if we
1613 // see something different.
1614 do {
1615 // Parse a single identifier.
1616 if (Tok.is(tok::identifier) &&
1617 (NextToken().is(tok::comma) ||
1618 NextToken().is(tok::greater) ||
1619 NextToken().is(tok::greatergreater))) {
1620 identifiers.push_back(Tok.getIdentifierInfo());
1621 identifierLocs.push_back(ConsumeToken());
1622 continue;
1623 }
1624
1625 if (Tok.is(tok::code_completion)) {
1626 // FIXME: Also include types here.
1627 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1628 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001629 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
Douglas Gregore9d95f12015-07-07 03:57:35 +00001630 identifierLocs[i]));
1631 }
1632
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001633 QualType BaseT = Actions.GetTypeFromParser(baseType);
1634 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1635 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1636 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001637 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001638 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001639 cutOffParsing();
1640 return;
1641 }
1642
1643 allSingleIdentifiers = false;
1644 break;
1645 } while (TryConsumeToken(tok::comma));
1646
1647 // If we parsed an identifier list, semantic analysis sorts out
1648 // whether it refers to protocols or to type arguments.
1649 if (allSingleIdentifiers) {
1650 // Parse the closing '>'.
1651 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001652 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001653 /*ObjCGenericList=*/true);
1654
1655 // Let Sema figure out what we parsed.
1656 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001657 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001658 lAngleLoc,
1659 identifiers,
1660 identifierLocs,
1661 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001662 typeArgsLAngleLoc,
1663 typeArgs,
1664 typeArgsRAngleLoc,
1665 protocolLAngleLoc,
1666 protocols,
1667 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001668 warnOnIncompleteProtocols);
1669 return;
1670 }
1671
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001672 // We parsed an identifier list but stumbled into non single identifiers, this
1673 // means we might (a) check that what we already parsed is a legitimate type
1674 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1675 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001676
1677 // Convert the identifiers into type arguments.
1678 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001679 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1680 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1681 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1682 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1683
Douglas Gregore9d95f12015-07-07 03:57:35 +00001684 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1685 ParsedType typeArg
1686 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1687 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001688 DeclSpec DS(AttrFactory);
1689 const char *prevSpec = nullptr;
1690 unsigned diagID;
Faisal Vali090da2d2018-01-01 18:23:28 +00001691 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1692 typeArg, Actions.getASTContext().getPrintingPolicy());
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001693
1694 // Form a declarator to turn this into a type.
Faisal Vali421b2d12017-12-29 05:41:00 +00001695 Declarator D(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001696 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001697 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001698 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001699 if (!foundValidTypeId) {
1700 foundValidTypeId = identifiers[i];
1701 foundValidTypeSrcLoc = identifierLocs[i];
1702 }
1703 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001704 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001705 unknownTypeArgs.push_back(identifiers[i]);
1706 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1707 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001708 } else {
1709 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001710 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1711 unknownTypeArgs.push_back(identifiers[i]);
1712 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1713 } else if (!foundProtocolId) {
1714 foundProtocolId = identifiers[i];
1715 foundProtocolSrcLoc = identifierLocs[i];
1716 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001717 }
1718 }
1719
1720 // Continue parsing type-names.
1721 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001722 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001723 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001724
1725 // Consume the '...' for a pack expansion.
1726 SourceLocation ellipsisLoc;
1727 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1728 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1729 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1730 }
1731
Douglas Gregore9d95f12015-07-07 03:57:35 +00001732 if (typeArg.isUsable()) {
1733 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001734 if (!foundValidTypeId) {
1735 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1736 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1737 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001738 } else {
1739 invalid = true;
1740 }
1741 } while (TryConsumeToken(tok::comma));
1742
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001743 // Diagnose the mix between type args and protocols.
1744 if (foundProtocolId && foundValidTypeId)
1745 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1746 foundValidTypeId,
1747 foundValidTypeSrcLoc);
1748
1749 // Diagnose unknown arg types.
1750 ParsedType T;
1751 if (unknownTypeArgs.size())
1752 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1753 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1754 getCurScope(), nullptr, T);
1755
Douglas Gregore9d95f12015-07-07 03:57:35 +00001756 // Parse the closing '>'.
1757 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001758 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001759 /*ObjCGenericList=*/true);
1760
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001761 if (invalid) {
1762 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001763 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001764 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001765
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001766 // Record left/right angle locations.
1767 typeArgsLAngleLoc = lAngleLoc;
1768 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001769}
1770
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001771void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001772 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001773 SourceLocation &typeArgsLAngleLoc,
1774 SmallVectorImpl<ParsedType> &typeArgs,
1775 SourceLocation &typeArgsRAngleLoc,
1776 SourceLocation &protocolLAngleLoc,
1777 SmallVectorImpl<Decl *> &protocols,
1778 SmallVectorImpl<SourceLocation> &protocolLocs,
1779 SourceLocation &protocolRAngleLoc,
1780 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001781 assert(Tok.is(tok::less));
1782
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001783 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001784 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1785 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001786 typeArgs,
1787 typeArgsRAngleLoc,
1788 protocolLAngleLoc,
1789 protocols,
1790 protocolLocs,
1791 protocolRAngleLoc,
1792 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001793 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001794 if (Tok.is(tok::eof)) // Nothing else to do here...
1795 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001796
1797 // An Objective-C object pointer followed by type arguments
1798 // can then be followed again by a set of protocol references, e.g.,
1799 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001800 if ((consumeLastToken && Tok.is(tok::less)) ||
1801 (!consumeLastToken && NextToken().is(tok::less))) {
1802 // If we aren't consuming the last token, the prior '>' is still hanging
1803 // there. Consume it before we parse the protocol qualifiers.
1804 if (!consumeLastToken)
1805 ConsumeToken();
1806
1807 if (!protocols.empty()) {
1808 SkipUntilFlags skipFlags = SkipUntilFlags();
1809 if (!consumeLastToken)
1810 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001811 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001812 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1813 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001814 } else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001815 ParseObjCProtocolReferences(protocols, protocolLocs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001816 /*WarnOnDeclarations=*/false,
1817 /*ForObjCContainer=*/false,
Fangrui Song6907ce22018-07-30 19:24:48 +00001818 protocolLAngleLoc, protocolRAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001819 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001820 }
1821 }
1822}
1823
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001824TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1825 SourceLocation loc,
1826 ParsedType type,
1827 bool consumeLastToken,
1828 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001829 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001830 SourceLocation typeArgsLAngleLoc;
1831 SmallVector<ParsedType, 4> typeArgs;
1832 SourceLocation typeArgsRAngleLoc;
1833 SourceLocation protocolLAngleLoc;
1834 SmallVector<Decl *, 4> protocols;
1835 SmallVector<SourceLocation, 4> protocolLocs;
1836 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001837
1838 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001839 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001840 typeArgsRAngleLoc, protocolLAngleLoc,
1841 protocols, protocolLocs,
1842 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001843
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001844 if (Tok.is(tok::eof))
1845 return true; // Invalid type result.
1846
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001847 // Compute the location of the last token.
1848 if (consumeLastToken)
1849 endLoc = PrevTokLocation;
1850 else
1851 endLoc = Tok.getLocation();
1852
1853 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1854 getCurScope(),
1855 loc,
1856 type,
1857 typeArgsLAngleLoc,
1858 typeArgs,
1859 typeArgsRAngleLoc,
1860 protocolLAngleLoc,
1861 protocols,
1862 protocolLocs,
1863 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001864}
1865
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001866void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1867 BalancedDelimiterTracker &T,
1868 SmallVectorImpl<Decl *> &AllIvarDecls,
1869 bool RBraceMissing) {
1870 if (!RBraceMissing)
1871 T.consumeClose();
Fangrui Song6907ce22018-07-30 19:24:48 +00001872
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001873 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1874 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1875 Actions.ActOnObjCContainerFinishDefinition();
1876 // Call ActOnFields() even if we don't have any decls. This is useful
1877 // for code rewriting tools that need to be aware of the empty list.
Erich Keanec480f302018-07-12 21:09:05 +00001878 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl, AllIvarDecls,
1879 T.getOpenLocation(), T.getCloseLocation(),
1880 ParsedAttributesView());
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001881}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001882
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001883/// objc-class-instance-variables:
1884/// '{' objc-instance-variable-decl-list[opt] '}'
1885///
1886/// objc-instance-variable-decl-list:
1887/// objc-visibility-spec
1888/// objc-instance-variable-decl ';'
1889/// ';'
1890/// objc-instance-variable-decl-list objc-visibility-spec
1891/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
Nico Weber98dd0852019-03-14 14:18:56 +00001892/// objc-instance-variable-decl-list static_assert-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001893/// objc-instance-variable-decl-list ';'
1894///
1895/// objc-visibility-spec:
1896/// @private
1897/// @protected
1898/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001899/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001900///
1901/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001902/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001903///
John McCall48871652010-08-21 09:40:31 +00001904void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001905 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001906 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001907 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001908 SmallVector<Decl *, 32> AllIvarDecls;
Fangrui Song6907ce22018-07-30 19:24:48 +00001909
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001910 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001911 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001912
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001913 BalancedDelimiterTracker T(*this, tok::l_brace);
1914 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001915 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001916 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001917 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001918
Steve Naroff00433d32007-08-21 21:17:12 +00001919 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001920 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001921 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001922 continue;
1923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Steve Naroff00433d32007-08-21 21:17:12 +00001925 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001926 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001927 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001928 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001929 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001930 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001931
Steve Naroff7c348172007-08-23 18:16:40 +00001932 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001933 case tok::objc_private:
1934 case tok::objc_public:
1935 case tok::objc_protected:
1936 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001937 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001938 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001939 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001940
1941 case tok::objc_end:
1942 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001943 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1944 Tok.setKind(tok::at);
1945 Tok.setLength(1);
Ilya Biryukov929af672019-05-17 09:32:05 +00001946 PP.EnterToken(Tok, /*IsReinject*/true);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001947 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1948 T, AllIvarDecls, true);
1949 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001950
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001951 default:
1952 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1953 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001954 }
1955 }
Mike Stump11289f42009-09-09 15:08:12 +00001956
Douglas Gregor48d46252010-01-13 21:54:15 +00001957 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001958 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001959 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001960 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001961 }
John McCallcfefb6d2009-11-03 02:38:08 +00001962
Nico Weber98dd0852019-03-14 14:18:56 +00001963 // This needs to duplicate a small amount of code from
1964 // ParseStructUnionBody() for things that should work in both
1965 // C struct and in Objective-C class instance variables.
1966 if (Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
1967 SourceLocation DeclEnd;
1968 ParseStaticAssertDeclaration(DeclEnd);
1969 continue;
1970 }
1971
Benjamin Kramera39beb92014-09-03 11:06:10 +00001972 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1973 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1974 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001975 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001976 Decl *Field = Actions.ActOnIvar(
1977 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1978 FD.BitfieldSize, visibility);
1979 Actions.ActOnObjCContainerFinishDefinition();
1980 if (Field)
1981 AllIvarDecls.push_back(Field);
1982 FD.complete(Field);
1983 };
John McCallcfefb6d2009-11-03 02:38:08 +00001984
Chris Lattnera12405b2008-04-10 06:46:29 +00001985 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001986 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001987 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Chris Lattner0ef13522007-10-09 17:51:17 +00001989 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001990 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001991 } else {
1992 Diag(Tok, diag::err_expected_semi_decl_list);
1993 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001994 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001995 }
1996 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001997 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1998 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001999}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002000
2001/// objc-protocol-declaration:
2002/// objc-protocol-definition
2003/// objc-protocol-forward-reference
2004///
2005/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002006/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002007/// objc-protocol-refs[opt]
2008/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002009/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002010///
2011/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002012/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002013///
James Dennett1355bd12012-06-11 06:19:40 +00002014/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002015/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002016/// semicolon in the first alternative if objc-protocol-refs are omitted.
Fangrui Song6907ce22018-07-30 19:24:48 +00002017Parser::DeclGroupPtrTy
Douglas Gregorf6102672012-01-01 21:23:57 +00002018Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2019 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002020 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002021 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2022 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002023
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002024 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002025 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002026 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002027 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002028 }
2029
Nico Weber69a79142013-04-04 00:15:10 +00002030 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002031
Alex Lorenzf1278212017-04-11 15:01:53 +00002032 if (expectIdentifier())
2033 return nullptr; // missing protocol name.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002034 // Save the protocol name, then consume it.
2035 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2036 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002037
Alp Toker383d2c42014-01-01 03:08:43 +00002038 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002039 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Erich Keanec480f302018-07-12 21:09:05 +00002040 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo, attrs);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002041 }
Mike Stump11289f42009-09-09 15:08:12 +00002042
Erik Verbruggenf9887852011-12-08 09:58:43 +00002043 CheckNestedObjCContexts(AtLoc);
2044
Chris Lattner0ef13522007-10-09 17:51:17 +00002045 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002046 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002047 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2048
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002049 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002050 while (1) {
2051 ConsumeToken(); // the ','
Alex Lorenzf1278212017-04-11 15:01:53 +00002052 if (expectIdentifier()) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002053 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002054 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002055 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002056 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2057 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002058 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002059
Chris Lattner0ef13522007-10-09 17:51:17 +00002060 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002061 break;
2062 }
2063 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002064 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002065 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002066
Erich Keanec480f302018-07-12 21:09:05 +00002067 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs, attrs);
Chris Lattnerd7352d62008-07-21 22:17:28 +00002068 }
Mike Stump11289f42009-09-09 15:08:12 +00002069
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002070 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002071 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002072
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002073 SmallVector<Decl *, 8> ProtocolRefs;
2074 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002075 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002076 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002077 LAngleLoc, EndProtoLoc,
2078 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002079 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002080
Erich Keanec480f302018-07-12 21:09:05 +00002081 Decl *ProtoType = Actions.ActOnStartProtocolInterface(
2082 AtLoc, protocolName, nameLoc, ProtocolRefs.data(), ProtocolRefs.size(),
2083 ProtocolLocs.data(), EndProtoLoc, attrs);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002084
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002085 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002086 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002087}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002088
2089/// objc-implementation:
2090/// objc-class-implementation-prologue
2091/// objc-category-implementation-prologue
2092///
2093/// objc-class-implementation-prologue:
2094/// @implementation identifier objc-superclass[opt]
2095/// objc-class-instance-variables[opt]
2096///
2097/// objc-category-implementation-prologue:
2098/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002099Parser::DeclGroupPtrTy
Erik Pilkingtonc5a05832019-04-11 17:55:30 +00002100Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc,
2101 ParsedAttributes &Attrs) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002102 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2103 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002104 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002105 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002106
Douglas Gregor49c22a72009-11-18 16:26:39 +00002107 // Code completion after '@implementation'.
2108 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002109 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002110 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002111 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002112 }
2113
Nico Weber69a79142013-04-04 00:15:10 +00002114 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002115
Alex Lorenzf1278212017-04-11 15:01:53 +00002116 if (expectIdentifier())
2117 return nullptr; // missing class or category name.
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002118 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002119 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002120 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002121 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002122
Douglas Gregor85f3f952015-07-07 03:57:15 +00002123 // Neither a type parameter list nor a list of protocol references is
2124 // permitted here. Parse and diagnose them.
2125 if (Tok.is(tok::less)) {
2126 SourceLocation lAngleLoc, rAngleLoc;
2127 SmallVector<IdentifierLocPair, 8> protocolIdents;
2128 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002129 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2130 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2131 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002132 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2133 << SourceRange(diagLoc, PrevTokLocation);
2134 } else if (lAngleLoc.isValid()) {
2135 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2136 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2137 }
2138 }
2139
Mike Stump11289f42009-09-09 15:08:12 +00002140 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002141 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002142 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002143 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002144 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002145
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002146 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002147 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002148 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002149 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002150 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002151
Chris Lattner0ef13522007-10-09 17:51:17 +00002152 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002153 categoryId = Tok.getIdentifierInfo();
2154 categoryLoc = ConsumeToken();
2155 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002156 Diag(Tok, diag::err_expected)
2157 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002158 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002159 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002160 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002161 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002162 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002163 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002164 }
2165 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002166 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2167 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002168 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2169 SmallVector<Decl *, 4> protocols;
2170 SmallVector<SourceLocation, 4> protocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00002171 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002172 /*warnOnIncompleteProtocols=*/false,
2173 /*ForObjCContainer=*/false,
2174 protocolLAngleLoc, protocolRAngleLoc,
2175 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002176 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002177 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Pilkingtonc5a05832019-04-11 17:55:30 +00002178 AtLoc, nameId, nameLoc, categoryId, categoryLoc, Attrs);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002179
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002180 } else {
2181 // We have a class implementation
2182 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002183 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002184 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002185 // We have a super class
Alex Lorenzf1278212017-04-11 15:01:53 +00002186 if (expectIdentifier())
2187 return nullptr; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002188 superClassId = Tok.getIdentifierInfo();
2189 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002190 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002191 ObjCImpDecl = Actions.ActOnStartClassImplementation(
Erik Pilkingtonc5a05832019-04-11 17:55:30 +00002192 AtLoc, nameId, nameLoc, superClassId, superClassLoc, Attrs);
Fangrui Song6907ce22018-07-30 19:24:48 +00002193
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002194 if (Tok.is(tok::l_brace)) // we have ivars
2195 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002196 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2197 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002198
2199 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2200 SmallVector<Decl *, 4> protocols;
2201 SmallVector<SourceLocation, 4> protocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00002202 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002203 /*warnOnIncompleteProtocols=*/false,
2204 /*ForObjCContainer=*/false,
2205 protocolLAngleLoc, protocolRAngleLoc,
2206 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002207 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002208 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002209 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002210
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002211 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002212
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002213 {
2214 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002215 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002216 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002217 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002218 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2219 DeclGroupRef DG = DGP.get();
2220 DeclsInGroup.append(DG.begin(), DG.end());
2221 }
2222 }
2223 }
2224
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002225 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002226}
Steve Naroff33a1e802007-10-29 21:38:07 +00002227
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002228Parser::DeclGroupPtrTy
2229Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002230 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2231 "ParseObjCAtEndDeclaration(): Expected @end");
2232 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002233 if (CurParsedObjCImpl)
2234 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002235 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002236 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002237 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002238 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002239}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002240
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002241Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2242 if (!Finished) {
2243 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002244 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002245 P.Diag(P.Tok, diag::err_objc_missing_end)
2246 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002247 P.Diag(Dcl->getBeginLoc(), diag::note_objc_container_start)
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002248 << Sema::OCK_Implementation;
2249 }
2250 }
Craig Topper161e4db2014-05-21 06:02:52 +00002251 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002252 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002253}
2254
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002255void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2256 assert(!Finished);
Alex Lorenz6c9af502017-07-03 10:12:24 +00002257 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl, AtEnd.getBegin());
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002258 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fangrui Song6907ce22018-07-30 19:24:48 +00002259 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002260 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002261
2262 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2263
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002264 if (HasCFunction)
2265 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fangrui Song6907ce22018-07-30 19:24:48 +00002266 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002267 false/*c-functions*/);
Fangrui Song6907ce22018-07-30 19:24:48 +00002268
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002269 /// Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002270 for (LateParsedObjCMethodContainer::iterator
2271 I = LateParsedObjCMethods.begin(),
2272 E = LateParsedObjCMethods.end(); I != E; ++I)
2273 delete *I;
2274 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002275
2276 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002277}
2278
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002279/// compatibility-alias-decl:
2280/// @compatibility_alias alias-name class-name ';'
2281///
John McCall48871652010-08-21 09:40:31 +00002282Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002283 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2284 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2285 ConsumeToken(); // consume compatibility_alias
Alex Lorenzf1278212017-04-11 15:01:53 +00002286 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002287 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002288 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2289 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Alex Lorenzf1278212017-04-11 15:01:53 +00002290 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002291 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002292 IdentifierInfo *classId = Tok.getIdentifierInfo();
2293 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002294 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002295 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2296 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002297}
2298
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002299/// property-synthesis:
2300/// @synthesize property-ivar-list ';'
2301///
2302/// property-ivar-list:
2303/// property-ivar
2304/// property-ivar-list ',' property-ivar
2305///
2306/// property-ivar:
2307/// identifier
2308/// identifier '=' identifier
2309///
John McCall48871652010-08-21 09:40:31 +00002310Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002311 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002312 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002313 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002314
Douglas Gregor88e72a02009-11-18 19:45:45 +00002315 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002316 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002317 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002318 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002319 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002320 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002321
Douglas Gregor88e72a02009-11-18 19:45:45 +00002322 if (Tok.isNot(tok::identifier)) {
2323 Diag(Tok, diag::err_synthesized_property_name);
2324 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002325 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002326 }
Craig Topper161e4db2014-05-21 06:02:52 +00002327
2328 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002329 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2330 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002331 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002332 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002333 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002334 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002335 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002336 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002337 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002338 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002339
2340 if (expectIdentifier())
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002341 break;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002342 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002343 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002344 }
Manman Ren5b786402016-01-28 18:49:28 +00002345 Actions.ActOnPropertyImplDecl(
2346 getCurScope(), atLoc, propertyLoc, true,
2347 propertyId, propertyIvar, propertyIvarLoc,
2348 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002349 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002350 break;
2351 ConsumeToken(); // consume ','
2352 }
Alp Toker383d2c42014-01-01 03:08:43 +00002353 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002354 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002355}
2356
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002357/// property-dynamic:
2358/// @dynamic property-list
2359///
2360/// property-list:
2361/// identifier
2362/// property-list ',' identifier
2363///
John McCall48871652010-08-21 09:40:31 +00002364Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002365 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2366 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002367 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002368
2369 bool isClassProperty = false;
2370 if (Tok.is(tok::l_paren)) {
2371 ConsumeParen();
2372 const IdentifierInfo *II = Tok.getIdentifierInfo();
2373
2374 if (!II) {
2375 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2376 SkipUntil(tok::r_paren, StopAtSemi);
2377 } else {
2378 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2379 if (II->isStr("class")) {
2380 isClassProperty = true;
2381 if (Tok.isNot(tok::r_paren)) {
2382 Diag(Tok, diag::err_expected) << tok::r_paren;
2383 SkipUntil(tok::r_paren, StopAtSemi);
2384 } else
2385 ConsumeParen();
2386 } else {
2387 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2388 SkipUntil(tok::r_paren, StopAtSemi);
2389 }
2390 }
2391 }
2392
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002393 while (true) {
2394 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002395 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002396 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002397 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002398 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002399
2400 if (expectIdentifier()) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002401 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002402 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002403 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002404
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002405 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2406 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002407 Actions.ActOnPropertyImplDecl(
2408 getCurScope(), atLoc, propertyLoc, false,
2409 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002410 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002411 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002412
Chris Lattner0ef13522007-10-09 17:51:17 +00002413 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002414 break;
2415 ConsumeToken(); // consume ','
2416 }
Alp Toker383d2c42014-01-01 03:08:43 +00002417 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002418 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002419}
Mike Stump11289f42009-09-09 15:08:12 +00002420
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002421/// objc-throw-statement:
2422/// throw expression[opt];
2423///
John McCalldadc5752010-08-24 06:29:42 +00002424StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2425 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002426 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002427 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002428 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002429 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002430 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002431 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002432 }
2433 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002434 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002435 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002436 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002437}
2438
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002439/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002440/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002441///
John McCalldadc5752010-08-24 06:29:42 +00002442StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002443Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002444 ConsumeToken(); // consume synchronized
2445 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002446 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002447 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002448 }
John McCalld9bb7432011-07-27 21:50:02 +00002449
2450 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002451 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002452 ExprResult operand(ParseExpression());
2453
2454 if (Tok.is(tok::r_paren)) {
2455 ConsumeParen(); // ')'
2456 } else {
2457 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002458 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002459
2460 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002461 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002462 }
John McCalld9bb7432011-07-27 21:50:02 +00002463
2464 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002465 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002466 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002467 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002468 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002469 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002470
John McCalld9bb7432011-07-27 21:50:02 +00002471 // Check the @synchronized operand now.
2472 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002473 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002474
John McCalld9bb7432011-07-27 21:50:02 +00002475 // Parse the compound statement within a new scope.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002476 ParseScope bodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalld9bb7432011-07-27 21:50:02 +00002477 StmtResult body(ParseCompoundStatementBody());
2478 bodyScope.Exit();
2479
2480 // If there was a semantic or parse error earlier with the
2481 // operand, fail now.
2482 if (operand.isInvalid())
2483 return StmtError();
2484
2485 if (body.isInvalid())
2486 body = Actions.ActOnNullStmt(Tok.getLocation());
2487
2488 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002489}
2490
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002491/// objc-try-catch-statement:
2492/// @try compound-statement objc-catch-list[opt]
2493/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2494///
2495/// objc-catch-list:
2496/// @catch ( parameter-declaration ) compound-statement
2497/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2498/// catch-parameter-declaration:
2499/// parameter-declaration
2500/// '...' [OBJC2]
2501///
John McCalldadc5752010-08-24 06:29:42 +00002502StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002503 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002504
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002505 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002506 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002507 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002508 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002509 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002510 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002511 StmtResult FinallyStmt;
Momchil Velikov57c681f2017-08-10 15:43:06 +00002512 ParseScope TryScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalldadc5752010-08-24 06:29:42 +00002513 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002514 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002515 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002516 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002517
Chris Lattner0ef13522007-10-09 17:51:17 +00002518 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002519 // At this point, we need to lookahead to determine if this @ is the start
2520 // of an @catch or @finally. We don't want to consume the @ token if this
2521 // is an @try or @encode or something else.
2522 Token AfterAt = GetLookAheadToken(1);
2523 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2524 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2525 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002526
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002527 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002528 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002529 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002530 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002531 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002532 ConsumeParen();
Momchil Velikov57c681f2017-08-10 15:43:06 +00002533 ParseScope CatchScope(this, Scope::DeclScope |
2534 Scope::CompoundStmtScope |
2535 Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002536 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002537 DeclSpec DS(AttrFactory);
Faisal Valia534f072018-04-26 00:42:40 +00002538 ParseDeclarationSpecifiers(DS);
Faisal Vali421b2d12017-12-29 05:41:00 +00002539 Declarator ParmDecl(DS, DeclaratorContext::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002540 ParseDeclarator(ParmDecl);
2541
Douglas Gregore11ee112010-04-23 23:01:43 +00002542 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002543 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002544 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002545 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002546 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002547
Steve Naroff65a00892009-04-07 22:56:58 +00002548 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002549
Steve Naroff65a00892009-04-07 22:56:58 +00002550 if (Tok.is(tok::r_paren))
2551 RParenLoc = ConsumeParen();
2552 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002553 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002554
John McCalldadc5752010-08-24 06:29:42 +00002555 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002556 if (Tok.is(tok::l_brace))
2557 CatchBody = ParseCompoundStatementBody();
2558 else
Alp Tokerec543272013-12-24 09:48:30 +00002559 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002560 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002561 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00002562
John McCalldadc5752010-08-24 06:29:42 +00002563 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +00002564 RParenLoc,
2565 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002566 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002567 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002568 CatchStmts.push_back(Catch.get());
Fangrui Song6907ce22018-07-30 19:24:48 +00002569
Steve Naroffe6016792008-02-05 21:27:35 +00002570 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002571 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2572 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002573 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002574 }
2575 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002576 } else {
2577 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002578 ConsumeToken(); // consume finally
Momchil Velikov57c681f2017-08-10 15:43:06 +00002579 ParseScope FinallyScope(this,
2580 Scope::DeclScope | Scope::CompoundStmtScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002581
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002582 bool ShouldCapture =
2583 getTargetInfo().getTriple().isWindowsMSVCEnvironment();
2584 if (ShouldCapture)
2585 Actions.ActOnCapturedRegionStart(Tok.getLocation(), getCurScope(),
2586 CR_ObjCAtFinally, 1);
2587
John McCalldadc5752010-08-24 06:29:42 +00002588 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002589 if (Tok.is(tok::l_brace))
2590 FinallyBody = ParseCompoundStatementBody();
2591 else
Alp Tokerec543272013-12-24 09:48:30 +00002592 Diag(Tok, diag::err_expected) << tok::l_brace;
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002593
2594 if (FinallyBody.isInvalid()) {
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002595 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002596 if (ShouldCapture)
2597 Actions.ActOnCapturedRegionError();
2598 } else if (ShouldCapture) {
2599 FinallyBody = Actions.ActOnCapturedRegionEnd(FinallyBody.get());
2600 }
2601
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002602 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002603 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002604 catch_or_finally_seen = true;
2605 break;
2606 }
2607 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002608 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002609 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002610 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002611 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002612
2613 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002614 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002615 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002616}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002617
John McCall31168b02011-06-15 23:02:42 +00002618/// objc-autoreleasepool-statement:
2619/// @autoreleasepool compound-statement
2620///
2621StmtResult
2622Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2623 ConsumeToken(); // consume autoreleasepool
2624 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002625 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002626 return StmtError();
2627 }
2628 // Enter a scope to hold everything within the compound stmt. Compound
2629 // statements can always hold declarations.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002630 ParseScope BodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCall31168b02011-06-15 23:02:42 +00002631
2632 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2633
2634 BodyScope.Exit();
2635 if (AutoreleasePoolBody.isInvalid())
2636 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00002637 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002638 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002639}
2640
Fangrui Song6907ce22018-07-30 19:24:48 +00002641/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002642/// for later parsing.
2643void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002644 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2645 trySkippingFunctionBody()) {
2646 Actions.ActOnSkippedFunctionBody(MDecl);
2647 return;
2648 }
2649
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002650 LexedMethod* LM = new LexedMethod(this, MDecl);
2651 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2652 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002653 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002654 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002655 if (Tok.is(tok::kw_try)) {
2656 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002657 if (Tok.is(tok::colon)) {
2658 Toks.push_back(Tok);
2659 ConsumeToken();
2660 while (Tok.isNot(tok::l_brace)) {
2661 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2662 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2663 }
2664 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002665 Toks.push_back(Tok); // also store '{'
2666 }
2667 else if (Tok.is(tok::colon)) {
2668 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002669 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002670 while (Tok.isNot(tok::l_brace)) {
2671 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2672 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2673 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002674 Toks.push_back(Tok); // also store '{'
2675 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002676 ConsumeBrace();
2677 // Consume everything up to (and including) the matching right brace.
2678 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002679 while (Tok.is(tok::kw_catch)) {
2680 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2681 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2682 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002683}
2684
Steve Naroff09bf8152007-09-06 21:24:23 +00002685/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002686///
John McCall48871652010-08-21 09:40:31 +00002687Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002688 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002689
Jordan Rose1e879d82018-03-23 00:07:18 +00002690 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, MDecl, Tok.getLocation(),
John McCallfaf5fb42010-08-26 23:41:50 +00002691 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002692
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002693 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002694 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002695 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002696 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002697 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002698 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002699 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002700 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002701
Steve Naroffbb875722007-11-11 19:54:21 +00002702 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002703 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002704 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002705
Steve Naroffbb875722007-11-11 19:54:21 +00002706 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002707 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002708
Steve Naroffbb875722007-11-11 19:54:21 +00002709 // If we didn't find the '{', bail out.
2710 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002711 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002712 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002713
2714 if (!MDecl) {
2715 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002716 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002717 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002718 }
2719
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002720 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002721 Actions.AddAnyMethodToGlobalPool(MDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +00002722 assert (CurParsedObjCImpl
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002723 && "ParseObjCMethodDefinition - Method out of @implementation");
2724 // Consume the tokens and store them for later parsing.
2725 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002726 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002727}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002728
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002729StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc,
2730 ParsedStmtContext StmtCtx) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002731 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002732 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002733 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002734 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002735 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002736
Chris Lattner3ababf52009-12-07 16:33:19 +00002737 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002738 return ParseObjCTryStmt(AtLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00002739
Chris Lattner3ababf52009-12-07 16:33:19 +00002740 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002741 return ParseObjCThrowStmt(AtLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00002742
Chris Lattner3ababf52009-12-07 16:33:19 +00002743 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002744 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002745
2746 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2747 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002748
2749 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2750 getLangOpts().DebuggerSupport) {
2751 SkipUntil(tok::semi);
2752 return Actions.ActOnNullStmt(Tok.getLocation());
2753 }
2754
Alex Lorenza589abc2016-12-01 12:14:38 +00002755 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002756 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002757 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002758 // If the expression is invalid, skip ahead to the next semicolon. Not
2759 // doing this opens us up to the possibility of infinite loops if
2760 // ParseExpression does not consume any tokens.
2761 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002762 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002763 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002764
Steve Naroffe6016792008-02-05 21:27:35 +00002765 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002766 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002767 return handleExprStmt(Res, StmtCtx);
Steve Naroffe6016792008-02-05 21:27:35 +00002768}
2769
John McCalldadc5752010-08-24 06:29:42 +00002770ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002771 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002772 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002773 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002774 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002775 return ExprError();
2776
Ted Kremeneke65b0862012-03-06 20:05:56 +00002777 case tok::minus:
2778 case tok::plus: {
2779 tok::TokenKind Kind = Tok.getKind();
2780 SourceLocation OpLoc = ConsumeToken();
2781
2782 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002783 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002784 switch (Kind) {
2785 case tok::minus: Symbol = "-"; break;
2786 case tok::plus: Symbol = "+"; break;
2787 default: llvm_unreachable("missing unary operator case");
2788 }
2789 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2790 << Symbol;
2791 return ExprError();
2792 }
2793
2794 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2795 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002796 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002797 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002798 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002799
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002800 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002801 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002802 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002803
2804 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002805 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002806 }
2807
Chris Lattnere002fbe2007-12-12 01:04:12 +00002808 case tok::string_literal: // primary-expression: string-literal
2809 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002810 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002811
2812 case tok::char_constant:
2813 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002814
Ted Kremeneke65b0862012-03-06 20:05:56 +00002815 case tok::numeric_constant:
2816 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2817
2818 case tok::kw_true: // Objective-C++, etc.
2819 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2820 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2821 case tok::kw_false: // Objective-C++, etc.
2822 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2823 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
Fangrui Song6907ce22018-07-30 19:24:48 +00002824
Ted Kremeneke65b0862012-03-06 20:05:56 +00002825 case tok::l_square:
2826 // Objective-C array literal
2827 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002828
Ted Kremeneke65b0862012-03-06 20:05:56 +00002829 case tok::l_brace:
2830 // Objective-C dictionary literal
2831 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002832
Patrick Beard0caa3942012-04-19 00:25:12 +00002833 case tok::l_paren:
2834 // Objective-C boxed expression
2835 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002836
Chris Lattnere002fbe2007-12-12 01:04:12 +00002837 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002838 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002839 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002840
Chris Lattner197a3012008-08-05 06:19:09 +00002841 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2842 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002843 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002844 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002845 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002846 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002847 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002848 case tok::objc_available:
2849 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002850 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002851 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002852 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2853 // that this is a proper statement where such directives could actually
2854 // occur.
2855 if (GetLookAheadToken(1).is(tok::l_brace) &&
2856 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002857 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
Fangrui Song6907ce22018-07-30 19:24:48 +00002858 str =
2859 ch == 't' ? "try"
2860 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002861 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002862 }
2863 if (str) {
2864 SourceLocation kwLoc = Tok.getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002865 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002866 FixItHint::CreateReplacement(kwLoc, str));
2867 }
2868 else
2869 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2870 }
Chris Lattner197a3012008-08-05 06:19:09 +00002871 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002872 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002873}
2874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002875/// Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002876///
2877/// This routine parses the receiver of a message send in
2878/// Objective-C++ either as a type or as an expression. Note that this
2879/// routine must not be called to parse a send to 'super', since it
2880/// has no way to return such a result.
Fangrui Song6907ce22018-07-30 19:24:48 +00002881///
Douglas Gregor8d4de672010-04-21 22:36:40 +00002882/// \param IsExpr Whether the receiver was parsed as an expression.
2883///
2884/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2885/// IsExpr is true), the parsed expression. If the receiver was parsed
2886/// as a type (\c IsExpr is false), the parsed type.
2887///
2888/// \returns True if an error occurred during parsing or semantic
2889/// analysis, in which case the arguments do not have valid
2890/// values. Otherwise, returns false for a successful parse.
2891///
2892/// objc-receiver: [C++]
2893/// 'super' [not parsed here]
2894/// expression
2895/// simple-type-specifier
2896/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002897bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002898 InMessageExpressionRAIIObject InMessage(*this, true);
2899
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002900 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2901 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002902 TryAnnotateTypeOrScopeToken();
2903
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002904 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002905 // objc-receiver:
2906 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002907 // Make sure any typos in the receiver are corrected or diagnosed, so that
2908 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2909 // only the things that are valid ObjC receivers?
2910 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002911 if (Receiver.isInvalid())
2912 return true;
2913
2914 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002915 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002916 return false;
2917 }
2918
2919 // objc-receiver:
2920 // typename-specifier
2921 // simple-type-specifier
2922 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002923 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002924 ParseCXXSimpleTypeSpecifier(DS);
Fangrui Song6907ce22018-07-30 19:24:48 +00002925
Douglas Gregor8d4de672010-04-21 22:36:40 +00002926 if (Tok.is(tok::l_paren)) {
2927 // If we see an opening parentheses at this point, we are
2928 // actually parsing an expression that starts with a
2929 // function-style cast, e.g.,
2930 //
2931 // postfix-expression:
2932 // simple-type-specifier ( expression-list [opt] )
2933 // typename-specifier ( expression-list [opt] )
2934 //
2935 // Parse the remainder of this case, then the (optional)
2936 // postfix-expression suffix, followed by the (optional)
2937 // right-hand side of the binary expression. We have an
2938 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002939 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002940 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002941 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002942 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002943 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002944 if (Receiver.isInvalid())
2945 return true;
2946
2947 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002948 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002949 return false;
2950 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002951
Douglas Gregor8d4de672010-04-21 22:36:40 +00002952 // We have a class message. Turn the simple-type-specifier or
2953 // typename-specifier we parsed into a type and parse the
2954 // remainder of the class message.
Faisal Vali421b2d12017-12-29 05:41:00 +00002955 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002956 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002957 if (Type.isInvalid())
2958 return true;
2959
2960 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002961 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002962 return false;
2963}
2964
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002965/// Determine whether the parser is currently referring to a an
Douglas Gregor990ccac2010-05-31 14:40:22 +00002966/// Objective-C message send, using a simplified heuristic to avoid overhead.
2967///
2968/// This routine will only return true for a subset of valid message-send
2969/// expressions.
2970bool Parser::isSimpleObjCMessageExpression() {
Erik Pilkingtonfa983902018-10-30 20:31:30 +00002971 assert(Tok.is(tok::l_square) && getLangOpts().ObjC &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002972 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002973 return GetLookAheadToken(1).is(tok::identifier) &&
2974 GetLookAheadToken(2).is(tok::identifier);
2975}
2976
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002977bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
Erik Pilkingtonfa983902018-10-30 20:31:30 +00002978 if (!getLangOpts().ObjC || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002979 InMessageExpression)
2980 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002981
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002982 ParsedType Type;
2983
Fangrui Song6907ce22018-07-30 19:24:48 +00002984 if (Tok.is(tok::annot_typename))
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002985 Type = getTypeAnnotation(Tok);
2986 else if (Tok.is(tok::identifier))
Fangrui Song6907ce22018-07-30 19:24:48 +00002987 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002988 getCurScope());
2989 else
2990 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002991
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002992 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2993 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002994 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002995 if (Tok.is(tok::identifier))
2996 TryAnnotateTypeOrScopeToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00002997
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002998 return Tok.is(tok::annot_typename);
2999 }
3000 }
3001
3002 return false;
3003}
3004
Mike Stump11289f42009-09-09 15:08:12 +00003005/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003006/// '[' objc-receiver objc-message-args ']'
3007///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003008/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003009/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003010/// expression
3011/// class-name
3012/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003013///
John McCalldadc5752010-08-24 06:29:42 +00003014ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003015 assert(Tok.is(tok::l_square) && "'[' expected");
3016 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3017
Douglas Gregora817a192010-05-27 23:06:34 +00003018 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003019 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003020 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003021 return ExprError();
3022 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003023
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003024 InMessageExpressionRAIIObject InMessage(*this, true);
Fangrui Song6907ce22018-07-30 19:24:48 +00003025
David Blaikiebbafb8a2012-03-11 07:00:24 +00003026 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003027 // We completely separate the C and C++ cases because C++ requires
Fangrui Song6907ce22018-07-30 19:24:48 +00003028 // more complicated (read: slower) parsing.
3029
3030 // Handle send to super.
Douglas Gregor8d4de672010-04-21 22:36:40 +00003031 // FIXME: This doesn't benefit from the same typo-correction we
3032 // get in Objective-C.
3033 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003034 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003035 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3036 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003037
3038 // Parse the receiver, which is either a type or an expression.
3039 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003040 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003041 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003042 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003043 return ExprError();
3044 }
3045
3046 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003047 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3048 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003049
Fangrui Song6907ce22018-07-30 19:24:48 +00003050 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003051 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003052 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003053 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003054
Chris Lattner47054fb2010-05-31 18:18:22 +00003055 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003056 IdentifierInfo *Name = Tok.getIdentifierInfo();
3057 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003058 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003059 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003060 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003061 NextToken().is(tok::period),
3062 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003063 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003064 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3065 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003066
John McCallfaf5fb42010-08-26 23:41:50 +00003067 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003068 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003069 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003070 return ExprError();
3071 }
3072
Douglas Gregore5798dc2010-04-21 20:38:13 +00003073 ConsumeToken(); // the type name
3074
Douglas Gregore83b9562015-07-07 03:57:53 +00003075 // Parse type arguments and protocol qualifiers.
3076 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003077 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003078 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003079 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3080 /*consumeLastToken=*/true,
3081 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003082 if (!NewReceiverType.isUsable()) {
3083 SkipUntil(tok::r_square, StopAtSemi);
3084 return ExprError();
3085 }
3086
3087 ReceiverType = NewReceiverType.get();
3088 }
3089
Fangrui Song6907ce22018-07-30 19:24:48 +00003090 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003091 ReceiverType, nullptr);
3092
John McCallfaf5fb42010-08-26 23:41:50 +00003093 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003094 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003095 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003096 }
Chris Lattner8f697062008-01-25 18:59:06 +00003097 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003098
Chris Lattnera36ec422010-04-11 08:28:14 +00003099 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003100 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003101 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003102 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003103 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003104 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003105
David Blaikieefdccaa2016-01-15 23:43:34 +00003106 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3107 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003108}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003109
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003110/// Parse the remainder of an Objective-C message following the
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003111/// '[' objc-receiver.
3112///
3113/// This routine handles sends to super, class messages (sent to a
3114/// class name), and instance messages (sent to an object), and the
3115/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3116/// ReceiverExpr, respectively. Only one of these parameters may have
3117/// a valid value.
3118///
3119/// \param LBracLoc The location of the opening '['.
3120///
3121/// \param SuperLoc If this is a send to 'super', the location of the
3122/// 'super' keyword that indicates a send to the superclass.
3123///
3124/// \param ReceiverType If this is a class message, the type of the
3125/// class we are sending a message to.
3126///
3127/// \param ReceiverExpr If this is an instance message, the expression
3128/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003129///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003130/// objc-message-args:
3131/// objc-selector
3132/// objc-keywordarg-list
3133///
3134/// objc-keywordarg-list:
3135/// objc-keywordarg
3136/// objc-keywordarg-list objc-keywordarg
3137///
Mike Stump11289f42009-09-09 15:08:12 +00003138/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003139/// selector-name[opt] ':' objc-keywordexpr
3140///
3141/// objc-keywordexpr:
3142/// nonempty-expr-list
3143///
3144/// nonempty-expr-list:
3145/// assignment-expression
3146/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003147///
John McCalldadc5752010-08-24 06:29:42 +00003148ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003149Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003150 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003151 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003152 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003153 InMessageExpressionRAIIObject InMessage(*this, true);
3154
Steve Naroffeae65032009-11-07 02:08:14 +00003155 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003156 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003157 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003158 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003159 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003160 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003161 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003162 else
John McCallb268a282010-08-23 23:25:46 +00003163 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003164 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003165 cutOffParsing();
3166 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003167 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003168
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003169 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003170 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003171 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fangrui Song6907ce22018-07-30 19:24:48 +00003172
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003173 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003174 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003175 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003176
Chris Lattner0ef13522007-10-09 17:51:17 +00003177 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003178 while (1) {
3179 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003180 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003181 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003182
Alp Toker383d2c42014-01-01 03:08:43 +00003183 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003184 // We must manually skip to a ']', otherwise the expression skipper will
3185 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3186 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003187 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003188 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003189 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003190
Mike Stump11289f42009-09-09 15:08:12 +00003191 /// Parse the expression after ':'
Fangrui Song6907ce22018-07-30 19:24:48 +00003192
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003193 if (Tok.is(tok::code_completion)) {
3194 if (SuperLoc.isValid())
Fangrui Song6907ce22018-07-30 19:24:48 +00003195 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003196 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003197 /*AtArgumentExpression=*/true);
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003198 else if (ReceiverType)
3199 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003200 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003201 /*AtArgumentExpression=*/true);
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003202 else
3203 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003204 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003205 /*AtArgumentExpression=*/true);
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003206
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003207 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003208 return ExprError();
3209 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003210
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003211 ExprResult Expr;
3212 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3213 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3214 Expr = ParseBraceInitializer();
3215 } else
3216 Expr = ParseAssignmentExpression();
Fangrui Song6907ce22018-07-30 19:24:48 +00003217
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003218 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003219 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003220 // We must manually skip to a ']', otherwise the expression skipper will
3221 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3222 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003223 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003224 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003225 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003226
Steve Naroff486760a2007-09-17 20:25:27 +00003227 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003228 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003229
Douglas Gregor1b605f72009-11-19 01:08:35 +00003230 // Code completion after each argument.
3231 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003232 if (SuperLoc.isValid())
Fangrui Song6907ce22018-07-30 19:24:48 +00003233 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003234 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003235 /*AtArgumentExpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003236 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003237 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003238 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003239 /*AtArgumentExpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003240 else
John McCallb268a282010-08-23 23:25:46 +00003241 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003242 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003243 /*AtArgumentExpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003244 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003245 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003246 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003247
Steve Naroff486760a2007-09-17 20:25:27 +00003248 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003249 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003250 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003251 break;
3252 // We have a selector or a colon, continue parsing.
3253 }
3254 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003255 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003256 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003257 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003258 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003259 if (Tok.is(tok::colon))
3260 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003261 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003262 if (Tok.is(tok::colon)) {
3263 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3264 FixItHint::CreateRemoval(commaLoc);
3265 }
Chris Lattner197a3012008-08-05 06:19:09 +00003266 // We must manually skip to a ']', otherwise the expression skipper will
3267 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3268 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003269 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003270 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003271 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003272
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003273 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003274 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003275 }
3276 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003277 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003278
Chris Lattner197a3012008-08-05 06:19:09 +00003279 // We must manually skip to a ']', otherwise the expression skipper will
3280 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3281 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003282 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003283 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003284 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003285
Chris Lattner0ef13522007-10-09 17:51:17 +00003286 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003287 Diag(Tok, diag::err_expected)
3288 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003289 // We must manually skip to a ']', otherwise the expression skipper will
3290 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3291 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003292 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003293 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003294 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003295
Chris Lattner8f697062008-01-25 18:59:06 +00003296 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003297
Steve Naroffe61bfa82007-10-05 18:42:47 +00003298 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003299 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003300 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003301 KeyLocs.push_back(Loc);
3302 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003303 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003304
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003305 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003306 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003307 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003308 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003309 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003310 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003311 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003312 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003313}
3314
John McCalldadc5752010-08-24 06:29:42 +00003315ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3316 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003317 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003318
Chris Lattnere002fbe2007-12-12 01:04:12 +00003319 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3320 // expressions. At this point, we know that the only valid thing that starts
3321 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003322 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003323 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003324 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003325 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003326
Chris Lattnere002fbe2007-12-12 01:04:12 +00003327 while (Tok.is(tok::at)) {
3328 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003329
Sebastian Redlc13f2682008-12-09 20:22:58 +00003330 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003331 if (!isTokenStringLiteral())
3332 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003333
John McCalldadc5752010-08-24 06:29:42 +00003334 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003335 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003336 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003337
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003338 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003339 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003340
Craig Topper883dd332015-12-24 23:58:11 +00003341 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003342}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003343
Ted Kremeneke65b0862012-03-06 20:05:56 +00003344/// ParseObjCBooleanLiteral -
3345/// objc-scalar-literal : '@' boolean-keyword
3346/// ;
3347/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3348/// ;
Fangrui Song6907ce22018-07-30 19:24:48 +00003349ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003350 bool ArgValue) {
3351 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3352 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3353}
3354
3355/// ParseObjCCharacterLiteral -
3356/// objc-scalar-literal : '@' character-literal
3357/// ;
3358ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3359 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3360 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003361 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003362 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003363 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003364 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003365}
3366
3367/// ParseObjCNumericLiteral -
3368/// objc-scalar-literal : '@' scalar-literal
3369/// ;
3370/// scalar-literal : | numeric-constant /* any numeric constant. */
3371/// ;
3372ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3373 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3374 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003375 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003376 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003377 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003378 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003379}
3380
Patrick Beard0caa3942012-04-19 00:25:12 +00003381/// ParseObjCBoxedExpr -
3382/// objc-box-expression:
3383/// @( assignment-expression )
3384ExprResult
3385Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3386 if (Tok.isNot(tok::l_paren))
3387 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3388
3389 BalancedDelimiterTracker T(*this, tok::l_paren);
3390 T.consumeOpen();
3391 ExprResult ValueExpr(ParseAssignmentExpression());
3392 if (T.consumeClose())
3393 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003394
3395 if (ValueExpr.isInvalid())
3396 return ExprError();
3397
Patrick Beard0caa3942012-04-19 00:25:12 +00003398 // Wrap the sub-expression in a parenthesized expression, to distinguish
3399 // a boxed expression from a literal.
3400 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003401 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003402 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003403 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003404}
3405
Ted Kremeneke65b0862012-03-06 20:05:56 +00003406ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003407 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003408 ConsumeBracket(); // consume the l_square.
3409
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003410 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003411 while (Tok.isNot(tok::r_square)) {
3412 // Parse list of array element expressions (all must be id types).
3413 ExprResult Res(ParseAssignmentExpression());
3414 if (Res.isInvalid()) {
3415 // We must manually skip to a ']', otherwise the expression skipper will
3416 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3417 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003418 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003419 return Res;
Fangrui Song6907ce22018-07-30 19:24:48 +00003420 }
3421
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003422 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3423 if (Res.isInvalid())
3424 HasInvalidEltExpr = true;
3425
Ted Kremeneke65b0862012-03-06 20:05:56 +00003426 // Parse the ellipsis that indicates a pack expansion.
3427 if (Tok.is(tok::ellipsis))
Fangrui Song6907ce22018-07-30 19:24:48 +00003428 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003429 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003430 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003431
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003432 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003433
3434 if (Tok.is(tok::comma))
3435 ConsumeToken(); // Eat the ','.
3436 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003437 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3438 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003439 }
3440 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003441
3442 if (HasInvalidEltExpr)
3443 return ExprError();
3444
Benjamin Kramerf0623432012-08-23 22:51:59 +00003445 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003446 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003447}
3448
3449ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3450 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3451 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003452 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003453 while (Tok.isNot(tok::r_brace)) {
3454 // Parse the comma separated key : value expressions.
3455 ExprResult KeyExpr;
3456 {
3457 ColonProtectionRAIIObject X(*this);
3458 KeyExpr = ParseAssignmentExpression();
3459 if (KeyExpr.isInvalid()) {
3460 // We must manually skip to a '}', otherwise the expression skipper will
3461 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3462 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003463 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003464 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003465 }
3466 }
3467
Alp Toker383d2c42014-01-01 03:08:43 +00003468 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003469 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003470 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003471 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003472
Ted Kremeneke65b0862012-03-06 20:05:56 +00003473 ExprResult ValueExpr(ParseAssignmentExpression());
3474 if (ValueExpr.isInvalid()) {
3475 // We must manually skip to a '}', otherwise the expression skipper will
3476 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3477 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003478 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003479 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003480 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003481
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003482 // Check the key and value for possible typos
3483 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3484 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3485 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3486 HasInvalidEltExpr = true;
3487
3488 // Parse the ellipsis that designates this as a pack expansion. Do not
3489 // ActOnPackExpansion here, leave it to template instantiation time where
3490 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003491 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003492 if (getLangOpts().CPlusPlus)
3493 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3494
Ted Kremeneke65b0862012-03-06 20:05:56 +00003495 // We have a valid expression. Collect it in a vector so we can
3496 // build the argument list.
Fangrui Song6907ce22018-07-30 19:24:48 +00003497 ObjCDictionaryElement Element = {
3498 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003499 };
3500 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003501
3502 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003503 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3504 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003505 }
3506 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003507
3508 if (HasInvalidEltExpr)
3509 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003510
Ted Kremeneke65b0862012-03-06 20:05:56 +00003511 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003512 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003513 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003514}
3515
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003516/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003517/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003518ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003519Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003520 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003521
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003522 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003523
Chris Lattner197a3012008-08-05 06:19:09 +00003524 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003525 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3526
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003527 BalancedDelimiterTracker T(*this, tok::l_paren);
3528 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003529
Douglas Gregor220cac52009-02-18 17:45:20 +00003530 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003531
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003532 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003533
Douglas Gregor220cac52009-02-18 17:45:20 +00003534 if (Ty.isInvalid())
3535 return ExprError();
3536
Nico Webera7c7e602012-12-31 00:28:03 +00003537 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3538 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003539}
Anders Carlssone01493d2007-08-23 15:25:28 +00003540
3541/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003542/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003543ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003544Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003545 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003546
Chris Lattner197a3012008-08-05 06:19:09 +00003547 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003548 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3549
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003550 BalancedDelimiterTracker T(*this, tok::l_paren);
3551 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003552
Alex Lorenzf1278212017-04-11 15:01:53 +00003553 if (expectIdentifier())
3554 return ExprError();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003555
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003556 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003557 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003558
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003559 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003560
Nico Webera7c7e602012-12-31 00:28:03 +00003561 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3562 T.getOpenLocation(), ProtoIdLoc,
3563 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003564}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003565
3566/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003567/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003568ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003569 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003570
Chris Lattner197a3012008-08-05 06:19:09 +00003571 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003572 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3573
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003574 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003575 SourceLocation sLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00003576
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003577 BalancedDelimiterTracker T(*this, tok::l_paren);
3578 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003579 bool HasOptionalParen = Tok.is(tok::l_paren);
3580 if (HasOptionalParen)
3581 ConsumeParen();
Fangrui Song6907ce22018-07-30 19:24:48 +00003582
Douglas Gregor67c692c2010-08-26 15:07:07 +00003583 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003584 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003585 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003586 return ExprError();
3587 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003588
Chris Lattner4f472a32009-04-11 18:13:45 +00003589 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003590 if (!SelIdent && // missing selector name.
3591 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003592 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003593
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003594 KeyIdents.push_back(SelIdent);
Fangrui Song6907ce22018-07-30 19:24:48 +00003595
Steve Naroff152dd812007-12-05 22:21:29 +00003596 unsigned nColons = 0;
3597 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003598 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003599 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003600 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003601 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003602 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3603 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003604 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003605
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003606 if (Tok.is(tok::r_paren))
3607 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00003608
Douglas Gregor67c692c2010-08-26 15:07:07 +00003609 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003610 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003611 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003612 return ExprError();
3613 }
3614
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003615 // Check for another keyword selector.
3616 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003617 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003618 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003619 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003620 break;
3621 }
Steve Naroff152dd812007-12-05 22:21:29 +00003622 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003623 if (HasOptionalParen && Tok.is(tok::r_paren))
3624 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003625 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003626 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003627 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3628 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003629 T.getCloseLocation(),
3630 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003631}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003632
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003633void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3634 // MCDecl might be null due to error in method or c-function prototype, etc.
3635 Decl *MCDecl = LM.D;
Fangrui Song6907ce22018-07-30 19:24:48 +00003636 bool skip = MCDecl &&
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003637 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3638 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3639 if (skip)
3640 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003641
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003642 // Save the current token position.
3643 SourceLocation OrigLoc = Tok.getLocation();
3644
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003645 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
Alex Lorenz812012f2017-06-19 17:53:21 +00003646 // Store an artificial EOF token to ensure that we don't run off the end of
3647 // the method's body when we come to parse it.
3648 Token Eof;
3649 Eof.startToken();
3650 Eof.setKind(tok::eof);
3651 Eof.setEofData(MCDecl);
3652 Eof.setLocation(OrigLoc);
3653 LM.Toks.push_back(Eof);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003654 // Append the current token at the end of the new token stream so that it
3655 // doesn't get lost.
3656 LM.Toks.push_back(Tok);
Ilya Biryukov929af672019-05-17 09:32:05 +00003657 PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true);
David Blaikie2eabcc92016-02-09 18:52:09 +00003658
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003659 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003660 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00003661
3662 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003663 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003664 // Enter a scope for the method or c-function body.
Momchil Velikov57c681f2017-08-10 15:43:06 +00003665 ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
3666 Scope::FnScope | Scope::DeclScope |
3667 Scope::CompoundStmtScope);
3668
Fangrui Song6907ce22018-07-30 19:24:48 +00003669 // Tell the actions module that we have entered a method or c-function definition
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003670 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003671 if (parseMethod)
3672 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3673 else
3674 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003675 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003676 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003677 else {
3678 if (Tok.is(tok::colon))
3679 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003680 else
3681 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003682 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003683 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003684
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003685 if (Tok.getLocation() != OrigLoc) {
3686 // Due to parsing error, we either went over the cached tokens or
3687 // there are still cached tokens left. If it's the latter case skip the
3688 // leftover tokens.
3689 // Since this is an uncommon situation that should be avoided, use the
3690 // expensive isBeforeInTranslationUnit call.
3691 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3692 OrigLoc))
3693 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3694 ConsumeAnyToken();
3695 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003696 // Clean up the remaining EOF token.
3697 ConsumeAnyToken();
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003698}