blob: 42d6221a7333aeb4542f529596ed1e56ac191a53 [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 Lattner038a3e32008-10-20 05:46:22 +0000713 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000714 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000715 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000716 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000717 break;
Mike Stump11289f42009-09-09 15:08:12 +0000718
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000719 case tok::objc_property:
Chris Lattner038a3e32008-10-20 05:46:22 +0000720 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000721 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000722 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000723 if (Tok.is(tok::l_paren)) {
724 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000725 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000726 }
Mike Stump11289f42009-09-09 15:08:12 +0000727
Douglas Gregor813a0662015-06-19 18:14:38 +0000728 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000729 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
730 if (FD.D.getIdentifier() == nullptr) {
731 Diag(AtLoc, diag::err_objc_property_requires_field_name)
732 << FD.D.getSourceRange();
733 return;
734 }
735 if (FD.BitfieldSize) {
736 Diag(AtLoc, diag::err_objc_property_bitfield)
737 << FD.D.getSourceRange();
738 return;
739 }
740
Douglas Gregor813a0662015-06-19 18:14:38 +0000741 // Map a nullability property attribute to a context-sensitive keyword
742 // attribute.
743 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
744 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
745 OCDS.getNullabilityLoc(),
746 addedToDeclSpec);
747
Benjamin Kramera39beb92014-09-03 11:06:10 +0000748 // Install the property declarator into interfaceDecl.
749 IdentifierInfo *SelName =
750 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
751
752 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
753 IdentifierInfo *SetterName = OCDS.getSetterName();
754 Selector SetterSel;
755 if (SetterName)
756 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
757 else
758 SetterSel = SelectorTable::constructSetterSelector(
759 PP.getIdentifierTable(), PP.getSelectorTable(),
760 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000761 Decl *Property = Actions.ActOnProperty(
762 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000763 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000764
765 FD.complete(Property);
766 };
John McCallcfefb6d2009-11-03 02:38:08 +0000767
Chris Lattner038a3e32008-10-20 05:46:22 +0000768 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000769 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000770 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000771
John McCall405988b2011-03-26 01:53:26 +0000772 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000773 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000774 }
Steve Naroff99264b42007-08-22 16:35:03 +0000775 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000776
777 // We break out of the big loop in two cases: when we see @end or when we see
778 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000779 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000780 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000781 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000782 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000783 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000784 } else {
785 Diag(Tok, diag::err_objc_missing_end)
786 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000787 Diag(CDecl->getBeginLoc(), diag::note_objc_container_start)
788 << (int)Actions.getObjCContainerKind();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000789 AtEnd.setBegin(Tok.getLocation());
790 AtEnd.setEnd(Tok.getLocation());
791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000793 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000794 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000795 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000796}
797
Douglas Gregor813a0662015-06-19 18:14:38 +0000798/// Diagnose redundant or conflicting nullability information.
799static void diagnoseRedundantPropertyNullability(Parser &P,
800 ObjCDeclSpec &DS,
801 NullabilityKind nullability,
802 SourceLocation nullabilityLoc){
803 if (DS.getNullability() == nullability) {
804 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000805 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000806 << SourceRange(DS.getNullabilityLoc());
807 return;
808 }
809
810 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000811 << DiagNullabilityKind(nullability, true)
812 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000813 << SourceRange(DS.getNullabilityLoc());
814}
815
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000816/// Parse property attribute declarations.
817///
818/// property-attr-decl: '(' property-attrlist ')'
819/// property-attrlist:
820/// property-attribute
821/// property-attrlist ',' property-attribute
822/// property-attribute:
823/// getter '=' identifier
824/// setter '=' identifier ':'
825/// readonly
826/// readwrite
827/// assign
828/// retain
829/// copy
830/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000831/// atomic
832/// strong
833/// weak
834/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000835/// nonnull
836/// nullable
837/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000838/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000839/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000840///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000841void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000842 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000843 BalancedDelimiterTracker T(*this, tok::l_paren);
844 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000846 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000847 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000848 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000849 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000850 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000851 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner76619232008-10-20 07:22:18 +0000853 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000854 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000855 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000856 return;
857 }
Mike Stump11289f42009-09-09 15:08:12 +0000858
Chris Lattner1db33542008-10-20 07:37:22 +0000859 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner68e48682008-11-20 04:42:34 +0000861 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000862 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000863 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000864 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000865 else if (II->isStr("unsafe_unretained"))
866 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000867 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000868 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000869 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000870 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000871 else if (II->isStr("strong"))
872 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000873 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000874 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000875 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000876 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000877 else if (II->isStr("atomic"))
878 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000879 else if (II->isStr("weak"))
880 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000881 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000882 bool IsSetter = II->getNameStart()[0] == 's';
883
Chris Lattner825bca12008-10-20 07:39:53 +0000884 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000885 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000886 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000887
Alp Toker383d2c42014-01-01 03:08:43 +0000888 if (ExpectAndConsume(tok::equal, DiagID)) {
889 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000890 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000891 }
Mike Stump11289f42009-09-09 15:08:12 +0000892
Douglas Gregorc8537c52009-11-19 07:41:15 +0000893 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000894 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000895 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000896 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000897 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000898 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000899 }
900
Anders Carlssonfe15a782010-10-02 17:45:21 +0000901 SourceLocation SelLoc;
902 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
903
904 if (!SelIdent) {
905 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
906 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000907 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000908 return;
909 }
Mike Stump11289f42009-09-09 15:08:12 +0000910
Anders Carlssonfe15a782010-10-02 17:45:21 +0000911 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000912 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000913 DS.setSetterName(SelIdent, SelLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000914
Alp Toker383d2c42014-01-01 03:08:43 +0000915 if (ExpectAndConsume(tok::colon,
916 diag::err_expected_colon_after_setter_name)) {
917 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000918 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000919 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000920 } else {
921 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000922 DS.setGetterName(SelIdent, SelLoc);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000923 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000924 } else if (II->isStr("nonnull")) {
925 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
926 diagnoseRedundantPropertyNullability(*this, DS,
927 NullabilityKind::NonNull,
928 Tok.getLocation());
929 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
930 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
931 } else if (II->isStr("nullable")) {
932 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
933 diagnoseRedundantPropertyNullability(*this, DS,
934 NullabilityKind::Nullable,
935 Tok.getLocation());
936 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
937 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
938 } else if (II->isStr("null_unspecified")) {
939 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
940 diagnoseRedundantPropertyNullability(*this, DS,
941 NullabilityKind::Unspecified,
942 Tok.getLocation());
943 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
944 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000945 } else if (II->isStr("null_resettable")) {
946 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
947 diagnoseRedundantPropertyNullability(*this, DS,
948 NullabilityKind::Unspecified,
949 Tok.getLocation());
950 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
951 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
952
953 // Also set the null_resettable bit.
954 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000955 } else if (II->isStr("class")) {
956 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000957 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000958 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000959 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000960 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000961 }
Mike Stump11289f42009-09-09 15:08:12 +0000962
Chris Lattner1db33542008-10-20 07:37:22 +0000963 if (Tok.isNot(tok::comma))
964 break;
Mike Stump11289f42009-09-09 15:08:12 +0000965
Chris Lattner1db33542008-10-20 07:37:22 +0000966 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000967 }
Mike Stump11289f42009-09-09 15:08:12 +0000968
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000969 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000970}
971
Steve Naroff09bf8152007-09-06 21:24:23 +0000972/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000973/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000974/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000975///
976/// objc-instance-method: '-'
977/// objc-class-method: '+'
978///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000979/// objc-method-attributes: [OBJC2]
980/// __attribute__((deprecated))
981///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000982Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000983 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000984 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000985
Mike Stump11289f42009-09-09 15:08:12 +0000986 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000987 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000988 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000989 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000990 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000991 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000992 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000993}
994
995/// objc-selector:
996/// identifier
997/// one of
998/// enum struct union if else while do for switch case default
999/// break continue return goto asm sizeof typeof __alignof
1000/// unsigned long const short volatile signed restrict _Complex
1001/// in out inout bycopy byref oneway int char float double void _Bool
1002///
Chris Lattner4f472a32009-04-11 18:13:45 +00001003IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001004
Chris Lattner5700fab2007-10-07 02:00:24 +00001005 switch (Tok.getKind()) {
1006 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001007 return nullptr;
Alex Lorenz9b9188d2017-07-13 10:50:21 +00001008 case tok::colon:
1009 // Empty selector piece uses the location of the ':'.
1010 SelectorLoc = Tok.getLocation();
1011 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001012 case tok::ampamp:
1013 case tok::ampequal:
1014 case tok::amp:
1015 case tok::pipe:
1016 case tok::tilde:
1017 case tok::exclaim:
1018 case tok::exclaimequal:
1019 case tok::pipepipe:
1020 case tok::pipeequal:
1021 case tok::caret:
1022 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001023 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001024 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001025 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001026 Tok.setKind(tok::identifier);
1027 SelectorLoc = ConsumeToken();
1028 return II;
1029 }
Craig Topper161e4db2014-05-21 06:02:52 +00001030 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001031 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001032
Chris Lattner5700fab2007-10-07 02:00:24 +00001033 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001034 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001035 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001036 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001037 case tok::kw_break:
1038 case tok::kw_case:
1039 case tok::kw_catch:
1040 case tok::kw_char:
1041 case tok::kw_class:
1042 case tok::kw_const:
1043 case tok::kw_const_cast:
1044 case tok::kw_continue:
1045 case tok::kw_default:
1046 case tok::kw_delete:
1047 case tok::kw_do:
1048 case tok::kw_double:
1049 case tok::kw_dynamic_cast:
1050 case tok::kw_else:
1051 case tok::kw_enum:
1052 case tok::kw_explicit:
1053 case tok::kw_export:
1054 case tok::kw_extern:
1055 case tok::kw_false:
1056 case tok::kw_float:
1057 case tok::kw_for:
1058 case tok::kw_friend:
1059 case tok::kw_goto:
1060 case tok::kw_if:
1061 case tok::kw_inline:
1062 case tok::kw_int:
1063 case tok::kw_long:
1064 case tok::kw_mutable:
1065 case tok::kw_namespace:
1066 case tok::kw_new:
1067 case tok::kw_operator:
1068 case tok::kw_private:
1069 case tok::kw_protected:
1070 case tok::kw_public:
1071 case tok::kw_register:
1072 case tok::kw_reinterpret_cast:
1073 case tok::kw_restrict:
1074 case tok::kw_return:
1075 case tok::kw_short:
1076 case tok::kw_signed:
1077 case tok::kw_sizeof:
1078 case tok::kw_static:
1079 case tok::kw_static_cast:
1080 case tok::kw_struct:
1081 case tok::kw_switch:
1082 case tok::kw_template:
1083 case tok::kw_this:
1084 case tok::kw_throw:
1085 case tok::kw_true:
1086 case tok::kw_try:
1087 case tok::kw_typedef:
1088 case tok::kw_typeid:
1089 case tok::kw_typename:
1090 case tok::kw_typeof:
1091 case tok::kw_union:
1092 case tok::kw_unsigned:
1093 case tok::kw_using:
1094 case tok::kw_virtual:
1095 case tok::kw_void:
1096 case tok::kw_volatile:
1097 case tok::kw_wchar_t:
1098 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001099 case tok::kw__Bool:
1100 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001101 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001102 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001103 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001104 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001105 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001106 }
Steve Naroff99264b42007-08-22 16:35:03 +00001107}
1108
Fariborz Jahanian83615522008-01-02 22:54:34 +00001109/// objc-for-collection-in: 'in'
1110///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001111bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001112 // FIXME: May have to do additional look-ahead to only allow for
1113 // valid tokens following an 'in'; such as an identifier, unary operators,
1114 // '[' etc.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001115 return (getLangOpts().ObjC && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001116 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001117}
1118
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001119/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001120/// qualifier list and builds their bitmask representation in the input
1121/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001122///
1123/// objc-type-qualifiers:
1124/// objc-type-qualifier
1125/// objc-type-qualifiers objc-type-qualifier
1126///
Douglas Gregor813a0662015-06-19 18:14:38 +00001127/// objc-type-qualifier:
1128/// 'in'
1129/// 'out'
1130/// 'inout'
1131/// 'oneway'
1132/// 'bycopy'
1133/// 'byref'
1134/// 'nonnull'
1135/// 'nullable'
1136/// 'null_unspecified'
1137///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001138void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001139 DeclaratorContext Context) {
1140 assert(Context == DeclaratorContext::ObjCParameterContext ||
1141 Context == DeclaratorContext::ObjCResultContext);
John McCalla55902b2011-10-01 09:56:14 +00001142
Chris Lattner61511e12007-12-12 06:56:32 +00001143 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001144 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001145 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001146 Context == DeclaratorContext::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001147 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001148 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001149
Chris Lattner5e530bc2007-12-27 19:57:00 +00001150 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001151 return;
Mike Stump11289f42009-09-09 15:08:12 +00001152
Chris Lattner61511e12007-12-12 06:56:32 +00001153 const IdentifierInfo *II = Tok.getIdentifierInfo();
1154 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001155 if (II != ObjCTypeQuals[i] ||
1156 NextToken().is(tok::less) ||
1157 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001158 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001159
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001160 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001161 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001162 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001163 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001164 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1165 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1166 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1167 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1168 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1169 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001170
Fangrui Song6907ce22018-07-30 19:24:48 +00001171 case objc_nonnull:
Douglas Gregor813a0662015-06-19 18:14:38 +00001172 Qual = ObjCDeclSpec::DQ_CSNullability;
1173 Nullability = NullabilityKind::NonNull;
1174 break;
1175
Fangrui Song6907ce22018-07-30 19:24:48 +00001176 case objc_nullable:
Douglas Gregor813a0662015-06-19 18:14:38 +00001177 Qual = ObjCDeclSpec::DQ_CSNullability;
1178 Nullability = NullabilityKind::Nullable;
1179 break;
1180
Fangrui Song6907ce22018-07-30 19:24:48 +00001181 case objc_null_unspecified:
Douglas Gregor813a0662015-06-19 18:14:38 +00001182 Qual = ObjCDeclSpec::DQ_CSNullability;
1183 Nullability = NullabilityKind::Unspecified;
1184 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001185 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001186
1187 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001188 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001189 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1190 DS.setNullability(Tok.getLocation(), Nullability);
1191
Chris Lattner61511e12007-12-12 06:56:32 +00001192 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001193 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001194 break;
1195 }
Mike Stump11289f42009-09-09 15:08:12 +00001196
Chris Lattner61511e12007-12-12 06:56:32 +00001197 // If this wasn't a recognized qualifier, bail out.
1198 if (II) return;
1199 }
1200}
1201
John McCalla55902b2011-10-01 09:56:14 +00001202/// Take all the decl attributes out of the given list and add
1203/// them to the given attribute set.
Erich Keanec480f302018-07-12 21:09:05 +00001204static void takeDeclAttributes(ParsedAttributesView &attrs,
1205 ParsedAttributesView &from) {
1206 for (auto &AL : llvm::reverse(from)) {
1207 if (!AL.isUsedAsTypeAttr()) {
1208 from.remove(&AL);
Michael Krusedc5ce722018-08-03 01:21:16 +00001209 attrs.addAtEnd(&AL);
John McCalla55902b2011-10-01 09:56:14 +00001210 }
1211 }
1212}
1213
1214/// takeDeclAttributes - Take all the decl attributes from the given
1215/// declarator and add them to the given list.
1216static void takeDeclAttributes(ParsedAttributes &attrs,
1217 Declarator &D) {
1218 // First, take ownership of all attributes.
1219 attrs.getPool().takeAllFrom(D.getAttributePool());
1220 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1221
1222 // Now actually move the attributes over.
Erich Keanec480f302018-07-12 21:09:05 +00001223 takeDeclAttributes(attrs, D.getMutableDeclSpec().getAttributes());
John McCalla55902b2011-10-01 09:56:14 +00001224 takeDeclAttributes(attrs, D.getAttributes());
1225 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
Erich Keanec480f302018-07-12 21:09:05 +00001226 takeDeclAttributes(attrs, D.getTypeObject(i).getAttrs());
John McCalla55902b2011-10-01 09:56:14 +00001227}
1228
Chris Lattner61511e12007-12-12 06:56:32 +00001229/// objc-type-name:
1230/// '(' objc-type-qualifiers[opt] type-name ')'
1231/// '(' objc-type-qualifiers[opt] ')'
1232///
Fangrui Song6907ce22018-07-30 19:24:48 +00001233ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001234 DeclaratorContext context,
John McCalla55902b2011-10-01 09:56:14 +00001235 ParsedAttributes *paramAttrs) {
Faisal Vali421b2d12017-12-29 05:41:00 +00001236 assert(context == DeclaratorContext::ObjCParameterContext ||
1237 context == DeclaratorContext::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001238 assert((paramAttrs != nullptr) ==
Faisal Vali421b2d12017-12-29 05:41:00 +00001239 (context == DeclaratorContext::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001240
Chris Lattner0ef13522007-10-09 17:51:17 +00001241 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001242
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001243 BalancedDelimiterTracker T(*this, tok::l_paren);
1244 T.consumeOpen();
1245
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001246 ObjCDeclContextSwitch ObjCDC(*this);
1247
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001248 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001249 ParseObjCTypeQualifierList(DS, context);
Erik Pilkingtond7999cb2019-06-26 23:39:23 +00001250 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff7e901fd2007-08-22 23:18:22 +00001251
John McCallba7bf592010-08-24 05:47:05 +00001252 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001253 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001254 // Parse an abstract declarator.
1255 DeclSpec declSpec(AttrFactory);
1256 declSpec.setObjCQualifiers(&DS);
Faisal Vali7db85c52017-12-31 00:06:40 +00001257 DeclSpecContext dsContext = DeclSpecContext::DSC_normal;
Faisal Vali421b2d12017-12-29 05:41:00 +00001258 if (context == DeclaratorContext::ObjCResultContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00001259 dsContext = DeclSpecContext::DSC_objc_method_result;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001260 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001261 Declarator declarator(declSpec, context);
1262 ParseDeclarator(declarator);
1263
1264 // If that's not invalid, extract a type.
1265 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001266 // Map a nullability specifier to a context-sensitive keyword attribute.
1267 bool addedToDeclSpec = false;
1268 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1269 addContextSensitiveTypeNullability(*this, declarator,
1270 DS.getNullability(),
1271 DS.getNullabilityLoc(),
1272 addedToDeclSpec);
1273
John McCalla55902b2011-10-01 09:56:14 +00001274 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1275 if (!type.isInvalid())
1276 Ty = type.get();
1277
1278 // If we're parsing a parameter, steal all the decl attributes
1279 // and add them to the decl spec.
Faisal Vali421b2d12017-12-29 05:41:00 +00001280 if (context == DeclaratorContext::ObjCParameterContext)
John McCalla55902b2011-10-01 09:56:14 +00001281 takeDeclAttributes(*paramAttrs, declarator);
1282 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001283 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001284
Steve Naroff90255b42008-10-21 14:15:04 +00001285 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001286 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001287 else if (Tok.getLocation() == TypeStartLoc) {
1288 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001289 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001290 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001291 } else {
1292 // Otherwise, we found *something*, but didn't get a ')' in the right
1293 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001294 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001295 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001296 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001297}
1298
1299/// objc-method-decl:
1300/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001301/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001302/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001303/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001304///
1305/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001306/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001307/// objc-keyword-selector objc-keyword-decl
1308///
1309/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001310/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1311/// objc-selector ':' objc-keyword-attributes[opt] identifier
1312/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1313/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001314///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001315/// objc-parmlist:
1316/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001317///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001318/// objc-parms:
1319/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001320///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001321/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001322/// , ...
1323///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001324/// objc-keyword-attributes: [OBJC2]
1325/// __attribute__((unused))
1326///
John McCall48871652010-08-21 09:40:31 +00001327Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001328 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001329 tok::ObjCKeywordKind MethodImplKind,
1330 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001331 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001332
Douglas Gregor636a61e2010-04-07 00:21:17 +00001333 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001334 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1335 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001336 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001337 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001338 }
1339
Chris Lattner2ebb1782008-08-23 01:48:03 +00001340 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001341 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001342 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001343 if (Tok.is(tok::l_paren))
Faisal Vali421b2d12017-12-29 05:41:00 +00001344 ReturnType = ParseObjCTypeName(DSRet, DeclaratorContext::ObjCResultContext,
Craig Topper161e4db2014-05-21 06:02:52 +00001345 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001346
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001347 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001348 ParsedAttributes methodAttrs(AttrFactory);
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001349 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001350 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001351 MaybeParseCXX11Attributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001352
Douglas Gregor636a61e2010-04-07 00:21:17 +00001353 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001354 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001355 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001356 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001357 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001358 }
1359
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001360 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001361 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001362 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001363
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001364 // An unnamed colon is valid.
1365 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001366 Diag(Tok, diag::err_expected_selector_for_method)
1367 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001368 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001369 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001370 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001371 }
Mike Stump11289f42009-09-09 15:08:12 +00001372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001373 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001374 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001375 // If attributes exist after the method, parse them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001376 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001377 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001378 MaybeParseCXX11Attributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattner5700fab2007-10-07 02:00:24 +00001380 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Erich Keanec480f302018-07-12 21:09:05 +00001381 Decl *Result = Actions.ActOnMethodDeclaration(
1382 getCurScope(), mLoc, Tok.getLocation(), mType, DSRet, ReturnType,
1383 selLoc, Sel, nullptr, CParamInfo.data(), CParamInfo.size(), methodAttrs,
1384 MethodImplKind, false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001385 PD.complete(Result);
1386 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001387 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001388
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001389 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001390 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001391 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001392 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1393 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001394
1395 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001396 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001397 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001398 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001399
Chris Lattner5700fab2007-10-07 02:00:24 +00001400 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001401 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001402 break;
Mike Stump11289f42009-09-09 15:08:12 +00001403
David Blaikieefdccaa2016-01-15 23:43:34 +00001404 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001405 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001406 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
Faisal Vali421b2d12017-12-29 05:41:00 +00001407 DeclaratorContext::ObjCParameterContext,
John McCalla55902b2011-10-01 09:56:14 +00001408 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001409
Chris Lattner5700fab2007-10-07 02:00:24 +00001410 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001411 // Regardless, collect all the attributes we've parsed so far.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001412 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001413 MaybeParseGNUAttributes(paramAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001414 MaybeParseCXX11Attributes(paramAttrs);
Erich Keanec480f302018-07-12 21:09:05 +00001415 ArgInfo.ArgAttrs = paramAttrs;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001416
Douglas Gregor45879692010-07-08 23:37:41 +00001417 // Code completion for the next piece of the selector.
1418 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001419 KeyIdents.push_back(SelIdent);
Fangrui Song6907ce22018-07-30 19:24:48 +00001420 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
Douglas Gregor45879692010-07-08 23:37:41 +00001421 mType == tok::minus,
1422 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001423 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001424 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001425 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001426 }
Alex Lorenzf1278212017-04-11 15:01:53 +00001427
1428 if (expectIdentifier())
1429 break; // missing argument name.
Mike Stump11289f42009-09-09 15:08:12 +00001430
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001431 ArgInfo.Name = Tok.getIdentifierInfo();
1432 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001433 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001434
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001435 ArgInfos.push_back(ArgInfo);
1436 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001437 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001438
John McCall084e83d2011-03-24 11:26:52 +00001439 // Make sure the attributes persist.
1440 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1441
Douglas Gregor95887f92010-07-08 23:20:03 +00001442 // Code completion for the next piece of the selector.
1443 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001444 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
Douglas Gregor95887f92010-07-08 23:20:03 +00001445 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001446 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001447 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001448 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001449 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001450 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001451
Chris Lattner5700fab2007-10-07 02:00:24 +00001452 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001453 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001454 if (!SelIdent && Tok.isNot(tok::colon))
1455 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001456 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001457 SourceLocation ColonLoc = Tok.getLocation();
1458 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001459 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1460 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1461 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001462 }
1463 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001464 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001465 }
Mike Stump11289f42009-09-09 15:08:12 +00001466
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001467 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001468 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001469 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001470 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001471 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001472 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001473 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001474 ConsumeToken();
1475 break;
1476 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001477 if (!cStyleParamWarned) {
1478 Diag(Tok, diag::warn_cstyle_param);
1479 cStyleParamWarned = true;
1480 }
John McCall084e83d2011-03-24 11:26:52 +00001481 DeclSpec DS(AttrFactory);
Faisal Valia534f072018-04-26 00:42:40 +00001482 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001483 // Parse the declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001484 Declarator ParmDecl(DS, DeclaratorContext::PrototypeContext);
Chris Lattner5700fab2007-10-07 02:00:24 +00001485 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001486 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001487 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001488 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Fangrui Song6907ce22018-07-30 19:24:48 +00001489 ParmDecl.getIdentifierLoc(),
Fariborz Jahanian60462092010-04-08 00:30:06 +00001490 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001491 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001492 }
Mike Stump11289f42009-09-09 15:08:12 +00001493
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001494 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001495 // If attributes exist after the method, parse them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001496 if (getLangOpts().ObjC)
John McCall084e83d2011-03-24 11:26:52 +00001497 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001498 MaybeParseCXX11Attributes(methodAttrs);
1499
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001500 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001501 return nullptr;
1502
Chris Lattner5700fab2007-10-07 02:00:24 +00001503 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1504 &KeyIdents[0]);
Erich Keanec480f302018-07-12 21:09:05 +00001505 Decl *Result = Actions.ActOnMethodDeclaration(
1506 getCurScope(), mLoc, Tok.getLocation(), mType, DSRet, ReturnType, KeyLocs,
1507 Sel, &ArgInfos[0], CParamInfo.data(), CParamInfo.size(), methodAttrs,
1508 MethodImplKind, isVariadic, MethodDefinition);
1509
John McCall28a6aea2009-11-04 02:18:39 +00001510 PD.complete(Result);
1511 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001512}
1513
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001514/// objc-protocol-refs:
1515/// '<' identifier-list '>'
1516///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001517bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001518ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1519 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001520 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001521 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1522 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001523 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001524
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001525 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001526
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001527 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001528
Chris Lattner3bbae002008-07-26 04:03:38 +00001529 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001530 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001531 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001532 cutOffParsing();
1533 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001534 }
1535
Alex Lorenzf1278212017-04-11 15:01:53 +00001536 if (expectIdentifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001537 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001538 return true;
1539 }
1540 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1541 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001542 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001543 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001544
Alp Toker383d2c42014-01-01 03:08:43 +00001545 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001546 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001547 }
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner3bbae002008-07-26 04:03:38 +00001549 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001550 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001551 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001552 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001553
Chris Lattner3bbae002008-07-26 04:03:38 +00001554 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001555 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001556 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001557 return false;
1558}
1559
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001560TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001561 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001562 assert(getLangOpts().ObjC && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001563
1564 SourceLocation lAngleLoc;
1565 SmallVector<Decl *, 8> protocols;
1566 SmallVector<SourceLocation, 8> protocolLocs;
1567 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1568 lAngleLoc, rAngleLoc,
1569 /*consumeLastToken=*/true);
1570 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1571 protocols,
1572 protocolLocs,
1573 rAngleLoc);
1574 if (result.isUsable()) {
1575 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1576 << FixItHint::CreateInsertion(lAngleLoc, "id")
1577 << SourceRange(lAngleLoc, rAngleLoc);
1578 }
1579
1580 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001581}
1582
Douglas Gregore9d95f12015-07-07 03:57:35 +00001583/// Parse Objective-C type arguments or protocol qualifiers.
1584///
1585/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001586/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001587///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001588void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001589 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001590 SourceLocation &typeArgsLAngleLoc,
1591 SmallVectorImpl<ParsedType> &typeArgs,
1592 SourceLocation &typeArgsRAngleLoc,
1593 SourceLocation &protocolLAngleLoc,
1594 SmallVectorImpl<Decl *> &protocols,
1595 SmallVectorImpl<SourceLocation> &protocolLocs,
1596 SourceLocation &protocolRAngleLoc,
1597 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001598 bool warnOnIncompleteProtocols) {
1599 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1600 SourceLocation lAngleLoc = ConsumeToken();
1601
1602 // Whether all of the elements we've parsed thus far are single
1603 // identifiers, which might be types or might be protocols.
1604 bool allSingleIdentifiers = true;
1605 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001606 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001607
1608 // Parse a list of comma-separated identifiers, bailing out if we
1609 // see something different.
1610 do {
1611 // Parse a single identifier.
1612 if (Tok.is(tok::identifier) &&
1613 (NextToken().is(tok::comma) ||
1614 NextToken().is(tok::greater) ||
1615 NextToken().is(tok::greatergreater))) {
1616 identifiers.push_back(Tok.getIdentifierInfo());
1617 identifierLocs.push_back(ConsumeToken());
1618 continue;
1619 }
1620
1621 if (Tok.is(tok::code_completion)) {
1622 // FIXME: Also include types here.
1623 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1624 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001625 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
Douglas Gregore9d95f12015-07-07 03:57:35 +00001626 identifierLocs[i]));
1627 }
1628
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001629 QualType BaseT = Actions.GetTypeFromParser(baseType);
1630 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1631 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1632 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001633 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001634 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001635 cutOffParsing();
1636 return;
1637 }
1638
1639 allSingleIdentifiers = false;
1640 break;
1641 } while (TryConsumeToken(tok::comma));
1642
1643 // If we parsed an identifier list, semantic analysis sorts out
1644 // whether it refers to protocols or to type arguments.
1645 if (allSingleIdentifiers) {
1646 // Parse the closing '>'.
1647 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001648 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001649 /*ObjCGenericList=*/true);
1650
1651 // Let Sema figure out what we parsed.
1652 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001653 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001654 lAngleLoc,
1655 identifiers,
1656 identifierLocs,
1657 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001658 typeArgsLAngleLoc,
1659 typeArgs,
1660 typeArgsRAngleLoc,
1661 protocolLAngleLoc,
1662 protocols,
1663 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001664 warnOnIncompleteProtocols);
1665 return;
1666 }
1667
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001668 // We parsed an identifier list but stumbled into non single identifiers, this
1669 // means we might (a) check that what we already parsed is a legitimate type
1670 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1671 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001672
1673 // Convert the identifiers into type arguments.
1674 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001675 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1676 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1677 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1678 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1679
Douglas Gregore9d95f12015-07-07 03:57:35 +00001680 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1681 ParsedType typeArg
1682 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1683 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001684 DeclSpec DS(AttrFactory);
1685 const char *prevSpec = nullptr;
1686 unsigned diagID;
Faisal Vali090da2d2018-01-01 18:23:28 +00001687 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1688 typeArg, Actions.getASTContext().getPrintingPolicy());
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001689
1690 // Form a declarator to turn this into a type.
Faisal Vali421b2d12017-12-29 05:41:00 +00001691 Declarator D(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001692 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001693 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001694 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001695 if (!foundValidTypeId) {
1696 foundValidTypeId = identifiers[i];
1697 foundValidTypeSrcLoc = identifierLocs[i];
1698 }
1699 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001700 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001701 unknownTypeArgs.push_back(identifiers[i]);
1702 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1703 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001704 } else {
1705 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001706 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1707 unknownTypeArgs.push_back(identifiers[i]);
1708 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1709 } else if (!foundProtocolId) {
1710 foundProtocolId = identifiers[i];
1711 foundProtocolSrcLoc = identifierLocs[i];
1712 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001713 }
1714 }
1715
1716 // Continue parsing type-names.
1717 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001718 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001719 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001720
1721 // Consume the '...' for a pack expansion.
1722 SourceLocation ellipsisLoc;
1723 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1724 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1725 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1726 }
1727
Douglas Gregore9d95f12015-07-07 03:57:35 +00001728 if (typeArg.isUsable()) {
1729 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001730 if (!foundValidTypeId) {
1731 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1732 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1733 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001734 } else {
1735 invalid = true;
1736 }
1737 } while (TryConsumeToken(tok::comma));
1738
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001739 // Diagnose the mix between type args and protocols.
1740 if (foundProtocolId && foundValidTypeId)
1741 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1742 foundValidTypeId,
1743 foundValidTypeSrcLoc);
1744
1745 // Diagnose unknown arg types.
1746 ParsedType T;
1747 if (unknownTypeArgs.size())
1748 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1749 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1750 getCurScope(), nullptr, T);
1751
Douglas Gregore9d95f12015-07-07 03:57:35 +00001752 // Parse the closing '>'.
1753 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001754 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001755 /*ObjCGenericList=*/true);
1756
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001757 if (invalid) {
1758 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001759 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001760 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001761
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001762 // Record left/right angle locations.
1763 typeArgsLAngleLoc = lAngleLoc;
1764 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001765}
1766
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001767void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001768 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001769 SourceLocation &typeArgsLAngleLoc,
1770 SmallVectorImpl<ParsedType> &typeArgs,
1771 SourceLocation &typeArgsRAngleLoc,
1772 SourceLocation &protocolLAngleLoc,
1773 SmallVectorImpl<Decl *> &protocols,
1774 SmallVectorImpl<SourceLocation> &protocolLocs,
1775 SourceLocation &protocolRAngleLoc,
1776 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001777 assert(Tok.is(tok::less));
1778
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001779 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001780 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1781 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001782 typeArgs,
1783 typeArgsRAngleLoc,
1784 protocolLAngleLoc,
1785 protocols,
1786 protocolLocs,
1787 protocolRAngleLoc,
1788 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001789 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001790 if (Tok.is(tok::eof)) // Nothing else to do here...
1791 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001792
1793 // An Objective-C object pointer followed by type arguments
1794 // can then be followed again by a set of protocol references, e.g.,
1795 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001796 if ((consumeLastToken && Tok.is(tok::less)) ||
1797 (!consumeLastToken && NextToken().is(tok::less))) {
1798 // If we aren't consuming the last token, the prior '>' is still hanging
1799 // there. Consume it before we parse the protocol qualifiers.
1800 if (!consumeLastToken)
1801 ConsumeToken();
1802
1803 if (!protocols.empty()) {
1804 SkipUntilFlags skipFlags = SkipUntilFlags();
1805 if (!consumeLastToken)
1806 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001807 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001808 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1809 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001810 } else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001811 ParseObjCProtocolReferences(protocols, protocolLocs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001812 /*WarnOnDeclarations=*/false,
1813 /*ForObjCContainer=*/false,
Fangrui Song6907ce22018-07-30 19:24:48 +00001814 protocolLAngleLoc, protocolRAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001815 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001816 }
1817 }
1818}
1819
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001820TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1821 SourceLocation loc,
1822 ParsedType type,
1823 bool consumeLastToken,
1824 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001825 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001826 SourceLocation typeArgsLAngleLoc;
1827 SmallVector<ParsedType, 4> typeArgs;
1828 SourceLocation typeArgsRAngleLoc;
1829 SourceLocation protocolLAngleLoc;
1830 SmallVector<Decl *, 4> protocols;
1831 SmallVector<SourceLocation, 4> protocolLocs;
1832 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001833
1834 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001835 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001836 typeArgsRAngleLoc, protocolLAngleLoc,
1837 protocols, protocolLocs,
1838 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001839
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001840 if (Tok.is(tok::eof))
1841 return true; // Invalid type result.
1842
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001843 // Compute the location of the last token.
1844 if (consumeLastToken)
1845 endLoc = PrevTokLocation;
1846 else
1847 endLoc = Tok.getLocation();
1848
1849 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1850 getCurScope(),
1851 loc,
1852 type,
1853 typeArgsLAngleLoc,
1854 typeArgs,
1855 typeArgsRAngleLoc,
1856 protocolLAngleLoc,
1857 protocols,
1858 protocolLocs,
1859 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001860}
1861
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001862void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1863 BalancedDelimiterTracker &T,
1864 SmallVectorImpl<Decl *> &AllIvarDecls,
1865 bool RBraceMissing) {
1866 if (!RBraceMissing)
1867 T.consumeClose();
Fangrui Song6907ce22018-07-30 19:24:48 +00001868
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001869 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1870 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1871 Actions.ActOnObjCContainerFinishDefinition();
1872 // Call ActOnFields() even if we don't have any decls. This is useful
1873 // for code rewriting tools that need to be aware of the empty list.
Erich Keanec480f302018-07-12 21:09:05 +00001874 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl, AllIvarDecls,
1875 T.getOpenLocation(), T.getCloseLocation(),
1876 ParsedAttributesView());
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001877}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001878
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001879/// objc-class-instance-variables:
1880/// '{' objc-instance-variable-decl-list[opt] '}'
1881///
1882/// objc-instance-variable-decl-list:
1883/// objc-visibility-spec
1884/// objc-instance-variable-decl ';'
1885/// ';'
1886/// objc-instance-variable-decl-list objc-visibility-spec
1887/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
Nico Weber98dd0852019-03-14 14:18:56 +00001888/// objc-instance-variable-decl-list static_assert-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001889/// objc-instance-variable-decl-list ';'
1890///
1891/// objc-visibility-spec:
1892/// @private
1893/// @protected
1894/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001895/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001896///
1897/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001898/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001899///
John McCall48871652010-08-21 09:40:31 +00001900void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001901 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001902 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001903 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001904 SmallVector<Decl *, 32> AllIvarDecls;
Fangrui Song6907ce22018-07-30 19:24:48 +00001905
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001906 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001907 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001908
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001909 BalancedDelimiterTracker T(*this, tok::l_brace);
1910 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001911 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001912 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001913 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001914
Steve Naroff00433d32007-08-21 21:17:12 +00001915 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001916 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001917 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001918 continue;
1919 }
Mike Stump11289f42009-09-09 15:08:12 +00001920
Steve Naroff00433d32007-08-21 21:17:12 +00001921 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001922 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001923 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001924 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001925 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001926 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001927
Steve Naroff7c348172007-08-23 18:16:40 +00001928 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001929 case tok::objc_private:
1930 case tok::objc_public:
1931 case tok::objc_protected:
1932 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001933 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001934 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001935 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001936
1937 case tok::objc_end:
1938 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001939 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1940 Tok.setKind(tok::at);
1941 Tok.setLength(1);
Ilya Biryukov929af672019-05-17 09:32:05 +00001942 PP.EnterToken(Tok, /*IsReinject*/true);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001943 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1944 T, AllIvarDecls, true);
1945 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001946
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001947 default:
1948 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1949 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001950 }
1951 }
Mike Stump11289f42009-09-09 15:08:12 +00001952
Douglas Gregor48d46252010-01-13 21:54:15 +00001953 if (Tok.is(tok::code_completion)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001954 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001955 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001956 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001957 }
John McCallcfefb6d2009-11-03 02:38:08 +00001958
Nico Weber98dd0852019-03-14 14:18:56 +00001959 // This needs to duplicate a small amount of code from
1960 // ParseStructUnionBody() for things that should work in both
1961 // C struct and in Objective-C class instance variables.
1962 if (Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
1963 SourceLocation DeclEnd;
1964 ParseStaticAssertDeclaration(DeclEnd);
1965 continue;
1966 }
1967
Benjamin Kramera39beb92014-09-03 11:06:10 +00001968 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1969 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1970 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001971 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001972 Decl *Field = Actions.ActOnIvar(
1973 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1974 FD.BitfieldSize, visibility);
1975 Actions.ActOnObjCContainerFinishDefinition();
1976 if (Field)
1977 AllIvarDecls.push_back(Field);
1978 FD.complete(Field);
1979 };
John McCallcfefb6d2009-11-03 02:38:08 +00001980
Chris Lattnera12405b2008-04-10 06:46:29 +00001981 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001982 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001983 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001984
Chris Lattner0ef13522007-10-09 17:51:17 +00001985 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001986 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001987 } else {
1988 Diag(Tok, diag::err_expected_semi_decl_list);
1989 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001990 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001991 }
1992 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001993 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1994 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001995}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001996
1997/// objc-protocol-declaration:
1998/// objc-protocol-definition
1999/// objc-protocol-forward-reference
2000///
2001/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002002/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002003/// objc-protocol-refs[opt]
2004/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002005/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002006///
2007/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002008/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002009///
James Dennett1355bd12012-06-11 06:19:40 +00002010/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002011/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002012/// semicolon in the first alternative if objc-protocol-refs are omitted.
Fangrui Song6907ce22018-07-30 19:24:48 +00002013Parser::DeclGroupPtrTy
Douglas Gregorf6102672012-01-01 21:23:57 +00002014Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2015 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002016 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002017 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2018 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002019
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002020 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002021 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002022 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002023 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002024 }
2025
Nico Weber69a79142013-04-04 00:15:10 +00002026 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002027
Alex Lorenzf1278212017-04-11 15:01:53 +00002028 if (expectIdentifier())
2029 return nullptr; // missing protocol name.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002030 // Save the protocol name, then consume it.
2031 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2032 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002033
Alp Toker383d2c42014-01-01 03:08:43 +00002034 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002035 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Erich Keanec480f302018-07-12 21:09:05 +00002036 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo, attrs);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002037 }
Mike Stump11289f42009-09-09 15:08:12 +00002038
Erik Verbruggenf9887852011-12-08 09:58:43 +00002039 CheckNestedObjCContexts(AtLoc);
2040
Chris Lattner0ef13522007-10-09 17:51:17 +00002041 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002042 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002043 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2044
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002045 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002046 while (1) {
2047 ConsumeToken(); // the ','
Alex Lorenzf1278212017-04-11 15:01:53 +00002048 if (expectIdentifier()) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002049 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002050 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002051 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002052 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2053 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002054 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002055
Chris Lattner0ef13522007-10-09 17:51:17 +00002056 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002057 break;
2058 }
2059 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002060 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002061 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002062
Erich Keanec480f302018-07-12 21:09:05 +00002063 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs, attrs);
Chris Lattnerd7352d62008-07-21 22:17:28 +00002064 }
Mike Stump11289f42009-09-09 15:08:12 +00002065
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002066 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002067 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002068
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002069 SmallVector<Decl *, 8> ProtocolRefs;
2070 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002071 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002072 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002073 LAngleLoc, EndProtoLoc,
2074 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002075 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002076
Erich Keanec480f302018-07-12 21:09:05 +00002077 Decl *ProtoType = Actions.ActOnStartProtocolInterface(
2078 AtLoc, protocolName, nameLoc, ProtocolRefs.data(), ProtocolRefs.size(),
2079 ProtocolLocs.data(), EndProtoLoc, attrs);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002080
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002081 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002082 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002083}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002084
2085/// objc-implementation:
2086/// objc-class-implementation-prologue
2087/// objc-category-implementation-prologue
2088///
2089/// objc-class-implementation-prologue:
2090/// @implementation identifier objc-superclass[opt]
2091/// objc-class-instance-variables[opt]
2092///
2093/// objc-category-implementation-prologue:
2094/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002095Parser::DeclGroupPtrTy
Erik Pilkingtonc5a05832019-04-11 17:55:30 +00002096Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc,
2097 ParsedAttributes &Attrs) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002098 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2099 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002100 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002101 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002102
Douglas Gregor49c22a72009-11-18 16:26:39 +00002103 // Code completion after '@implementation'.
2104 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002105 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002106 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002107 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002108 }
2109
Nico Weber69a79142013-04-04 00:15:10 +00002110 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002111
Alex Lorenzf1278212017-04-11 15:01:53 +00002112 if (expectIdentifier())
2113 return nullptr; // missing class or category name.
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002114 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002115 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002116 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002117 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002118
Douglas Gregor85f3f952015-07-07 03:57:15 +00002119 // Neither a type parameter list nor a list of protocol references is
2120 // permitted here. Parse and diagnose them.
2121 if (Tok.is(tok::less)) {
2122 SourceLocation lAngleLoc, rAngleLoc;
2123 SmallVector<IdentifierLocPair, 8> protocolIdents;
2124 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002125 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2126 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2127 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002128 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2129 << SourceRange(diagLoc, PrevTokLocation);
2130 } else if (lAngleLoc.isValid()) {
2131 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2132 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2133 }
2134 }
2135
Mike Stump11289f42009-09-09 15:08:12 +00002136 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002137 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002138 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002139 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002140 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002141
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002142 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002143 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002144 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002145 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002146 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002147
Chris Lattner0ef13522007-10-09 17:51:17 +00002148 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002149 categoryId = Tok.getIdentifierInfo();
2150 categoryLoc = ConsumeToken();
2151 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002152 Diag(Tok, diag::err_expected)
2153 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002154 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002155 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002156 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002157 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002158 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002159 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002160 }
2161 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002162 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2163 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002164 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2165 SmallVector<Decl *, 4> protocols;
2166 SmallVector<SourceLocation, 4> protocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00002167 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002168 /*warnOnIncompleteProtocols=*/false,
2169 /*ForObjCContainer=*/false,
2170 protocolLAngleLoc, protocolRAngleLoc,
2171 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002172 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002173 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Pilkingtonc5a05832019-04-11 17:55:30 +00002174 AtLoc, nameId, nameLoc, categoryId, categoryLoc, Attrs);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002175
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002176 } else {
2177 // We have a class implementation
2178 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002179 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002180 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002181 // We have a super class
Alex Lorenzf1278212017-04-11 15:01:53 +00002182 if (expectIdentifier())
2183 return nullptr; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002184 superClassId = Tok.getIdentifierInfo();
2185 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002186 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002187 ObjCImpDecl = Actions.ActOnStartClassImplementation(
Erik Pilkingtonc5a05832019-04-11 17:55:30 +00002188 AtLoc, nameId, nameLoc, superClassId, superClassLoc, Attrs);
Fangrui Song6907ce22018-07-30 19:24:48 +00002189
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002190 if (Tok.is(tok::l_brace)) // we have ivars
2191 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002192 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2193 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002194
2195 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2196 SmallVector<Decl *, 4> protocols;
2197 SmallVector<SourceLocation, 4> protocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00002198 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002199 /*warnOnIncompleteProtocols=*/false,
2200 /*ForObjCContainer=*/false,
2201 protocolLAngleLoc, protocolRAngleLoc,
2202 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002203 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002204 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002205 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002206
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002207 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002208
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002209 {
2210 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002211 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002212 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002213 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002214 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2215 DeclGroupRef DG = DGP.get();
2216 DeclsInGroup.append(DG.begin(), DG.end());
2217 }
2218 }
2219 }
2220
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002221 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002222}
Steve Naroff33a1e802007-10-29 21:38:07 +00002223
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002224Parser::DeclGroupPtrTy
2225Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002226 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2227 "ParseObjCAtEndDeclaration(): Expected @end");
2228 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002229 if (CurParsedObjCImpl)
2230 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002231 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002232 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002233 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002234 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002235}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002236
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002237Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2238 if (!Finished) {
2239 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002240 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002241 P.Diag(P.Tok, diag::err_objc_missing_end)
2242 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002243 P.Diag(Dcl->getBeginLoc(), diag::note_objc_container_start)
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002244 << Sema::OCK_Implementation;
2245 }
2246 }
Craig Topper161e4db2014-05-21 06:02:52 +00002247 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002248 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002249}
2250
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002251void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2252 assert(!Finished);
Alex Lorenz6c9af502017-07-03 10:12:24 +00002253 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl, AtEnd.getBegin());
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002254 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fangrui Song6907ce22018-07-30 19:24:48 +00002255 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002256 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002257
2258 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2259
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002260 if (HasCFunction)
2261 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fangrui Song6907ce22018-07-30 19:24:48 +00002262 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002263 false/*c-functions*/);
Fangrui Song6907ce22018-07-30 19:24:48 +00002264
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002265 /// Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002266 for (LateParsedObjCMethodContainer::iterator
2267 I = LateParsedObjCMethods.begin(),
2268 E = LateParsedObjCMethods.end(); I != E; ++I)
2269 delete *I;
2270 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002271
2272 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002273}
2274
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002275/// compatibility-alias-decl:
2276/// @compatibility_alias alias-name class-name ';'
2277///
John McCall48871652010-08-21 09:40:31 +00002278Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002279 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2280 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2281 ConsumeToken(); // consume compatibility_alias
Alex Lorenzf1278212017-04-11 15:01:53 +00002282 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002283 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002284 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2285 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
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 *classId = Tok.getIdentifierInfo();
2289 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002290 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002291 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2292 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002293}
2294
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002295/// property-synthesis:
2296/// @synthesize property-ivar-list ';'
2297///
2298/// property-ivar-list:
2299/// property-ivar
2300/// property-ivar-list ',' property-ivar
2301///
2302/// property-ivar:
2303/// identifier
2304/// identifier '=' identifier
2305///
John McCall48871652010-08-21 09:40:31 +00002306Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002307 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002308 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002309 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002310
Douglas Gregor88e72a02009-11-18 19:45:45 +00002311 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002312 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002313 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002314 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002315 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002316 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002317
Douglas Gregor88e72a02009-11-18 19:45:45 +00002318 if (Tok.isNot(tok::identifier)) {
2319 Diag(Tok, diag::err_synthesized_property_name);
2320 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002321 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002322 }
Craig Topper161e4db2014-05-21 06:02:52 +00002323
2324 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002325 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2326 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002327 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002328 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002329 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002330 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002331 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002332 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002333 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002334 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002335
2336 if (expectIdentifier())
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002337 break;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002338 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002339 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002340 }
Manman Ren5b786402016-01-28 18:49:28 +00002341 Actions.ActOnPropertyImplDecl(
2342 getCurScope(), atLoc, propertyLoc, true,
2343 propertyId, propertyIvar, propertyIvarLoc,
2344 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002345 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002346 break;
2347 ConsumeToken(); // consume ','
2348 }
Alp Toker383d2c42014-01-01 03:08:43 +00002349 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002350 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002351}
2352
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002353/// property-dynamic:
2354/// @dynamic property-list
2355///
2356/// property-list:
2357/// identifier
2358/// property-list ',' identifier
2359///
John McCall48871652010-08-21 09:40:31 +00002360Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002361 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2362 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002363 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002364
2365 bool isClassProperty = false;
2366 if (Tok.is(tok::l_paren)) {
2367 ConsumeParen();
2368 const IdentifierInfo *II = Tok.getIdentifierInfo();
2369
2370 if (!II) {
2371 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2372 SkipUntil(tok::r_paren, StopAtSemi);
2373 } else {
2374 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2375 if (II->isStr("class")) {
2376 isClassProperty = true;
2377 if (Tok.isNot(tok::r_paren)) {
2378 Diag(Tok, diag::err_expected) << tok::r_paren;
2379 SkipUntil(tok::r_paren, StopAtSemi);
2380 } else
2381 ConsumeParen();
2382 } else {
2383 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2384 SkipUntil(tok::r_paren, StopAtSemi);
2385 }
2386 }
2387 }
2388
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002389 while (true) {
2390 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002391 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002392 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002393 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002394 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002395
2396 if (expectIdentifier()) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002397 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002398 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002399 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002400
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002401 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2402 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002403 Actions.ActOnPropertyImplDecl(
2404 getCurScope(), atLoc, propertyLoc, false,
2405 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002406 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002407 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002408
Chris Lattner0ef13522007-10-09 17:51:17 +00002409 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002410 break;
2411 ConsumeToken(); // consume ','
2412 }
Alp Toker383d2c42014-01-01 03:08:43 +00002413 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002414 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002415}
Mike Stump11289f42009-09-09 15:08:12 +00002416
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002417/// objc-throw-statement:
2418/// throw expression[opt];
2419///
John McCalldadc5752010-08-24 06:29:42 +00002420StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2421 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002422 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002423 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002424 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002425 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002426 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002427 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002428 }
2429 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002430 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002431 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002432 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002433}
2434
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002435/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002436/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002437///
John McCalldadc5752010-08-24 06:29:42 +00002438StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002439Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002440 ConsumeToken(); // consume synchronized
2441 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002442 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002443 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002444 }
John McCalld9bb7432011-07-27 21:50:02 +00002445
2446 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002447 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002448 ExprResult operand(ParseExpression());
2449
2450 if (Tok.is(tok::r_paren)) {
2451 ConsumeParen(); // ')'
2452 } else {
2453 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002454 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002455
2456 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002457 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002458 }
John McCalld9bb7432011-07-27 21:50:02 +00002459
2460 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002461 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002462 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002463 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002464 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002465 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002466
John McCalld9bb7432011-07-27 21:50:02 +00002467 // Check the @synchronized operand now.
2468 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002469 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002470
John McCalld9bb7432011-07-27 21:50:02 +00002471 // Parse the compound statement within a new scope.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002472 ParseScope bodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalld9bb7432011-07-27 21:50:02 +00002473 StmtResult body(ParseCompoundStatementBody());
2474 bodyScope.Exit();
2475
2476 // If there was a semantic or parse error earlier with the
2477 // operand, fail now.
2478 if (operand.isInvalid())
2479 return StmtError();
2480
2481 if (body.isInvalid())
2482 body = Actions.ActOnNullStmt(Tok.getLocation());
2483
2484 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002485}
2486
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002487/// objc-try-catch-statement:
2488/// @try compound-statement objc-catch-list[opt]
2489/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2490///
2491/// objc-catch-list:
2492/// @catch ( parameter-declaration ) compound-statement
2493/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2494/// catch-parameter-declaration:
2495/// parameter-declaration
2496/// '...' [OBJC2]
2497///
John McCalldadc5752010-08-24 06:29:42 +00002498StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002499 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002500
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002501 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002502 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002503 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002504 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002505 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002506 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002507 StmtResult FinallyStmt;
Momchil Velikov57c681f2017-08-10 15:43:06 +00002508 ParseScope TryScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalldadc5752010-08-24 06:29:42 +00002509 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002510 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002511 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002512 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002513
Chris Lattner0ef13522007-10-09 17:51:17 +00002514 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002515 // At this point, we need to lookahead to determine if this @ is the start
2516 // of an @catch or @finally. We don't want to consume the @ token if this
2517 // is an @try or @encode or something else.
2518 Token AfterAt = GetLookAheadToken(1);
2519 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2520 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2521 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002522
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002523 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002524 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002525 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002526 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002527 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002528 ConsumeParen();
Momchil Velikov57c681f2017-08-10 15:43:06 +00002529 ParseScope CatchScope(this, Scope::DeclScope |
2530 Scope::CompoundStmtScope |
2531 Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002532 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002533 DeclSpec DS(AttrFactory);
Faisal Valia534f072018-04-26 00:42:40 +00002534 ParseDeclarationSpecifiers(DS);
Faisal Vali421b2d12017-12-29 05:41:00 +00002535 Declarator ParmDecl(DS, DeclaratorContext::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002536 ParseDeclarator(ParmDecl);
2537
Douglas Gregore11ee112010-04-23 23:01:43 +00002538 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002539 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002540 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002541 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002542 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002543
Steve Naroff65a00892009-04-07 22:56:58 +00002544 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002545
Steve Naroff65a00892009-04-07 22:56:58 +00002546 if (Tok.is(tok::r_paren))
2547 RParenLoc = ConsumeParen();
2548 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002549 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002550
John McCalldadc5752010-08-24 06:29:42 +00002551 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002552 if (Tok.is(tok::l_brace))
2553 CatchBody = ParseCompoundStatementBody();
2554 else
Alp Tokerec543272013-12-24 09:48:30 +00002555 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002556 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002557 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00002558
John McCalldadc5752010-08-24 06:29:42 +00002559 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +00002560 RParenLoc,
2561 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002562 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002563 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002564 CatchStmts.push_back(Catch.get());
Fangrui Song6907ce22018-07-30 19:24:48 +00002565
Steve Naroffe6016792008-02-05 21:27:35 +00002566 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002567 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2568 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002569 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002570 }
2571 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002572 } else {
2573 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002574 ConsumeToken(); // consume finally
Momchil Velikov57c681f2017-08-10 15:43:06 +00002575 ParseScope FinallyScope(this,
2576 Scope::DeclScope | Scope::CompoundStmtScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002577
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002578 bool ShouldCapture =
2579 getTargetInfo().getTriple().isWindowsMSVCEnvironment();
2580 if (ShouldCapture)
2581 Actions.ActOnCapturedRegionStart(Tok.getLocation(), getCurScope(),
2582 CR_ObjCAtFinally, 1);
2583
John McCalldadc5752010-08-24 06:29:42 +00002584 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002585 if (Tok.is(tok::l_brace))
2586 FinallyBody = ParseCompoundStatementBody();
2587 else
Alp Tokerec543272013-12-24 09:48:30 +00002588 Diag(Tok, diag::err_expected) << tok::l_brace;
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002589
2590 if (FinallyBody.isInvalid()) {
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002591 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002592 if (ShouldCapture)
2593 Actions.ActOnCapturedRegionError();
2594 } else if (ShouldCapture) {
2595 FinallyBody = Actions.ActOnCapturedRegionEnd(FinallyBody.get());
2596 }
2597
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002598 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002599 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002600 catch_or_finally_seen = true;
2601 break;
2602 }
2603 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002604 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002605 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002606 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002607 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002608
2609 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002610 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002611 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002612}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002613
John McCall31168b02011-06-15 23:02:42 +00002614/// objc-autoreleasepool-statement:
2615/// @autoreleasepool compound-statement
2616///
2617StmtResult
2618Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2619 ConsumeToken(); // consume autoreleasepool
2620 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002621 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002622 return StmtError();
2623 }
2624 // Enter a scope to hold everything within the compound stmt. Compound
2625 // statements can always hold declarations.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002626 ParseScope BodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCall31168b02011-06-15 23:02:42 +00002627
2628 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2629
2630 BodyScope.Exit();
2631 if (AutoreleasePoolBody.isInvalid())
2632 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00002633 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002634 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002635}
2636
Fangrui Song6907ce22018-07-30 19:24:48 +00002637/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002638/// for later parsing.
2639void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002640 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2641 trySkippingFunctionBody()) {
2642 Actions.ActOnSkippedFunctionBody(MDecl);
2643 return;
2644 }
2645
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002646 LexedMethod* LM = new LexedMethod(this, MDecl);
2647 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2648 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002649 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002650 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002651 if (Tok.is(tok::kw_try)) {
2652 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002653 if (Tok.is(tok::colon)) {
2654 Toks.push_back(Tok);
2655 ConsumeToken();
2656 while (Tok.isNot(tok::l_brace)) {
2657 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2658 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2659 }
2660 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002661 Toks.push_back(Tok); // also store '{'
2662 }
2663 else if (Tok.is(tok::colon)) {
2664 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002665 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002666 while (Tok.isNot(tok::l_brace)) {
2667 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2668 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2669 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002670 Toks.push_back(Tok); // also store '{'
2671 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002672 ConsumeBrace();
2673 // Consume everything up to (and including) the matching right brace.
2674 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002675 while (Tok.is(tok::kw_catch)) {
2676 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2677 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2678 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002679}
2680
Steve Naroff09bf8152007-09-06 21:24:23 +00002681/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002682///
John McCall48871652010-08-21 09:40:31 +00002683Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002684 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002685
Jordan Rose1e879d82018-03-23 00:07:18 +00002686 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, MDecl, Tok.getLocation(),
John McCallfaf5fb42010-08-26 23:41:50 +00002687 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002688
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002689 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002690 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002691 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002692 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002693 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002694 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002695 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002696 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002697
Steve Naroffbb875722007-11-11 19:54:21 +00002698 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002699 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002700 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002701
Steve Naroffbb875722007-11-11 19:54:21 +00002702 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002703 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002704
Steve Naroffbb875722007-11-11 19:54:21 +00002705 // If we didn't find the '{', bail out.
2706 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002707 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002708 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002709
2710 if (!MDecl) {
2711 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002712 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002713 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002714 }
2715
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002716 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002717 Actions.AddAnyMethodToGlobalPool(MDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +00002718 assert (CurParsedObjCImpl
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002719 && "ParseObjCMethodDefinition - Method out of @implementation");
2720 // Consume the tokens and store them for later parsing.
2721 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002722 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002723}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002724
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002725StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc,
2726 ParsedStmtContext StmtCtx) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002727 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002728 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002729 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002730 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002731 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002732
Chris Lattner3ababf52009-12-07 16:33:19 +00002733 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002734 return ParseObjCTryStmt(AtLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00002735
Chris Lattner3ababf52009-12-07 16:33:19 +00002736 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002737 return ParseObjCThrowStmt(AtLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00002738
Chris Lattner3ababf52009-12-07 16:33:19 +00002739 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002740 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002741
2742 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2743 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002744
2745 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2746 getLangOpts().DebuggerSupport) {
2747 SkipUntil(tok::semi);
2748 return Actions.ActOnNullStmt(Tok.getLocation());
2749 }
2750
Alex Lorenza589abc2016-12-01 12:14:38 +00002751 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002752 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002753 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002754 // If the expression is invalid, skip ahead to the next semicolon. Not
2755 // doing this opens us up to the possibility of infinite loops if
2756 // ParseExpression does not consume any tokens.
2757 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002758 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002759 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002760
Steve Naroffe6016792008-02-05 21:27:35 +00002761 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002762 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smitha6e8d5e2019-02-15 00:27:53 +00002763 return handleExprStmt(Res, StmtCtx);
Steve Naroffe6016792008-02-05 21:27:35 +00002764}
2765
John McCalldadc5752010-08-24 06:29:42 +00002766ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002767 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002768 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002769 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002770 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002771 return ExprError();
2772
Ted Kremeneke65b0862012-03-06 20:05:56 +00002773 case tok::minus:
2774 case tok::plus: {
2775 tok::TokenKind Kind = Tok.getKind();
2776 SourceLocation OpLoc = ConsumeToken();
2777
2778 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002779 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002780 switch (Kind) {
2781 case tok::minus: Symbol = "-"; break;
2782 case tok::plus: Symbol = "+"; break;
2783 default: llvm_unreachable("missing unary operator case");
2784 }
2785 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2786 << Symbol;
2787 return ExprError();
2788 }
2789
2790 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2791 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002792 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002793 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002794 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002795
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002796 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002797 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002798 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002799
2800 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002801 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002802 }
2803
Chris Lattnere002fbe2007-12-12 01:04:12 +00002804 case tok::string_literal: // primary-expression: string-literal
2805 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002806 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002807
2808 case tok::char_constant:
2809 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002810
Ted Kremeneke65b0862012-03-06 20:05:56 +00002811 case tok::numeric_constant:
2812 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2813
2814 case tok::kw_true: // Objective-C++, etc.
2815 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2816 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2817 case tok::kw_false: // Objective-C++, etc.
2818 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2819 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
Fangrui Song6907ce22018-07-30 19:24:48 +00002820
Ted Kremeneke65b0862012-03-06 20:05:56 +00002821 case tok::l_square:
2822 // Objective-C array literal
2823 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002824
Ted Kremeneke65b0862012-03-06 20:05:56 +00002825 case tok::l_brace:
2826 // Objective-C dictionary literal
2827 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002828
Patrick Beard0caa3942012-04-19 00:25:12 +00002829 case tok::l_paren:
2830 // Objective-C boxed expression
2831 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002832
Chris Lattnere002fbe2007-12-12 01:04:12 +00002833 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002834 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002835 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002836
Chris Lattner197a3012008-08-05 06:19:09 +00002837 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2838 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002839 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002840 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002841 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002842 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002843 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002844 case tok::objc_available:
2845 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002846 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002847 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002848 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2849 // that this is a proper statement where such directives could actually
2850 // occur.
2851 if (GetLookAheadToken(1).is(tok::l_brace) &&
2852 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002853 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
Fangrui Song6907ce22018-07-30 19:24:48 +00002854 str =
2855 ch == 't' ? "try"
2856 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002857 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002858 }
2859 if (str) {
2860 SourceLocation kwLoc = Tok.getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002861 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002862 FixItHint::CreateReplacement(kwLoc, str));
2863 }
2864 else
2865 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2866 }
Chris Lattner197a3012008-08-05 06:19:09 +00002867 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002868 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002869}
2870
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002871/// Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002872///
2873/// This routine parses the receiver of a message send in
2874/// Objective-C++ either as a type or as an expression. Note that this
2875/// routine must not be called to parse a send to 'super', since it
2876/// has no way to return such a result.
Fangrui Song6907ce22018-07-30 19:24:48 +00002877///
Douglas Gregor8d4de672010-04-21 22:36:40 +00002878/// \param IsExpr Whether the receiver was parsed as an expression.
2879///
2880/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2881/// IsExpr is true), the parsed expression. If the receiver was parsed
2882/// as a type (\c IsExpr is false), the parsed type.
2883///
2884/// \returns True if an error occurred during parsing or semantic
2885/// analysis, in which case the arguments do not have valid
2886/// values. Otherwise, returns false for a successful parse.
2887///
2888/// objc-receiver: [C++]
2889/// 'super' [not parsed here]
2890/// expression
2891/// simple-type-specifier
2892/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002893bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002894 InMessageExpressionRAIIObject InMessage(*this, true);
2895
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002896 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2897 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002898 TryAnnotateTypeOrScopeToken();
2899
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002900 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002901 // objc-receiver:
2902 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002903 // Make sure any typos in the receiver are corrected or diagnosed, so that
2904 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2905 // only the things that are valid ObjC receivers?
2906 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002907 if (Receiver.isInvalid())
2908 return true;
2909
2910 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002911 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002912 return false;
2913 }
2914
2915 // objc-receiver:
2916 // typename-specifier
2917 // simple-type-specifier
2918 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002919 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002920 ParseCXXSimpleTypeSpecifier(DS);
Fangrui Song6907ce22018-07-30 19:24:48 +00002921
Douglas Gregor8d4de672010-04-21 22:36:40 +00002922 if (Tok.is(tok::l_paren)) {
2923 // If we see an opening parentheses at this point, we are
2924 // actually parsing an expression that starts with a
2925 // function-style cast, e.g.,
2926 //
2927 // postfix-expression:
2928 // simple-type-specifier ( expression-list [opt] )
2929 // typename-specifier ( expression-list [opt] )
2930 //
2931 // Parse the remainder of this case, then the (optional)
2932 // postfix-expression suffix, followed by the (optional)
2933 // right-hand side of the binary expression. We have an
2934 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002935 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002936 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002937 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002938 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002939 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002940 if (Receiver.isInvalid())
2941 return true;
2942
2943 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002944 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002945 return false;
2946 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002947
Douglas Gregor8d4de672010-04-21 22:36:40 +00002948 // We have a class message. Turn the simple-type-specifier or
2949 // typename-specifier we parsed into a type and parse the
2950 // remainder of the class message.
Faisal Vali421b2d12017-12-29 05:41:00 +00002951 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002952 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002953 if (Type.isInvalid())
2954 return true;
2955
2956 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002957 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002958 return false;
2959}
2960
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002961/// Determine whether the parser is currently referring to a an
Douglas Gregor990ccac2010-05-31 14:40:22 +00002962/// Objective-C message send, using a simplified heuristic to avoid overhead.
2963///
2964/// This routine will only return true for a subset of valid message-send
2965/// expressions.
2966bool Parser::isSimpleObjCMessageExpression() {
Erik Pilkingtonfa983902018-10-30 20:31:30 +00002967 assert(Tok.is(tok::l_square) && getLangOpts().ObjC &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002968 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002969 return GetLookAheadToken(1).is(tok::identifier) &&
2970 GetLookAheadToken(2).is(tok::identifier);
2971}
2972
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002973bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
Erik Pilkingtonfa983902018-10-30 20:31:30 +00002974 if (!getLangOpts().ObjC || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002975 InMessageExpression)
2976 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002977
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002978 ParsedType Type;
2979
Fangrui Song6907ce22018-07-30 19:24:48 +00002980 if (Tok.is(tok::annot_typename))
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002981 Type = getTypeAnnotation(Tok);
2982 else if (Tok.is(tok::identifier))
Fangrui Song6907ce22018-07-30 19:24:48 +00002983 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002984 getCurScope());
2985 else
2986 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002987
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002988 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2989 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002990 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002991 if (Tok.is(tok::identifier))
2992 TryAnnotateTypeOrScopeToken();
Fangrui Song6907ce22018-07-30 19:24:48 +00002993
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002994 return Tok.is(tok::annot_typename);
2995 }
2996 }
2997
2998 return false;
2999}
3000
Mike Stump11289f42009-09-09 15:08:12 +00003001/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003002/// '[' objc-receiver objc-message-args ']'
3003///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003004/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003005/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003006/// expression
3007/// class-name
3008/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003009///
John McCalldadc5752010-08-24 06:29:42 +00003010ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003011 assert(Tok.is(tok::l_square) && "'[' expected");
3012 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3013
Douglas Gregora817a192010-05-27 23:06:34 +00003014 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003015 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003016 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003017 return ExprError();
3018 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003019
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003020 InMessageExpressionRAIIObject InMessage(*this, true);
Fangrui Song6907ce22018-07-30 19:24:48 +00003021
David Blaikiebbafb8a2012-03-11 07:00:24 +00003022 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003023 // We completely separate the C and C++ cases because C++ requires
Fangrui Song6907ce22018-07-30 19:24:48 +00003024 // more complicated (read: slower) parsing.
3025
3026 // Handle send to super.
Douglas Gregor8d4de672010-04-21 22:36:40 +00003027 // FIXME: This doesn't benefit from the same typo-correction we
3028 // get in Objective-C.
3029 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003030 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003031 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3032 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003033
3034 // Parse the receiver, which is either a type or an expression.
3035 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003036 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003037 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003038 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003039 return ExprError();
3040 }
3041
3042 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003043 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3044 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003045
Fangrui Song6907ce22018-07-30 19:24:48 +00003046 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003047 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003048 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003049 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003050
Chris Lattner47054fb2010-05-31 18:18:22 +00003051 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003052 IdentifierInfo *Name = Tok.getIdentifierInfo();
3053 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003054 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003055 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003056 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003057 NextToken().is(tok::period),
3058 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003059 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003060 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3061 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003062
John McCallfaf5fb42010-08-26 23:41:50 +00003063 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003064 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003065 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003066 return ExprError();
3067 }
3068
Douglas Gregore5798dc2010-04-21 20:38:13 +00003069 ConsumeToken(); // the type name
3070
Douglas Gregore83b9562015-07-07 03:57:53 +00003071 // Parse type arguments and protocol qualifiers.
3072 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003073 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003074 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003075 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3076 /*consumeLastToken=*/true,
3077 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003078 if (!NewReceiverType.isUsable()) {
3079 SkipUntil(tok::r_square, StopAtSemi);
3080 return ExprError();
3081 }
3082
3083 ReceiverType = NewReceiverType.get();
3084 }
3085
Fangrui Song6907ce22018-07-30 19:24:48 +00003086 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003087 ReceiverType, nullptr);
3088
John McCallfaf5fb42010-08-26 23:41:50 +00003089 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003090 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003091 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003092 }
Chris Lattner8f697062008-01-25 18:59:06 +00003093 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003094
Chris Lattnera36ec422010-04-11 08:28:14 +00003095 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003096 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003097 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003098 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003099 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003100 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003101
David Blaikieefdccaa2016-01-15 23:43:34 +00003102 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3103 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003104}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003105
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003106/// Parse the remainder of an Objective-C message following the
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003107/// '[' objc-receiver.
3108///
3109/// This routine handles sends to super, class messages (sent to a
3110/// class name), and instance messages (sent to an object), and the
3111/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3112/// ReceiverExpr, respectively. Only one of these parameters may have
3113/// a valid value.
3114///
3115/// \param LBracLoc The location of the opening '['.
3116///
3117/// \param SuperLoc If this is a send to 'super', the location of the
3118/// 'super' keyword that indicates a send to the superclass.
3119///
3120/// \param ReceiverType If this is a class message, the type of the
3121/// class we are sending a message to.
3122///
3123/// \param ReceiverExpr If this is an instance message, the expression
3124/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003125///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003126/// objc-message-args:
3127/// objc-selector
3128/// objc-keywordarg-list
3129///
3130/// objc-keywordarg-list:
3131/// objc-keywordarg
3132/// objc-keywordarg-list objc-keywordarg
3133///
Mike Stump11289f42009-09-09 15:08:12 +00003134/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003135/// selector-name[opt] ':' objc-keywordexpr
3136///
3137/// objc-keywordexpr:
3138/// nonempty-expr-list
3139///
3140/// nonempty-expr-list:
3141/// assignment-expression
3142/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003143///
John McCalldadc5752010-08-24 06:29:42 +00003144ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003145Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003146 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003147 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003148 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003149 InMessageExpressionRAIIObject InMessage(*this, true);
3150
Steve Naroffeae65032009-11-07 02:08:14 +00003151 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003152 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003153 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003154 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003155 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003156 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003157 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003158 else
John McCallb268a282010-08-23 23:25:46 +00003159 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003160 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003161 cutOffParsing();
3162 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003163 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003164
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003165 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003166 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003167 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fangrui Song6907ce22018-07-30 19:24:48 +00003168
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003169 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003170 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003171 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003172
Chris Lattner0ef13522007-10-09 17:51:17 +00003173 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003174 while (1) {
3175 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003176 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003177 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003178
Alp Toker383d2c42014-01-01 03:08:43 +00003179 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003180 // We must manually skip to a ']', otherwise the expression skipper will
3181 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3182 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003183 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003184 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003185 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003186
Mike Stump11289f42009-09-09 15:08:12 +00003187 /// Parse the expression after ':'
Fangrui Song6907ce22018-07-30 19:24:48 +00003188
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003189 if (Tok.is(tok::code_completion)) {
3190 if (SuperLoc.isValid())
Fangrui Song6907ce22018-07-30 19:24:48 +00003191 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003192 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003193 /*AtArgumentExpression=*/true);
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003194 else if (ReceiverType)
3195 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
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
3199 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
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
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003203 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003204 return ExprError();
3205 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003206
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003207 ExprResult Expr;
3208 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3209 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3210 Expr = ParseBraceInitializer();
3211 } else
3212 Expr = ParseAssignmentExpression();
Fangrui Song6907ce22018-07-30 19:24:48 +00003213
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003214 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003215 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003216 // We must manually skip to a ']', otherwise the expression skipper will
3217 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3218 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003219 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003220 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003221 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003222
Steve Naroff486760a2007-09-17 20:25:27 +00003223 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003224 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003225
Douglas Gregor1b605f72009-11-19 01:08:35 +00003226 // Code completion after each argument.
3227 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003228 if (SuperLoc.isValid())
Fangrui Song6907ce22018-07-30 19:24:48 +00003229 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003230 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003231 /*AtArgumentExpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003232 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003233 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003234 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003235 /*AtArgumentExpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003236 else
John McCallb268a282010-08-23 23:25:46 +00003237 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003238 KeyIdents,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003239 /*AtArgumentExpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003240 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003241 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003242 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003243
Steve Naroff486760a2007-09-17 20:25:27 +00003244 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003245 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003246 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003247 break;
3248 // We have a selector or a colon, continue parsing.
3249 }
3250 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003251 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003252 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003253 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003254 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003255 if (Tok.is(tok::colon))
3256 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003257 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003258 if (Tok.is(tok::colon)) {
3259 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3260 FixItHint::CreateRemoval(commaLoc);
3261 }
Chris Lattner197a3012008-08-05 06:19:09 +00003262 // We must manually skip to a ']', otherwise the expression skipper will
3263 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3264 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003265 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003266 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003267 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003268
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003269 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003270 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003271 }
3272 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003273 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003274
Chris Lattner197a3012008-08-05 06:19:09 +00003275 // We must manually skip to a ']', otherwise the expression skipper will
3276 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3277 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003278 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003279 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003280 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003281
Chris Lattner0ef13522007-10-09 17:51:17 +00003282 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003283 Diag(Tok, diag::err_expected)
3284 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003285 // We must manually skip to a ']', otherwise the expression skipper will
3286 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3287 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003288 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003289 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003290 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003291
Chris Lattner8f697062008-01-25 18:59:06 +00003292 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003293
Steve Naroffe61bfa82007-10-05 18:42:47 +00003294 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003295 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003296 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003297 KeyLocs.push_back(Loc);
3298 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003299 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003300
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003301 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003302 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003303 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003304 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003305 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003306 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003307 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003308 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003309}
3310
John McCalldadc5752010-08-24 06:29:42 +00003311ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3312 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003313 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003314
Chris Lattnere002fbe2007-12-12 01:04:12 +00003315 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3316 // expressions. At this point, we know that the only valid thing that starts
3317 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003318 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003319 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003320 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003321 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003322
Chris Lattnere002fbe2007-12-12 01:04:12 +00003323 while (Tok.is(tok::at)) {
3324 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003325
Sebastian Redlc13f2682008-12-09 20:22:58 +00003326 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003327 if (!isTokenStringLiteral())
3328 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003329
John McCalldadc5752010-08-24 06:29:42 +00003330 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003331 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003332 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003333
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003334 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003335 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003336
Craig Topper883dd332015-12-24 23:58:11 +00003337 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003338}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003339
Ted Kremeneke65b0862012-03-06 20:05:56 +00003340/// ParseObjCBooleanLiteral -
3341/// objc-scalar-literal : '@' boolean-keyword
3342/// ;
3343/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3344/// ;
Fangrui Song6907ce22018-07-30 19:24:48 +00003345ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003346 bool ArgValue) {
3347 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3348 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3349}
3350
3351/// ParseObjCCharacterLiteral -
3352/// objc-scalar-literal : '@' character-literal
3353/// ;
3354ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3355 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3356 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003357 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003358 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003359 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003360 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003361}
3362
3363/// ParseObjCNumericLiteral -
3364/// objc-scalar-literal : '@' scalar-literal
3365/// ;
3366/// scalar-literal : | numeric-constant /* any numeric constant. */
3367/// ;
3368ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3369 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3370 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003371 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003372 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003373 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003374 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003375}
3376
Patrick Beard0caa3942012-04-19 00:25:12 +00003377/// ParseObjCBoxedExpr -
3378/// objc-box-expression:
3379/// @( assignment-expression )
3380ExprResult
3381Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3382 if (Tok.isNot(tok::l_paren))
3383 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3384
3385 BalancedDelimiterTracker T(*this, tok::l_paren);
3386 T.consumeOpen();
3387 ExprResult ValueExpr(ParseAssignmentExpression());
3388 if (T.consumeClose())
3389 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003390
3391 if (ValueExpr.isInvalid())
3392 return ExprError();
3393
Patrick Beard0caa3942012-04-19 00:25:12 +00003394 // Wrap the sub-expression in a parenthesized expression, to distinguish
3395 // a boxed expression from a literal.
3396 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003397 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003398 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003399 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003400}
3401
Ted Kremeneke65b0862012-03-06 20:05:56 +00003402ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003403 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003404 ConsumeBracket(); // consume the l_square.
3405
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003406 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003407 while (Tok.isNot(tok::r_square)) {
3408 // Parse list of array element expressions (all must be id types).
3409 ExprResult Res(ParseAssignmentExpression());
3410 if (Res.isInvalid()) {
3411 // We must manually skip to a ']', otherwise the expression skipper will
3412 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3413 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003414 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003415 return Res;
Fangrui Song6907ce22018-07-30 19:24:48 +00003416 }
3417
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003418 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3419 if (Res.isInvalid())
3420 HasInvalidEltExpr = true;
3421
Ted Kremeneke65b0862012-03-06 20:05:56 +00003422 // Parse the ellipsis that indicates a pack expansion.
3423 if (Tok.is(tok::ellipsis))
Fangrui Song6907ce22018-07-30 19:24:48 +00003424 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003425 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003426 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003427
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003428 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003429
3430 if (Tok.is(tok::comma))
3431 ConsumeToken(); // Eat the ','.
3432 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003433 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3434 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003435 }
3436 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003437
3438 if (HasInvalidEltExpr)
3439 return ExprError();
3440
Benjamin Kramerf0623432012-08-23 22:51:59 +00003441 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003442 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003443}
3444
3445ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3446 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3447 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003448 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003449 while (Tok.isNot(tok::r_brace)) {
3450 // Parse the comma separated key : value expressions.
3451 ExprResult KeyExpr;
3452 {
3453 ColonProtectionRAIIObject X(*this);
3454 KeyExpr = ParseAssignmentExpression();
3455 if (KeyExpr.isInvalid()) {
3456 // We must manually skip to a '}', otherwise the expression skipper will
3457 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3458 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003459 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003460 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003461 }
3462 }
3463
Alp Toker383d2c42014-01-01 03:08:43 +00003464 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003465 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003466 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003467 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003468
Ted Kremeneke65b0862012-03-06 20:05:56 +00003469 ExprResult ValueExpr(ParseAssignmentExpression());
3470 if (ValueExpr.isInvalid()) {
3471 // We must manually skip to a '}', otherwise the expression skipper will
3472 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3473 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003474 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003475 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003476 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003477
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003478 // Check the key and value for possible typos
3479 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3480 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3481 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3482 HasInvalidEltExpr = true;
3483
3484 // Parse the ellipsis that designates this as a pack expansion. Do not
3485 // ActOnPackExpansion here, leave it to template instantiation time where
3486 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003487 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003488 if (getLangOpts().CPlusPlus)
3489 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3490
Ted Kremeneke65b0862012-03-06 20:05:56 +00003491 // We have a valid expression. Collect it in a vector so we can
3492 // build the argument list.
Fangrui Song6907ce22018-07-30 19:24:48 +00003493 ObjCDictionaryElement Element = {
3494 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003495 };
3496 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003497
3498 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003499 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3500 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003501 }
3502 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003503
3504 if (HasInvalidEltExpr)
3505 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003506
Ted Kremeneke65b0862012-03-06 20:05:56 +00003507 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003508 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003509 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003510}
3511
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003512/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003513/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003514ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003515Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003516 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003517
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003518 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003519
Chris Lattner197a3012008-08-05 06:19:09 +00003520 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003521 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3522
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003523 BalancedDelimiterTracker T(*this, tok::l_paren);
3524 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003525
Douglas Gregor220cac52009-02-18 17:45:20 +00003526 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003527
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003528 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003529
Douglas Gregor220cac52009-02-18 17:45:20 +00003530 if (Ty.isInvalid())
3531 return ExprError();
3532
Nico Webera7c7e602012-12-31 00:28:03 +00003533 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3534 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003535}
Anders Carlssone01493d2007-08-23 15:25:28 +00003536
3537/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003538/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003539ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003540Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003541 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003542
Chris Lattner197a3012008-08-05 06:19:09 +00003543 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003544 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3545
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003546 BalancedDelimiterTracker T(*this, tok::l_paren);
3547 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003548
Alex Lorenzf1278212017-04-11 15:01:53 +00003549 if (expectIdentifier())
3550 return ExprError();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003551
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003552 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003553 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003554
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003555 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003556
Nico Webera7c7e602012-12-31 00:28:03 +00003557 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3558 T.getOpenLocation(), ProtoIdLoc,
3559 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003560}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003561
3562/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003563/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003564ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003565 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003566
Chris Lattner197a3012008-08-05 06:19:09 +00003567 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003568 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3569
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003570 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003571 SourceLocation sLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00003572
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003573 BalancedDelimiterTracker T(*this, tok::l_paren);
3574 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003575 bool HasOptionalParen = Tok.is(tok::l_paren);
3576 if (HasOptionalParen)
3577 ConsumeParen();
Fangrui Song6907ce22018-07-30 19:24:48 +00003578
Douglas Gregor67c692c2010-08-26 15:07:07 +00003579 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003580 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003581 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003582 return ExprError();
3583 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003584
Chris Lattner4f472a32009-04-11 18:13:45 +00003585 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003586 if (!SelIdent && // missing selector name.
3587 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003588 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003589
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003590 KeyIdents.push_back(SelIdent);
Fangrui Song6907ce22018-07-30 19:24:48 +00003591
Steve Naroff152dd812007-12-05 22:21:29 +00003592 unsigned nColons = 0;
3593 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003594 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003595 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003596 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003597 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003598 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3599 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003600 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003601
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003602 if (Tok.is(tok::r_paren))
3603 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00003604
Douglas Gregor67c692c2010-08-26 15:07:07 +00003605 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003606 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003607 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003608 return ExprError();
3609 }
3610
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003611 // Check for another keyword selector.
3612 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003613 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003614 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003615 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003616 break;
3617 }
Steve Naroff152dd812007-12-05 22:21:29 +00003618 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003619 if (HasOptionalParen && Tok.is(tok::r_paren))
3620 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003621 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003622 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003623 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3624 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003625 T.getCloseLocation(),
3626 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003627}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003628
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003629void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3630 // MCDecl might be null due to error in method or c-function prototype, etc.
3631 Decl *MCDecl = LM.D;
Fangrui Song6907ce22018-07-30 19:24:48 +00003632 bool skip = MCDecl &&
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003633 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3634 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3635 if (skip)
3636 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003637
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003638 // Save the current token position.
3639 SourceLocation OrigLoc = Tok.getLocation();
3640
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003641 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
Alex Lorenz812012f2017-06-19 17:53:21 +00003642 // Store an artificial EOF token to ensure that we don't run off the end of
3643 // the method's body when we come to parse it.
3644 Token Eof;
3645 Eof.startToken();
3646 Eof.setKind(tok::eof);
3647 Eof.setEofData(MCDecl);
3648 Eof.setLocation(OrigLoc);
3649 LM.Toks.push_back(Eof);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003650 // Append the current token at the end of the new token stream so that it
3651 // doesn't get lost.
3652 LM.Toks.push_back(Tok);
Ilya Biryukov929af672019-05-17 09:32:05 +00003653 PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true);
David Blaikie2eabcc92016-02-09 18:52:09 +00003654
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003655 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003656 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00003657
3658 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003659 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003660 // Enter a scope for the method or c-function body.
Momchil Velikov57c681f2017-08-10 15:43:06 +00003661 ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
3662 Scope::FnScope | Scope::DeclScope |
3663 Scope::CompoundStmtScope);
3664
Fangrui Song6907ce22018-07-30 19:24:48 +00003665 // Tell the actions module that we have entered a method or c-function definition
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003666 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003667 if (parseMethod)
3668 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3669 else
3670 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003671 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003672 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003673 else {
3674 if (Tok.is(tok::colon))
3675 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003676 else
3677 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003678 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003679 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003680
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003681 if (Tok.getLocation() != OrigLoc) {
3682 // Due to parsing error, we either went over the cached tokens or
3683 // there are still cached tokens left. If it's the latter case skip the
3684 // leftover tokens.
3685 // Since this is an uncommon situation that should be avoided, use the
3686 // expensive isBeforeInTranslationUnit call.
3687 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3688 OrigLoc))
3689 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3690 ConsumeAnyToken();
3691 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003692 // Clean up the remaining EOF token.
3693 ConsumeAnyToken();
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003694}