blob: a36d2ea1b416789eb53cd3f84fe46948b567627e [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.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"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#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"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023using namespace clang;
24
Nico Weber04e213b2013-04-03 17:36:11 +000025/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000026void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000027 ParsedAttributes attrs(AttrFactory);
28 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000029 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
30 Diag(Tok, diag::err_objc_postfix_attribute_hint)
31 << (Kind == tok::objc_protocol);
32 else
33 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000034 ParseGNUAttributes(attrs);
35 }
36}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000037
Chris Lattner3a907162008-12-08 21:53:24 +000038/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000039/// external-declaration: [C99 6.9]
40/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000041/// [OBJC] objc-class-declaration
42/// [OBJC] objc-alias-declaration
43/// [OBJC] objc-protocol-definition
44/// [OBJC] objc-method-definition
45/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000046Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000047 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000048
Douglas Gregorf48706c2009-12-07 09:27:33 +000049 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000050 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000051 cutOffParsing();
52 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000053 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000054
55 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000056 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000057 case tok::objc_class:
58 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000059 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000060 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
62 break;
John McCall53fa7142010-12-24 02:08:15 +000063 }
64 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000065 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000066 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000067 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000068 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000069 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000070 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000071 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000072 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000073 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
74 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000075 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000076 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
77 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000078 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000079 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
80 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000081 case tok::objc_import:
David Blaikiebbafb8a2012-03-11 07:00:24 +000082 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000083 return ParseModuleImport(AtLoc);
84
85 // Fall through
86
Chris Lattnerce90ef52008-08-23 02:02:23 +000087 default:
88 Diag(AtLoc, diag::err_unexpected_at);
89 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000090 SingleDecl = 0;
91 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
96///
Mike Stump11289f42009-09-09 15:08:12 +000097/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000099///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000100Parser::DeclGroupPtrTy
101Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000102 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000103 SmallVector<IdentifierInfo *, 8> ClassNames;
104 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +0000105
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000107 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000108 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000109 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000110 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000111 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000112 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000113 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000114 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000115 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000116 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner0ef13522007-10-09 17:51:17 +0000118 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000119 break;
Mike Stump11289f42009-09-09 15:08:12 +0000120
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000121 ConsumeToken();
122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000124 // Consume the ';'.
125 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000126 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000127
Ted Kremeneka26da852009-11-17 23:12:20 +0000128 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
129 ClassLocs.data(),
130 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000131}
132
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000133void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
134{
135 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
136 if (ock == Sema::OCK_None)
137 return;
138
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000139 Decl *Decl = Actions.getObjCDeclContext();
140 if (CurParsedObjCImpl) {
141 CurParsedObjCImpl->finish(AtLoc);
142 } else {
143 Actions.ActOnAtEnd(getCurScope(), AtLoc);
144 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000145 Diag(AtLoc, diag::err_objc_missing_end)
146 << FixItHint::CreateInsertion(AtLoc, "@end\n");
147 if (Decl)
148 Diag(Decl->getLocStart(), diag::note_objc_container_start)
149 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000150}
151
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000152///
153/// objc-interface:
154/// objc-class-interface-attributes[opt] objc-class-interface
155/// objc-category-interface
156///
157/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000158/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000159/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000160/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000161/// objc-interface-decl-list
162/// @end
163///
164/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000165/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000166/// objc-protocol-refs[opt]
167/// objc-interface-decl-list
168/// @end
169///
170/// objc-superclass:
171/// ':' identifier
172///
173/// objc-class-interface-attributes:
174/// __attribute__((visibility("default")))
175/// __attribute__((visibility("hidden")))
176/// __attribute__((deprecated))
177/// __attribute__((unavailable))
178/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000179/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000181Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000182 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000183 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000184 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000185 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000186 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000187
Douglas Gregor49c22a72009-11-18 16:26:39 +0000188 // Code completion after '@interface'.
189 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000190 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000191 cutOffParsing();
192 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000193 }
194
Nico Weber69a79142013-04-04 00:15:10 +0000195 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000196
Chris Lattner0ef13522007-10-09 17:51:17 +0000197 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000198 Diag(Tok, diag::err_expected)
199 << tok::identifier; // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000200 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000201 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000202
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000204 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000205 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000206 if (Tok.is(tok::l_paren) &&
207 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000208
209 BalancedDelimiterTracker T(*this, tok::l_paren);
210 T.consumeOpen();
211
212 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000213 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000214 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000215 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000216 cutOffParsing();
217 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000218 }
219
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000220 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000221 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000222 categoryId = Tok.getIdentifierInfo();
223 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000224 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000225 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000226 Diag(Tok, diag::err_expected)
227 << tok::identifier; // missing category name.
John McCall48871652010-08-21 09:40:31 +0000228 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000229 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000230
231 T.consumeClose();
232 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000233 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000234
235 if (!attrs.empty()) { // categories don't support attributes.
236 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
237 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000238 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000239
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000240 // Next, we need to check for any protocol references.
241 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242 SmallVector<Decl *, 8> ProtocolRefs;
243 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000244 if (Tok.is(tok::less) &&
245 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000246 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000247 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000248
John McCall48871652010-08-21 09:40:31 +0000249 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000250 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000251 nameId, nameLoc,
252 categoryId, categoryLoc,
253 ProtocolRefs.data(),
254 ProtocolRefs.size(),
255 ProtocolLocs.data(),
256 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000257
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000258 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000259 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000260
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000261 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000262 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000263 }
264 // Parse a class interface.
265 IdentifierInfo *superClassId = 0;
266 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000267
Chris Lattner0ef13522007-10-09 17:51:17 +0000268 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000269 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000270
271 // Code completion of superclass names.
272 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000273 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000274 cutOffParsing();
275 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000276 }
277
Chris Lattner0ef13522007-10-09 17:51:17 +0000278 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000279 Diag(Tok, diag::err_expected)
280 << tok::identifier; // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000281 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000282 }
283 superClassId = Tok.getIdentifierInfo();
284 superClassLoc = ConsumeToken();
285 }
286 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000287 SmallVector<Decl *, 8> ProtocolRefs;
288 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000289 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000290 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000291 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
292 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000293 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000294
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000295 if (Tok.isNot(tok::less))
296 Actions.ActOnTypedefedProtocols(ProtocolRefs, superClassId, superClassLoc);
297
John McCall48871652010-08-21 09:40:31 +0000298 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000299 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000300 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000301 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000302 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000303 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattner0ef13522007-10-09 17:51:17 +0000305 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000306 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000307
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000308 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000309 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000310}
311
John McCall064d77b2009-12-03 22:31:13 +0000312/// The Objective-C property callback. This should be defined where
313/// it's used, but instead it's been lifted to here to support VS2005.
314struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie68e081d2011-12-20 02:48:34 +0000315private:
316 virtual void anchor();
317public:
John McCall064d77b2009-12-03 22:31:13 +0000318 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000319 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000320 ObjCDeclSpec &OCDS;
321 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000322 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000323 tok::ObjCKeywordKind MethodImplKind;
324
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000325 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000326 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000327 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000328 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000329 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000330 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000331 MethodImplKind(MethodImplKind) {
332 }
333
Eli Friedman934dbbf2012-08-08 23:53:27 +0000334 void invoke(ParsingFieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000335 if (FD.D.getIdentifier() == 0) {
336 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
337 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000338 return;
John McCall064d77b2009-12-03 22:31:13 +0000339 }
340 if (FD.BitfieldSize) {
341 P.Diag(AtLoc, diag::err_objc_property_bitfield)
342 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000343 return;
John McCall064d77b2009-12-03 22:31:13 +0000344 }
345
346 // Install the property declarator into interfaceDecl.
347 IdentifierInfo *SelName =
348 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
349
350 Selector GetterSel =
351 P.PP.getSelectorTable().getNullarySelector(SelName);
352 IdentifierInfo *SetterName = OCDS.getSetterName();
353 Selector SetterSel;
354 if (SetterName)
355 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
356 else
Adrian Prantla4ce9062013-06-07 22:29:12 +0000357 SetterSel =
358 SelectorTable::constructSetterSelector(P.PP.getIdentifierTable(),
359 P.PP.getSelectorTable(),
360 FD.D.getIdentifier());
John McCall064d77b2009-12-03 22:31:13 +0000361 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000362 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000363 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
364 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000365 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000366 &isOverridingProperty,
367 MethodImplKind);
368 if (!isOverridingProperty)
369 Props.push_back(Property);
370
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000371 FD.complete(Property);
John McCall064d77b2009-12-03 22:31:13 +0000372 }
373};
374
David Blaikie68e081d2011-12-20 02:48:34 +0000375void Parser::ObjCPropertyCallback::anchor() {
376}
377
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000378/// objc-interface-decl-list:
379/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000380/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000381/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000382/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000383/// objc-interface-decl-list declaration
384/// objc-interface-decl-list ';'
385///
Steve Naroff99264b42007-08-22 16:35:03 +0000386/// objc-method-requirement: [OBJC2]
387/// @required
388/// @optional
389///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000390void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
391 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000392 SmallVector<Decl *, 32> allMethods;
393 SmallVector<Decl *, 16> allProperties;
394 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000395 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenekc7c64312010-01-07 01:20:12 +0000397 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000398
Steve Naroff99264b42007-08-22 16:35:03 +0000399 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000400 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000401 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000402 if (Decl *methodPrototype =
403 ParseObjCMethodPrototype(MethodImplKind, false))
404 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000405 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
406 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000407 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
408 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000409 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000410 if (Tok.is(tok::semi))
411 ConsumeToken();
412 }
Steve Naroff99264b42007-08-22 16:35:03 +0000413 continue;
414 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000415 if (Tok.is(tok::l_paren)) {
416 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000417 ParseObjCMethodDecl(Tok.getLocation(),
418 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000419 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000420 continue;
421 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000422 // Ignore excess semicolons.
423 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000424 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000425 continue;
426 }
Mike Stump11289f42009-09-09 15:08:12 +0000427
Chris Lattnerda9fb152008-10-20 06:10:06 +0000428 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000429 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000430 break;
Mike Stump11289f42009-09-09 15:08:12 +0000431
Douglas Gregorf1934162010-01-13 21:24:21 +0000432 // Code completion within an Objective-C interface.
433 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000434 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000435 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000436 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000437 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000438 }
439
Chris Lattner038a3e32008-10-20 05:46:22 +0000440 // If we don't have an @ directive, parse it as a function definition.
441 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000442 // The code below does not consume '}'s because it is afraid of eating the
443 // end of a namespace. Because of the way this code is structured, an
444 // erroneous r_brace would cause an infinite loop if not handled here.
445 if (Tok.is(tok::r_brace))
446 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000447 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000448 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000449 continue;
450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner038a3e32008-10-20 05:46:22 +0000452 // Otherwise, we have an @ directive, eat the @.
453 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000454 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000455 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000456 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000457 }
458
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000460
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000461 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000462 AtEnd.setBegin(AtLoc);
463 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000464 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000465 } else if (DirectiveKind == tok::objc_not_keyword) {
466 Diag(Tok, diag::err_objc_unknown_at);
467 SkipUntil(tok::semi);
468 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattnerda9fb152008-10-20 06:10:06 +0000471 // Eat the identifier.
472 ConsumeToken();
473
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000474 switch (DirectiveKind) {
475 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000476 // FIXME: If someone forgets an @end on a protocol, this loop will
477 // continue to eat up tons of stuff and spew lots of nonsense errors. It
478 // would probably be better to bail out if we saw an @class or @interface
479 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000480 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000481 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000482 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000483 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000484
485 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000486 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000487 Diag(AtLoc, diag::err_objc_missing_end)
488 << FixItHint::CreateInsertion(AtLoc, "@end\n");
489 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
490 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000491 ConsumeToken();
492 break;
493
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000494 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000495 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000496 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000497 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000498 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000499 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000500 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000501 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000502 break;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000504 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000505 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000506 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000507
Chris Lattner038a3e32008-10-20 05:46:22 +0000508 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000509 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000510 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000511 if (Tok.is(tok::l_paren)) {
512 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000513 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000516 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000517 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000518
Chris Lattner038a3e32008-10-20 05:46:22 +0000519 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000520 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +0000521 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000522
John McCall405988b2011-03-26 01:53:26 +0000523 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000524 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000525 }
Steve Naroff99264b42007-08-22 16:35:03 +0000526 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000527
528 // We break out of the big loop in two cases: when we see @end or when we see
529 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000530 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000531 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000532 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000533 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000534 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000535 } else {
536 Diag(Tok, diag::err_objc_missing_end)
537 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
538 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
539 << (int) Actions.getObjCContainerKind();
540 AtEnd.setBegin(Tok.getLocation());
541 AtEnd.setEnd(Tok.getLocation());
542 }
Mike Stump11289f42009-09-09 15:08:12 +0000543
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000544 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000545 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000546 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000547}
548
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000549/// Parse property attribute declarations.
550///
551/// property-attr-decl: '(' property-attrlist ')'
552/// property-attrlist:
553/// property-attribute
554/// property-attrlist ',' property-attribute
555/// property-attribute:
556/// getter '=' identifier
557/// setter '=' identifier ':'
558/// readonly
559/// readwrite
560/// assign
561/// retain
562/// copy
563/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000564/// atomic
565/// strong
566/// weak
567/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000568///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000569void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000570 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000571 BalancedDelimiterTracker T(*this, tok::l_paren);
572 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000574 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000575 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000576 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000577 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000578 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000579 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner76619232008-10-20 07:22:18 +0000581 // If this is not an identifier at all, bail out early.
582 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000583 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000584 return;
585 }
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner1db33542008-10-20 07:37:22 +0000587 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattner68e48682008-11-20 04:42:34 +0000589 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000590 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000591 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000592 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000593 else if (II->isStr("unsafe_unretained"))
594 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000595 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000596 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000597 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000598 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000599 else if (II->isStr("strong"))
600 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000601 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000602 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000603 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000604 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000605 else if (II->isStr("atomic"))
606 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000607 else if (II->isStr("weak"))
608 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000609 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000610 bool IsSetter = II->getNameStart()[0] == 's';
611
Chris Lattner825bca12008-10-20 07:39:53 +0000612 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000613 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
614 diag::err_objc_expected_equal_for_getter;
615
616 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000617 return;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Douglas Gregorc8537c52009-11-19 07:41:15 +0000619 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000620 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000621 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000622 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000623 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000624 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000625 }
626
Anders Carlssonfe15a782010-10-02 17:45:21 +0000627
628 SourceLocation SelLoc;
629 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
630
631 if (!SelIdent) {
632 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
633 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000634 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000635 return;
636 }
Mike Stump11289f42009-09-09 15:08:12 +0000637
Anders Carlssonfe15a782010-10-02 17:45:21 +0000638 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000639 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000640 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000642 if (ExpectAndConsume(tok::colon,
643 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000644 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000645 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000646 } else {
647 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000648 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000649 }
Chris Lattner825bca12008-10-20 07:39:53 +0000650 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000651 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000652 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000653 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000654 }
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattner1db33542008-10-20 07:37:22 +0000656 if (Tok.isNot(tok::comma))
657 break;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner1db33542008-10-20 07:37:22 +0000659 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000662 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000663}
664
Steve Naroff09bf8152007-09-06 21:24:23 +0000665/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000666/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000667/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000668///
669/// objc-instance-method: '-'
670/// objc-class-method: '+'
671///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000672/// objc-method-attributes: [OBJC2]
673/// __attribute__((deprecated))
674///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000675Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000676 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000677 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000678
Mike Stump11289f42009-09-09 15:08:12 +0000679 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000680 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000681 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000682 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000683 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000684 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000685 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000686}
687
688/// objc-selector:
689/// identifier
690/// one of
691/// enum struct union if else while do for switch case default
692/// break continue return goto asm sizeof typeof __alignof
693/// unsigned long const short volatile signed restrict _Complex
694/// in out inout bycopy byref oneway int char float double void _Bool
695///
Chris Lattner4f472a32009-04-11 18:13:45 +0000696IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000697
Chris Lattner5700fab2007-10-07 02:00:24 +0000698 switch (Tok.getKind()) {
699 default:
700 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000701 case tok::ampamp:
702 case tok::ampequal:
703 case tok::amp:
704 case tok::pipe:
705 case tok::tilde:
706 case tok::exclaim:
707 case tok::exclaimequal:
708 case tok::pipepipe:
709 case tok::pipeequal:
710 case tok::caret:
711 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000712 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000713 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000714 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
715 Tok.setKind(tok::identifier);
716 SelectorLoc = ConsumeToken();
717 return II;
718 }
719 return 0;
720 }
721
Chris Lattner5700fab2007-10-07 02:00:24 +0000722 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000723 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000724 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000725 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000726 case tok::kw_break:
727 case tok::kw_case:
728 case tok::kw_catch:
729 case tok::kw_char:
730 case tok::kw_class:
731 case tok::kw_const:
732 case tok::kw_const_cast:
733 case tok::kw_continue:
734 case tok::kw_default:
735 case tok::kw_delete:
736 case tok::kw_do:
737 case tok::kw_double:
738 case tok::kw_dynamic_cast:
739 case tok::kw_else:
740 case tok::kw_enum:
741 case tok::kw_explicit:
742 case tok::kw_export:
743 case tok::kw_extern:
744 case tok::kw_false:
745 case tok::kw_float:
746 case tok::kw_for:
747 case tok::kw_friend:
748 case tok::kw_goto:
749 case tok::kw_if:
750 case tok::kw_inline:
751 case tok::kw_int:
752 case tok::kw_long:
753 case tok::kw_mutable:
754 case tok::kw_namespace:
755 case tok::kw_new:
756 case tok::kw_operator:
757 case tok::kw_private:
758 case tok::kw_protected:
759 case tok::kw_public:
760 case tok::kw_register:
761 case tok::kw_reinterpret_cast:
762 case tok::kw_restrict:
763 case tok::kw_return:
764 case tok::kw_short:
765 case tok::kw_signed:
766 case tok::kw_sizeof:
767 case tok::kw_static:
768 case tok::kw_static_cast:
769 case tok::kw_struct:
770 case tok::kw_switch:
771 case tok::kw_template:
772 case tok::kw_this:
773 case tok::kw_throw:
774 case tok::kw_true:
775 case tok::kw_try:
776 case tok::kw_typedef:
777 case tok::kw_typeid:
778 case tok::kw_typename:
779 case tok::kw_typeof:
780 case tok::kw_union:
781 case tok::kw_unsigned:
782 case tok::kw_using:
783 case tok::kw_virtual:
784 case tok::kw_void:
785 case tok::kw_volatile:
786 case tok::kw_wchar_t:
787 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000788 case tok::kw__Bool:
789 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000790 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000791 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000792 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000793 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000794 }
Steve Naroff99264b42007-08-22 16:35:03 +0000795}
796
Fariborz Jahanian83615522008-01-02 22:54:34 +0000797/// objc-for-collection-in: 'in'
798///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000799bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000800 // FIXME: May have to do additional look-ahead to only allow for
801 // valid tokens following an 'in'; such as an identifier, unary operators,
802 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000803 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000804 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000805}
806
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000807/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000808/// qualifier list and builds their bitmask representation in the input
809/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000810///
811/// objc-type-qualifiers:
812/// objc-type-qualifier
813/// objc-type-qualifiers objc-type-qualifier
814///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000815void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000816 Declarator::TheContext Context) {
817 assert(Context == Declarator::ObjCParameterContext ||
818 Context == Declarator::ObjCResultContext);
819
Chris Lattner61511e12007-12-12 06:56:32 +0000820 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000821 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000822 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000823 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000824 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000825 }
826
Chris Lattner5e530bc2007-12-27 19:57:00 +0000827 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000828 return;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner61511e12007-12-12 06:56:32 +0000830 const IdentifierInfo *II = Tok.getIdentifierInfo();
831 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000832 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000833 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000834
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000835 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000836 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000837 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000838 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
839 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
840 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
841 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
842 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
843 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000844 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000845 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000846 ConsumeToken();
847 II = 0;
848 break;
849 }
Mike Stump11289f42009-09-09 15:08:12 +0000850
Chris Lattner61511e12007-12-12 06:56:32 +0000851 // If this wasn't a recognized qualifier, bail out.
852 if (II) return;
853 }
854}
855
John McCalla55902b2011-10-01 09:56:14 +0000856/// Take all the decl attributes out of the given list and add
857/// them to the given attribute set.
858static void takeDeclAttributes(ParsedAttributes &attrs,
859 AttributeList *list) {
860 while (list) {
861 AttributeList *cur = list;
862 list = cur->getNext();
863
864 if (!cur->isUsedAsTypeAttr()) {
865 // Clear out the next pointer. We're really completely
866 // destroying the internal invariants of the declarator here,
867 // but it doesn't matter because we're done with it.
868 cur->setNext(0);
869 attrs.add(cur);
870 }
871 }
872}
873
874/// takeDeclAttributes - Take all the decl attributes from the given
875/// declarator and add them to the given list.
876static void takeDeclAttributes(ParsedAttributes &attrs,
877 Declarator &D) {
878 // First, take ownership of all attributes.
879 attrs.getPool().takeAllFrom(D.getAttributePool());
880 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
881
882 // Now actually move the attributes over.
883 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
884 takeDeclAttributes(attrs, D.getAttributes());
885 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
886 takeDeclAttributes(attrs,
887 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
888}
889
Chris Lattner61511e12007-12-12 06:56:32 +0000890/// objc-type-name:
891/// '(' objc-type-qualifiers[opt] type-name ')'
892/// '(' objc-type-qualifiers[opt] ')'
893///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000894ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000895 Declarator::TheContext context,
896 ParsedAttributes *paramAttrs) {
897 assert(context == Declarator::ObjCParameterContext ||
898 context == Declarator::ObjCResultContext);
899 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
900
Chris Lattner0ef13522007-10-09 17:51:17 +0000901 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000903 BalancedDelimiterTracker T(*this, tok::l_paren);
904 T.consumeOpen();
905
Chris Lattner2ebb1782008-08-23 01:48:03 +0000906 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000907 ObjCDeclContextSwitch ObjCDC(*this);
908
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000909 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000910 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000911
John McCallba7bf592010-08-24 05:47:05 +0000912 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000913 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000914 // Parse an abstract declarator.
915 DeclSpec declSpec(AttrFactory);
916 declSpec.setObjCQualifiers(&DS);
917 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +0000918 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +0000919 Declarator declarator(declSpec, context);
920 ParseDeclarator(declarator);
921
922 // If that's not invalid, extract a type.
923 if (!declarator.isInvalidType()) {
924 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
925 if (!type.isInvalid())
926 Ty = type.get();
927
928 // If we're parsing a parameter, steal all the decl attributes
929 // and add them to the decl spec.
930 if (context == Declarator::ObjCParameterContext)
931 takeDeclAttributes(*paramAttrs, declarator);
932 }
933 } else if (context == Declarator::ObjCResultContext &&
934 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000935 if (!Ident_instancetype)
936 Ident_instancetype = PP.getIdentifierInfo("instancetype");
937
938 if (Tok.getIdentifierInfo() == Ident_instancetype) {
939 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
940 ConsumeToken();
941 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000942 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000943
Steve Naroff90255b42008-10-21 14:15:04 +0000944 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000945 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000946 else if (Tok.getLocation() == TypeStartLoc) {
947 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000948 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000949 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +0000950 } else {
951 // Otherwise, we found *something*, but didn't get a ')' in the right
952 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000953 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000954 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000955 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000956}
957
958/// objc-method-decl:
959/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000960/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000961/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000962/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000963///
964/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000965/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000966/// objc-keyword-selector objc-keyword-decl
967///
968/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000969/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
970/// objc-selector ':' objc-keyword-attributes[opt] identifier
971/// ':' objc-type-name objc-keyword-attributes[opt] identifier
972/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000973///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000974/// objc-parmlist:
975/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000976///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000977/// objc-parms:
978/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000979///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000980/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000981/// , ...
982///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000983/// objc-keyword-attributes: [OBJC2]
984/// __attribute__((unused))
985///
John McCall48871652010-08-21 09:40:31 +0000986Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000987 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000988 tok::ObjCKeywordKind MethodImplKind,
989 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000990 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000991
Douglas Gregor636a61e2010-04-07 00:21:17 +0000992 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000993 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000994 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000995 cutOffParsing();
996 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000997 }
998
Chris Lattner2ebb1782008-08-23 01:48:03 +0000999 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001000 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001001 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001002 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +00001003 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001004
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001005 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001006 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001007 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001008 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001009
Douglas Gregor636a61e2010-04-07 00:21:17 +00001010 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001011 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001012 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001013 cutOffParsing();
1014 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001015 }
1016
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001017 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001018 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001019 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001020
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001021 // An unnamed colon is valid.
1022 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001023 Diag(Tok, diag::err_expected_selector_for_method)
1024 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001025 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001026 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
John McCall48871652010-08-21 09:40:31 +00001027 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001030 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001031 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001032 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001033 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001034 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001035
Chris Lattner5700fab2007-10-07 02:00:24 +00001036 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001037 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001038 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001039 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001040 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001041 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001042 methodAttrs.getList(), MethodImplKind,
1043 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001044 PD.complete(Result);
1045 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001046 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001047
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001048 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001049 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001050 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001051 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1052 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001053
1054 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001055 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001056 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001057 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001058
Chris Lattner5700fab2007-10-07 02:00:24 +00001059 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001060 if (Tok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +00001061 Diag(Tok, diag::err_expected) << tok::colon;
Chris Lattner5700fab2007-10-07 02:00:24 +00001062 break;
1063 }
1064 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001065
John McCallba7bf592010-08-24 05:47:05 +00001066 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001067 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001068 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1069 Declarator::ObjCParameterContext,
1070 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001071
Chris Lattner5700fab2007-10-07 02:00:24 +00001072 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001073 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001074 ArgInfo.ArgAttrs = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001075 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001076 MaybeParseGNUAttributes(paramAttrs);
1077 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001078 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001079
Douglas Gregor45879692010-07-08 23:37:41 +00001080 // Code completion for the next piece of the selector.
1081 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001082 KeyIdents.push_back(SelIdent);
1083 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1084 mType == tok::minus,
1085 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001086 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001087 cutOffParsing();
1088 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001089 }
1090
Chris Lattner0ef13522007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001092 Diag(Tok, diag::err_expected)
1093 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001094 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001097 ArgInfo.Name = Tok.getIdentifierInfo();
1098 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001099 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001101 ArgInfos.push_back(ArgInfo);
1102 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001103 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001104
John McCall084e83d2011-03-24 11:26:52 +00001105 // Make sure the attributes persist.
1106 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1107
Douglas Gregor95887f92010-07-08 23:20:03 +00001108 // Code completion for the next piece of the selector.
1109 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001110 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1111 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001112 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001113 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001114 cutOffParsing();
1115 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001116 }
1117
Chris Lattner5700fab2007-10-07 02:00:24 +00001118 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001119 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001120 if (!SelIdent && Tok.isNot(tok::colon))
1121 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001122 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001123 SourceLocation ColonLoc = Tok.getLocation();
1124 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001125 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1126 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1127 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001128 }
1129 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001130 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001133 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001134 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001135 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001136 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001137 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001138 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001139 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001140 ConsumeToken();
1141 break;
1142 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001143 if (!cStyleParamWarned) {
1144 Diag(Tok, diag::warn_cstyle_param);
1145 cStyleParamWarned = true;
1146 }
John McCall084e83d2011-03-24 11:26:52 +00001147 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001148 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001149 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001150 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1151 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001152 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001153 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001154 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1155 ParmDecl.getIdentifierLoc(),
1156 Param,
1157 0));
Chris Lattner5700fab2007-10-07 02:00:24 +00001158 }
Mike Stump11289f42009-09-09 15:08:12 +00001159
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001160 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001161 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001162 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001163 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001164
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001165 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001166 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001167
Chris Lattner5700fab2007-10-07 02:00:24 +00001168 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1169 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001170 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001171 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001172 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001173 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001174 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001175 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001176 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001177
John McCall28a6aea2009-11-04 02:18:39 +00001178 PD.complete(Result);
1179 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001180}
1181
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001182/// objc-protocol-refs:
1183/// '<' identifier-list '>'
1184///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001185bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001186ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1187 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001188 bool WarnOnDeclarations,
1189 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001190 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001191
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001192 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001193
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001194 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattner3bbae002008-07-26 04:03:38 +00001196 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001197 if (Tok.is(tok::code_completion)) {
1198 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1199 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001200 cutOffParsing();
1201 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001202 }
1203
Chris Lattner3bbae002008-07-26 04:03:38 +00001204 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001205 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001206 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001207 return true;
1208 }
1209 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1210 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001211 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001212 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001213
Chris Lattner3bbae002008-07-26 04:03:38 +00001214 if (Tok.isNot(tok::comma))
1215 break;
1216 ConsumeToken();
1217 }
Mike Stump11289f42009-09-09 15:08:12 +00001218
Chris Lattner3bbae002008-07-26 04:03:38 +00001219 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001220 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001221 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001222
Chris Lattner3bbae002008-07-26 04:03:38 +00001223 // Convert the list of protocols identifiers into a list of protocol decls.
1224 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1225 &ProtocolIdents[0], ProtocolIdents.size(),
1226 Protocols);
1227 return false;
1228}
1229
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001230/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1231/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001232bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001233 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001234 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001235 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001236 SmallVector<Decl *, 8> ProtocolDecl;
1237 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001238 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1239 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001240 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1241 ProtocolLocs.data(), LAngleLoc);
1242 if (EndProtoLoc.isValid())
1243 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001244 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001245}
1246
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001247void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1248 BalancedDelimiterTracker &T,
1249 SmallVectorImpl<Decl *> &AllIvarDecls,
1250 bool RBraceMissing) {
1251 if (!RBraceMissing)
1252 T.consumeClose();
1253
1254 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1255 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1256 Actions.ActOnObjCContainerFinishDefinition();
1257 // Call ActOnFields() even if we don't have any decls. This is useful
1258 // for code rewriting tools that need to be aware of the empty list.
1259 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1260 AllIvarDecls,
1261 T.getOpenLocation(), T.getCloseLocation(), 0);
1262}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001263
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001264/// objc-class-instance-variables:
1265/// '{' objc-instance-variable-decl-list[opt] '}'
1266///
1267/// objc-instance-variable-decl-list:
1268/// objc-visibility-spec
1269/// objc-instance-variable-decl ';'
1270/// ';'
1271/// objc-instance-variable-decl-list objc-visibility-spec
1272/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1273/// objc-instance-variable-decl-list ';'
1274///
1275/// objc-visibility-spec:
1276/// @private
1277/// @protected
1278/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001279/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001280///
1281/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001282/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001283///
John McCall48871652010-08-21 09:40:31 +00001284void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001285 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001286 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001287 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001288 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001289
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001290 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001291 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001292
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001293 BalancedDelimiterTracker T(*this, tok::l_brace);
1294 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001295 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001296 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001297 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001298
Steve Naroff00433d32007-08-21 21:17:12 +00001299 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001300 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001301 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001302 continue;
1303 }
Mike Stump11289f42009-09-09 15:08:12 +00001304
Steve Naroff00433d32007-08-21 21:17:12 +00001305 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001306 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001307 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001308
1309 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001310 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001311 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001312 }
1313
Steve Naroff7c348172007-08-23 18:16:40 +00001314 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001315 case tok::objc_private:
1316 case tok::objc_public:
1317 case tok::objc_protected:
1318 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001319 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001320 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001321 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001322
1323 case tok::objc_end:
1324 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001325 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1326 Tok.setKind(tok::at);
1327 Tok.setLength(1);
1328 PP.EnterToken(Tok);
1329 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1330 T, AllIvarDecls, true);
1331 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001332
1333 default:
1334 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1335 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001336 }
1337 }
Mike Stump11289f42009-09-09 15:08:12 +00001338
Douglas Gregor48d46252010-01-13 21:54:15 +00001339 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001340 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001341 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001342 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001343 }
1344
John McCallcfefb6d2009-11-03 02:38:08 +00001345 struct ObjCIvarCallback : FieldCallback {
1346 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001347 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001348 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001349 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001350
John McCall48871652010-08-21 09:40:31 +00001351 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001352 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001353 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1354 }
1355
Eli Friedman934dbbf2012-08-08 23:53:27 +00001356 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001357 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001358 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001359 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001360 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001361 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001362 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001363 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001364 if (Field)
1365 AllIvarDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001366 FD.complete(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001367 }
1368 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001369
Chris Lattnera12405b2008-04-10 06:46:29 +00001370 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001371 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001372 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001373
Chris Lattner0ef13522007-10-09 17:51:17 +00001374 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001375 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001376 } else {
1377 Diag(Tok, diag::err_expected_semi_decl_list);
1378 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001379 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001380 }
1381 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001382 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1383 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001384 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001385}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001386
1387/// objc-protocol-declaration:
1388/// objc-protocol-definition
1389/// objc-protocol-forward-reference
1390///
1391/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001392/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001393/// objc-protocol-refs[opt]
1394/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001395/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001396///
1397/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001398/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001399///
James Dennett1355bd12012-06-11 06:19:40 +00001400/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001401/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001402/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001403Parser::DeclGroupPtrTy
1404Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1405 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001406 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001407 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1408 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001409
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001410 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001411 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001412 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001413 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001414 }
1415
Nico Weber69a79142013-04-04 00:15:10 +00001416 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001417
Chris Lattner0ef13522007-10-09 17:51:17 +00001418 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001419 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001420 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001421 }
1422 // Save the protocol name, then consume it.
1423 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1424 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001425
Chris Lattner0ef13522007-10-09 17:51:17 +00001426 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001427 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001428 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001429 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001430 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
Erik Verbruggenf9887852011-12-08 09:58:43 +00001433 CheckNestedObjCContexts(AtLoc);
1434
Chris Lattner0ef13522007-10-09 17:51:17 +00001435 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001436 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001437 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1438
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001439 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001440 while (1) {
1441 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001442 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001443 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001444 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001445 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001446 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001447 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1448 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001449 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattner0ef13522007-10-09 17:51:17 +00001451 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001452 break;
1453 }
1454 // Consume the ';'.
1455 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001456 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001457
Steve Naroff93eb5f12007-10-10 17:32:04 +00001458 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001459 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001460 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001461 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001462 }
Mike Stump11289f42009-09-09 15:08:12 +00001463
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001464 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001465 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001466
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001467 SmallVector<Decl *, 8> ProtocolRefs;
1468 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001469 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001470 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1471 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001472 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001473
John McCall48871652010-08-21 09:40:31 +00001474 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001475 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001476 ProtocolRefs.data(),
1477 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001478 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001479 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001480
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001481 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001482 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001483}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001484
1485/// objc-implementation:
1486/// objc-class-implementation-prologue
1487/// objc-category-implementation-prologue
1488///
1489/// objc-class-implementation-prologue:
1490/// @implementation identifier objc-superclass[opt]
1491/// objc-class-instance-variables[opt]
1492///
1493/// objc-category-implementation-prologue:
1494/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001495Parser::DeclGroupPtrTy
1496Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001497 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1498 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001499 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001500 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001501
Douglas Gregor49c22a72009-11-18 16:26:39 +00001502 // Code completion after '@implementation'.
1503 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001504 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001505 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001506 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001507 }
1508
Nico Weber69a79142013-04-04 00:15:10 +00001509 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001510
Chris Lattner0ef13522007-10-09 17:51:17 +00001511 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001512 Diag(Tok, diag::err_expected)
1513 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001514 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001515 }
1516 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001517 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001518 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001519 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001520
1521 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001522 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001523 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001524 SourceLocation categoryLoc, rparenLoc;
1525 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001526
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001527 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001528 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001529 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001530 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001531 }
1532
Chris Lattner0ef13522007-10-09 17:51:17 +00001533 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001534 categoryId = Tok.getIdentifierInfo();
1535 categoryLoc = ConsumeToken();
1536 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001537 Diag(Tok, diag::err_expected)
1538 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001539 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001540 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001541 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001542 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001543 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001544 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001545 }
1546 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001547 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1548 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1549 AttributeFactory attr;
1550 DeclSpec DS(attr);
1551 (void)ParseObjCProtocolQualifiers(DS);
1552 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001553 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001554 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001555 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001556
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001557 } else {
1558 // We have a class implementation
1559 SourceLocation superClassLoc;
1560 IdentifierInfo *superClassId = 0;
1561 if (Tok.is(tok::colon)) {
1562 // We have a super class
1563 ConsumeToken();
1564 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001565 Diag(Tok, diag::err_expected)
1566 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001567 return DeclGroupPtrTy();
1568 }
1569 superClassId = Tok.getIdentifierInfo();
1570 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001571 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001572 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1573 AtLoc, nameId, nameLoc,
1574 superClassId, superClassLoc);
1575
1576 if (Tok.is(tok::l_brace)) // we have ivars
1577 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001578 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1579 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1580 // try to recover.
1581 AttributeFactory attr;
1582 DeclSpec DS(attr);
1583 (void)ParseObjCProtocolQualifiers(DS);
1584 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001585 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001586 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001587
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001588 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001589
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001590 {
1591 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001592 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001593 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001594 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001595 MaybeParseMicrosoftAttributes(attrs);
1596 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1597 DeclGroupRef DG = DGP.get();
1598 DeclsInGroup.append(DG.begin(), DG.end());
1599 }
1600 }
1601 }
1602
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001603 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001604}
Steve Naroff33a1e802007-10-29 21:38:07 +00001605
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001606Parser::DeclGroupPtrTy
1607Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001608 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1609 "ParseObjCAtEndDeclaration(): Expected @end");
1610 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001611 if (CurParsedObjCImpl)
1612 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001613 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001614 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001615 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001616 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001617}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001618
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001619Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1620 if (!Finished) {
1621 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001622 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001623 P.Diag(P.Tok, diag::err_objc_missing_end)
1624 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1625 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1626 << Sema::OCK_Implementation;
1627 }
1628 }
1629 P.CurParsedObjCImpl = 0;
1630 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001631}
1632
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001633void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1634 assert(!Finished);
1635 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1636 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001637 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1638 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001639
1640 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1641
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001642 if (HasCFunction)
1643 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1644 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1645 false/*c-functions*/);
1646
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001647 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001648 for (LateParsedObjCMethodContainer::iterator
1649 I = LateParsedObjCMethods.begin(),
1650 E = LateParsedObjCMethods.end(); I != E; ++I)
1651 delete *I;
1652 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001653
1654 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001655}
1656
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001657/// compatibility-alias-decl:
1658/// @compatibility_alias alias-name class-name ';'
1659///
John McCall48871652010-08-21 09:40:31 +00001660Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001661 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1662 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1663 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001664 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001665 Diag(Tok, diag::err_expected) << tok::identifier;
John McCall48871652010-08-21 09:40:31 +00001666 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001667 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001668 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1669 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001670 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001671 Diag(Tok, diag::err_expected) << tok::identifier;
John McCall48871652010-08-21 09:40:31 +00001672 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001673 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001674 IdentifierInfo *classId = Tok.getIdentifierInfo();
1675 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001676 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1677 "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001678 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1679 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001680}
1681
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001682/// property-synthesis:
1683/// @synthesize property-ivar-list ';'
1684///
1685/// property-ivar-list:
1686/// property-ivar
1687/// property-ivar-list ',' property-ivar
1688///
1689/// property-ivar:
1690/// identifier
1691/// identifier '=' identifier
1692///
John McCall48871652010-08-21 09:40:31 +00001693Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001694 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001695 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001696 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001697
Douglas Gregor88e72a02009-11-18 19:45:45 +00001698 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001699 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001700 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001701 cutOffParsing();
1702 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001703 }
1704
Douglas Gregor88e72a02009-11-18 19:45:45 +00001705 if (Tok.isNot(tok::identifier)) {
1706 Diag(Tok, diag::err_synthesized_property_name);
1707 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001708 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001709 }
1710
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001711 IdentifierInfo *propertyIvar = 0;
1712 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1713 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001714 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001715 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001716 // property '=' ivar-name
1717 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001718
1719 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001720 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001721 cutOffParsing();
1722 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001723 }
1724
Chris Lattner0ef13522007-10-09 17:51:17 +00001725 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001726 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001727 break;
1728 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001729 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001730 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001731 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001732 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001733 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001734 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001735 break;
1736 ConsumeToken(); // consume ','
1737 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001738 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001739 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001740}
1741
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001742/// property-dynamic:
1743/// @dynamic property-list
1744///
1745/// property-list:
1746/// identifier
1747/// property-list ',' identifier
1748///
John McCall48871652010-08-21 09:40:31 +00001749Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001750 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1751 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001752 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001753 while (true) {
1754 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001755 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001756 cutOffParsing();
1757 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001758 }
1759
1760 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001761 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001762 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001763 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001764 }
1765
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001766 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1767 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001768 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001769 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001770
Chris Lattner0ef13522007-10-09 17:51:17 +00001771 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001772 break;
1773 ConsumeToken(); // consume ','
1774 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001775 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001776 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001777}
Mike Stump11289f42009-09-09 15:08:12 +00001778
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001779/// objc-throw-statement:
1780/// throw expression[opt];
1781///
John McCalldadc5752010-08-24 06:29:42 +00001782StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1783 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001784 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001785 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001786 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001787 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001788 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001789 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001790 }
1791 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001792 // consume ';'
1793 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001794 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001795}
1796
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001797/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001798/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001799///
John McCalldadc5752010-08-24 06:29:42 +00001800StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001801Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001802 ConsumeToken(); // consume synchronized
1803 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001804 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001805 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001806 }
John McCalld9bb7432011-07-27 21:50:02 +00001807
1808 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001809 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001810 ExprResult operand(ParseExpression());
1811
1812 if (Tok.is(tok::r_paren)) {
1813 ConsumeParen(); // ')'
1814 } else {
1815 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001816 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00001817
1818 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001819 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001820 }
John McCalld9bb7432011-07-27 21:50:02 +00001821
1822 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001823 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001824 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001825 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001826 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001827 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001828
John McCalld9bb7432011-07-27 21:50:02 +00001829 // Check the @synchronized operand now.
1830 if (!operand.isInvalid())
1831 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001832
John McCalld9bb7432011-07-27 21:50:02 +00001833 // Parse the compound statement within a new scope.
1834 ParseScope bodyScope(this, Scope::DeclScope);
1835 StmtResult body(ParseCompoundStatementBody());
1836 bodyScope.Exit();
1837
1838 // If there was a semantic or parse error earlier with the
1839 // operand, fail now.
1840 if (operand.isInvalid())
1841 return StmtError();
1842
1843 if (body.isInvalid())
1844 body = Actions.ActOnNullStmt(Tok.getLocation());
1845
1846 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001847}
1848
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001849/// objc-try-catch-statement:
1850/// @try compound-statement objc-catch-list[opt]
1851/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1852///
1853/// objc-catch-list:
1854/// @catch ( parameter-declaration ) compound-statement
1855/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1856/// catch-parameter-declaration:
1857/// parameter-declaration
1858/// '...' [OBJC2]
1859///
John McCalldadc5752010-08-24 06:29:42 +00001860StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001861 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001862
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001863 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001864 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001865 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001866 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001867 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001868 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001869 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001870 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001871 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001872 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001873 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001874 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001875
Chris Lattner0ef13522007-10-09 17:51:17 +00001876 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001877 // At this point, we need to lookahead to determine if this @ is the start
1878 // of an @catch or @finally. We don't want to consume the @ token if this
1879 // is an @try or @encode or something else.
1880 Token AfterAt = GetLookAheadToken(1);
1881 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1882 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1883 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001884
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001885 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001886 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001887 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001888 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001889 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001890 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001891 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001892 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001893 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001894 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001895 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001896 ParseDeclarator(ParmDecl);
1897
Douglas Gregore11ee112010-04-23 23:01:43 +00001898 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001899 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001900 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001901 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001902 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001903
Steve Naroff65a00892009-04-07 22:56:58 +00001904 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001905
Steve Naroff65a00892009-04-07 22:56:58 +00001906 if (Tok.is(tok::r_paren))
1907 RParenLoc = ConsumeParen();
1908 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001909 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00001910
John McCalldadc5752010-08-24 06:29:42 +00001911 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001912 if (Tok.is(tok::l_brace))
1913 CatchBody = ParseCompoundStatementBody();
1914 else
Alp Tokerec543272013-12-24 09:48:30 +00001915 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001916 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001917 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001918
John McCalldadc5752010-08-24 06:29:42 +00001919 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001920 RParenLoc,
1921 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001922 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001923 if (!Catch.isInvalid())
1924 CatchStmts.push_back(Catch.release());
1925
Steve Naroffe6016792008-02-05 21:27:35 +00001926 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001927 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1928 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001929 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001930 }
1931 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001932 } else {
1933 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001934 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001935 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001936
John McCalldadc5752010-08-24 06:29:42 +00001937 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001938 if (Tok.is(tok::l_brace))
1939 FinallyBody = ParseCompoundStatementBody();
1940 else
Alp Tokerec543272013-12-24 09:48:30 +00001941 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001942 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001943 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001944 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001945 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001946 catch_or_finally_seen = true;
1947 break;
1948 }
1949 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001950 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001951 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001952 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001953 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001954
John McCallb268a282010-08-23 23:25:46 +00001955 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001956 CatchStmts,
John McCallb268a282010-08-23 23:25:46 +00001957 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001958}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001959
John McCall31168b02011-06-15 23:02:42 +00001960/// objc-autoreleasepool-statement:
1961/// @autoreleasepool compound-statement
1962///
1963StmtResult
1964Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1965 ConsumeToken(); // consume autoreleasepool
1966 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001967 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00001968 return StmtError();
1969 }
1970 // Enter a scope to hold everything within the compound stmt. Compound
1971 // statements can always hold declarations.
1972 ParseScope BodyScope(this, Scope::DeclScope);
1973
1974 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1975
1976 BodyScope.Exit();
1977 if (AutoreleasePoolBody.isInvalid())
1978 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1979 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1980 AutoreleasePoolBody.take());
1981}
1982
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001983/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1984/// for later parsing.
1985void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1986 LexedMethod* LM = new LexedMethod(this, MDecl);
1987 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1988 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001989 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001990 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001991 if (Tok.is(tok::kw_try)) {
1992 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00001993 if (Tok.is(tok::colon)) {
1994 Toks.push_back(Tok);
1995 ConsumeToken();
1996 while (Tok.isNot(tok::l_brace)) {
1997 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1998 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1999 }
2000 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002001 Toks.push_back(Tok); // also store '{'
2002 }
2003 else if (Tok.is(tok::colon)) {
2004 ConsumeToken();
2005 while (Tok.isNot(tok::l_brace)) {
2006 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2007 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2008 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002009 Toks.push_back(Tok); // also store '{'
2010 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002011 ConsumeBrace();
2012 // Consume everything up to (and including) the matching right brace.
2013 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002014 while (Tok.is(tok::kw_catch)) {
2015 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2016 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2017 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002018}
2019
Steve Naroff09bf8152007-09-06 21:24:23 +00002020/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002021///
John McCall48871652010-08-21 09:40:31 +00002022Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002023 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002024
John McCallfaf5fb42010-08-26 23:41:50 +00002025 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2026 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002027
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002028 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002029 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002030 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002031 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002032 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002033 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002034 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002035 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002036
Steve Naroffbb875722007-11-11 19:54:21 +00002037 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002038 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002039 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002040
Steve Naroffbb875722007-11-11 19:54:21 +00002041 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002042 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002043
Steve Naroffbb875722007-11-11 19:54:21 +00002044 // If we didn't find the '{', bail out.
2045 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00002046 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002047 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002048
2049 if (!MDecl) {
2050 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002051 SkipUntil(tok::r_brace);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002052 return 0;
2053 }
2054
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002055 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002056 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002057 assert (CurParsedObjCImpl
2058 && "ParseObjCMethodDefinition - Method out of @implementation");
2059 // Consume the tokens and store them for later parsing.
2060 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002061 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002062}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002063
John McCalldadc5752010-08-24 06:29:42 +00002064StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002065 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002066 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002067 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002068 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002069 }
2070
2071 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002072 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002073
2074 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002075 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002076
2077 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002078 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002079
2080 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2081 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002082
John McCalldadc5752010-08-24 06:29:42 +00002083 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002084 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002085 // If the expression is invalid, skip ahead to the next semicolon. Not
2086 // doing this opens us up to the possibility of infinite loops if
2087 // ParseExpression does not consume any tokens.
2088 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002089 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002090 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002091
Steve Naroffe6016792008-02-05 21:27:35 +00002092 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002093 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002094 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002095}
2096
John McCalldadc5752010-08-24 06:29:42 +00002097ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002098 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002099 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002100 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002101 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002102 return ExprError();
2103
Ted Kremeneke65b0862012-03-06 20:05:56 +00002104 case tok::minus:
2105 case tok::plus: {
2106 tok::TokenKind Kind = Tok.getKind();
2107 SourceLocation OpLoc = ConsumeToken();
2108
2109 if (!Tok.is(tok::numeric_constant)) {
2110 const char *Symbol = 0;
2111 switch (Kind) {
2112 case tok::minus: Symbol = "-"; break;
2113 case tok::plus: Symbol = "+"; break;
2114 default: llvm_unreachable("missing unary operator case");
2115 }
2116 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2117 << Symbol;
2118 return ExprError();
2119 }
2120
2121 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2122 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002123 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002124 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002125 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002126
2127 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2128 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002129 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002130
2131 return ParsePostfixExpressionSuffix(
2132 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2133 }
2134
Chris Lattnere002fbe2007-12-12 01:04:12 +00002135 case tok::string_literal: // primary-expression: string-literal
2136 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002137 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002138
2139 case tok::char_constant:
2140 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2141
2142 case tok::numeric_constant:
2143 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2144
2145 case tok::kw_true: // Objective-C++, etc.
2146 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2147 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2148 case tok::kw_false: // Objective-C++, etc.
2149 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2150 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2151
2152 case tok::l_square:
2153 // Objective-C array literal
2154 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2155
2156 case tok::l_brace:
2157 // Objective-C dictionary literal
2158 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2159
Patrick Beard0caa3942012-04-19 00:25:12 +00002160 case tok::l_paren:
2161 // Objective-C boxed expression
2162 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2163
Chris Lattnere002fbe2007-12-12 01:04:12 +00002164 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002165 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002166 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002167
Chris Lattner197a3012008-08-05 06:19:09 +00002168 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2169 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002170 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002171 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002172 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002173 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002174 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002175 default: {
2176 const char *str = 0;
2177 if (GetLookAheadToken(1).is(tok::l_brace)) {
2178 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2179 str =
2180 ch == 't' ? "try"
2181 : (ch == 'f' ? "finally"
2182 : (ch == 'a' ? "autoreleasepool" : 0));
2183 }
2184 if (str) {
2185 SourceLocation kwLoc = Tok.getLocation();
2186 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2187 FixItHint::CreateReplacement(kwLoc, str));
2188 }
2189 else
2190 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2191 }
Chris Lattner197a3012008-08-05 06:19:09 +00002192 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002193 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002194}
2195
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002196/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002197///
2198/// This routine parses the receiver of a message send in
2199/// Objective-C++ either as a type or as an expression. Note that this
2200/// routine must not be called to parse a send to 'super', since it
2201/// has no way to return such a result.
2202///
2203/// \param IsExpr Whether the receiver was parsed as an expression.
2204///
2205/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2206/// IsExpr is true), the parsed expression. If the receiver was parsed
2207/// as a type (\c IsExpr is false), the parsed type.
2208///
2209/// \returns True if an error occurred during parsing or semantic
2210/// analysis, in which case the arguments do not have valid
2211/// values. Otherwise, returns false for a successful parse.
2212///
2213/// objc-receiver: [C++]
2214/// 'super' [not parsed here]
2215/// expression
2216/// simple-type-specifier
2217/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002218bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002219 InMessageExpressionRAIIObject InMessage(*this, true);
2220
Douglas Gregor8d4de672010-04-21 22:36:40 +00002221 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2222 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2223 TryAnnotateTypeOrScopeToken();
2224
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002225 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002226 // objc-receiver:
2227 // expression
John McCalldadc5752010-08-24 06:29:42 +00002228 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002229 if (Receiver.isInvalid())
2230 return true;
2231
2232 IsExpr = true;
2233 TypeOrExpr = Receiver.take();
2234 return false;
2235 }
2236
2237 // objc-receiver:
2238 // typename-specifier
2239 // simple-type-specifier
2240 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002241 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002242 ParseCXXSimpleTypeSpecifier(DS);
2243
2244 if (Tok.is(tok::l_paren)) {
2245 // If we see an opening parentheses at this point, we are
2246 // actually parsing an expression that starts with a
2247 // function-style cast, e.g.,
2248 //
2249 // postfix-expression:
2250 // simple-type-specifier ( expression-list [opt] )
2251 // typename-specifier ( expression-list [opt] )
2252 //
2253 // Parse the remainder of this case, then the (optional)
2254 // postfix-expression suffix, followed by the (optional)
2255 // right-hand side of the binary expression. We have an
2256 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002257 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002258 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002259 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002260 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002261 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002262 if (Receiver.isInvalid())
2263 return true;
2264
2265 IsExpr = true;
2266 TypeOrExpr = Receiver.take();
2267 return false;
2268 }
2269
2270 // We have a class message. Turn the simple-type-specifier or
2271 // typename-specifier we parsed into a type and parse the
2272 // remainder of the class message.
2273 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002274 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002275 if (Type.isInvalid())
2276 return true;
2277
2278 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002279 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002280 return false;
2281}
2282
Douglas Gregor990ccac2010-05-31 14:40:22 +00002283/// \brief Determine whether the parser is currently referring to a an
2284/// Objective-C message send, using a simplified heuristic to avoid overhead.
2285///
2286/// This routine will only return true for a subset of valid message-send
2287/// expressions.
2288bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002289 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002290 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002291 return GetLookAheadToken(1).is(tok::identifier) &&
2292 GetLookAheadToken(2).is(tok::identifier);
2293}
2294
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002295bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002296 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002297 InMessageExpression)
2298 return false;
2299
2300
2301 ParsedType Type;
2302
2303 if (Tok.is(tok::annot_typename))
2304 Type = getTypeAnnotation(Tok);
2305 else if (Tok.is(tok::identifier))
2306 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2307 getCurScope());
2308 else
2309 return false;
2310
2311 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2312 const Token &AfterNext = GetLookAheadToken(2);
2313 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2314 if (Tok.is(tok::identifier))
2315 TryAnnotateTypeOrScopeToken();
2316
2317 return Tok.is(tok::annot_typename);
2318 }
2319 }
2320
2321 return false;
2322}
2323
Mike Stump11289f42009-09-09 15:08:12 +00002324/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002325/// '[' objc-receiver objc-message-args ']'
2326///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002327/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002328/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002329/// expression
2330/// class-name
2331/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002332///
John McCalldadc5752010-08-24 06:29:42 +00002333ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002334 assert(Tok.is(tok::l_square) && "'[' expected");
2335 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2336
Douglas Gregora817a192010-05-27 23:06:34 +00002337 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002338 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002339 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002340 return ExprError();
2341 }
2342
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002343 InMessageExpressionRAIIObject InMessage(*this, true);
2344
David Blaikiebbafb8a2012-03-11 07:00:24 +00002345 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002346 // We completely separate the C and C++ cases because C++ requires
2347 // more complicated (read: slower) parsing.
2348
2349 // Handle send to super.
2350 // FIXME: This doesn't benefit from the same typo-correction we
2351 // get in Objective-C.
2352 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002353 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002354 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2355 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002356
2357 // Parse the receiver, which is either a type or an expression.
2358 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002359 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002360 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002361 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002362 return ExprError();
2363 }
2364
2365 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002366 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2367 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002368 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002369
2370 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002371 ParsedType::getFromOpaquePtr(TypeOrExpr),
2372 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002373 }
2374
2375 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002376 IdentifierInfo *Name = Tok.getIdentifierInfo();
2377 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002378 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002379 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002380 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002381 NextToken().is(tok::period),
2382 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002383 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002384 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2385 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002386
John McCallfaf5fb42010-08-26 23:41:50 +00002387 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002388 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002389 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002390 return ExprError();
2391 }
2392
Douglas Gregore5798dc2010-04-21 20:38:13 +00002393 ConsumeToken(); // the type name
2394
2395 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002396 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002397
John McCallfaf5fb42010-08-26 23:41:50 +00002398 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002399 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002400 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002401 }
Chris Lattner8f697062008-01-25 18:59:06 +00002402 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002403
2404 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002405 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002406 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002407 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002408 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002409 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002410
John McCallba7bf592010-08-24 05:47:05 +00002411 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2412 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002413}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002414
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002415/// \brief Parse the remainder of an Objective-C message following the
2416/// '[' objc-receiver.
2417///
2418/// This routine handles sends to super, class messages (sent to a
2419/// class name), and instance messages (sent to an object), and the
2420/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2421/// ReceiverExpr, respectively. Only one of these parameters may have
2422/// a valid value.
2423///
2424/// \param LBracLoc The location of the opening '['.
2425///
2426/// \param SuperLoc If this is a send to 'super', the location of the
2427/// 'super' keyword that indicates a send to the superclass.
2428///
2429/// \param ReceiverType If this is a class message, the type of the
2430/// class we are sending a message to.
2431///
2432/// \param ReceiverExpr If this is an instance message, the expression
2433/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002434///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002435/// objc-message-args:
2436/// objc-selector
2437/// objc-keywordarg-list
2438///
2439/// objc-keywordarg-list:
2440/// objc-keywordarg
2441/// objc-keywordarg-list objc-keywordarg
2442///
Mike Stump11289f42009-09-09 15:08:12 +00002443/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002444/// selector-name[opt] ':' objc-keywordexpr
2445///
2446/// objc-keywordexpr:
2447/// nonempty-expr-list
2448///
2449/// nonempty-expr-list:
2450/// assignment-expression
2451/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002452///
John McCalldadc5752010-08-24 06:29:42 +00002453ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002454Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002455 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002456 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002457 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002458 InMessageExpressionRAIIObject InMessage(*this, true);
2459
Steve Naroffeae65032009-11-07 02:08:14 +00002460 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002461 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002462 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002463 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002464 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002465 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002466 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002467 else
John McCallb268a282010-08-23 23:25:46 +00002468 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002469 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002470 cutOffParsing();
2471 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002472 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002473
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002474 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002475 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002476 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002477
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002478 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002479 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002480 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002481
Chris Lattner0ef13522007-10-09 17:51:17 +00002482 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002483 while (1) {
2484 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002485 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002486 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002487
Chris Lattner0ef13522007-10-09 17:51:17 +00002488 if (Tok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +00002489 Diag(Tok, diag::err_expected) << tok::colon;
Chris Lattner197a3012008-08-05 06:19:09 +00002490 // We must manually skip to a ']', otherwise the expression skipper will
2491 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2492 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002493 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002494 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002495 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002496
Steve Narofff73590d2007-09-27 14:38:14 +00002497 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002498 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002499
2500 if (Tok.is(tok::code_completion)) {
2501 if (SuperLoc.isValid())
2502 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002503 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002504 /*AtArgumentEpression=*/true);
2505 else if (ReceiverType)
2506 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002507 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002508 /*AtArgumentEpression=*/true);
2509 else
2510 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002511 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002512 /*AtArgumentEpression=*/true);
2513
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002514 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002515 return ExprError();
2516 }
2517
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002518 ExprResult Expr;
2519 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2520 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2521 Expr = ParseBraceInitializer();
2522 } else
2523 Expr = ParseAssignmentExpression();
2524
2525 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002526 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002527 // We must manually skip to a ']', otherwise the expression skipper will
2528 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2529 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002530 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002531 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002532 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002533
Steve Naroff486760a2007-09-17 20:25:27 +00002534 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002535 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002536
Douglas Gregor1b605f72009-11-19 01:08:35 +00002537 // Code completion after each argument.
2538 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002539 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002540 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002541 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002542 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002543 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002544 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002545 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002546 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002547 else
John McCallb268a282010-08-23 23:25:46 +00002548 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002549 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002550 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002551 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002552 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002553 }
2554
Steve Naroff486760a2007-09-17 20:25:27 +00002555 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002556 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002557 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002558 break;
2559 // We have a selector or a colon, continue parsing.
2560 }
2561 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002562 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002563 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002564 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002565 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002566 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002567 if (Tok.is(tok::colon)) {
2568 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2569 FixItHint::CreateRemoval(commaLoc);
2570 }
Chris Lattner197a3012008-08-05 06:19:09 +00002571 // We must manually skip to a ']', otherwise the expression skipper will
2572 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2573 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002574 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002575 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002576 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002577
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002578 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002579 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002580 }
2581 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002582 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002583
Chris Lattner197a3012008-08-05 06:19:09 +00002584 // We must manually skip to a ']', otherwise the expression skipper will
2585 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2586 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002587 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002588 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002589 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002590
Chris Lattner0ef13522007-10-09 17:51:17 +00002591 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002592 Diag(Tok, diag::err_expected)
2593 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002594 // We must manually skip to a ']', otherwise the expression skipper will
2595 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2596 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002597 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002598 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002599 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002600
Chris Lattner8f697062008-01-25 18:59:06 +00002601 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002602
Steve Naroffe61bfa82007-10-05 18:42:47 +00002603 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002604 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002605 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002606 KeyLocs.push_back(Loc);
2607 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002608 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002609
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002610 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002611 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002612 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002613 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002614 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002615 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002616 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002617 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002618}
2619
John McCalldadc5752010-08-24 06:29:42 +00002620ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2621 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002622 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002623
Chris Lattnere002fbe2007-12-12 01:04:12 +00002624 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2625 // expressions. At this point, we know that the only valid thing that starts
2626 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002627 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002628 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002629 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002630 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002631
Chris Lattnere002fbe2007-12-12 01:04:12 +00002632 while (Tok.is(tok::at)) {
2633 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002634
Sebastian Redlc13f2682008-12-09 20:22:58 +00002635 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002636 if (!isTokenStringLiteral())
2637 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002638
John McCalldadc5752010-08-24 06:29:42 +00002639 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002640 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002641 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002642
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002643 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002644 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002645
Nico Webera7c7e602012-12-31 00:28:03 +00002646 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2647 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002648}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002649
Ted Kremeneke65b0862012-03-06 20:05:56 +00002650/// ParseObjCBooleanLiteral -
2651/// objc-scalar-literal : '@' boolean-keyword
2652/// ;
2653/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2654/// ;
2655ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2656 bool ArgValue) {
2657 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2658 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2659}
2660
2661/// ParseObjCCharacterLiteral -
2662/// objc-scalar-literal : '@' character-literal
2663/// ;
2664ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2665 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2666 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002667 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002668 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002669 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002670 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002671}
2672
2673/// ParseObjCNumericLiteral -
2674/// objc-scalar-literal : '@' scalar-literal
2675/// ;
2676/// scalar-literal : | numeric-constant /* any numeric constant. */
2677/// ;
2678ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2679 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2680 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002681 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002682 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002683 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002684 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002685}
2686
Patrick Beard0caa3942012-04-19 00:25:12 +00002687/// ParseObjCBoxedExpr -
2688/// objc-box-expression:
2689/// @( assignment-expression )
2690ExprResult
2691Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2692 if (Tok.isNot(tok::l_paren))
2693 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2694
2695 BalancedDelimiterTracker T(*this, tok::l_paren);
2696 T.consumeOpen();
2697 ExprResult ValueExpr(ParseAssignmentExpression());
2698 if (T.consumeClose())
2699 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002700
2701 if (ValueExpr.isInvalid())
2702 return ExprError();
2703
Patrick Beard0caa3942012-04-19 00:25:12 +00002704 // Wrap the sub-expression in a parenthesized expression, to distinguish
2705 // a boxed expression from a literal.
2706 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2707 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Webera7c7e602012-12-31 00:28:03 +00002708 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2709 ValueExpr.take());
Patrick Beard0caa3942012-04-19 00:25:12 +00002710}
2711
Ted Kremeneke65b0862012-03-06 20:05:56 +00002712ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002713 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002714 ConsumeBracket(); // consume the l_square.
2715
2716 while (Tok.isNot(tok::r_square)) {
2717 // Parse list of array element expressions (all must be id types).
2718 ExprResult Res(ParseAssignmentExpression());
2719 if (Res.isInvalid()) {
2720 // We must manually skip to a ']', otherwise the expression skipper will
2721 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2722 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002723 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002724 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002725 }
2726
2727 // Parse the ellipsis that indicates a pack expansion.
2728 if (Tok.is(tok::ellipsis))
2729 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2730 if (Res.isInvalid())
2731 return true;
2732
2733 ElementExprs.push_back(Res.release());
2734
2735 if (Tok.is(tok::comma))
2736 ConsumeToken(); // Eat the ','.
2737 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00002738 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
2739 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002740 }
2741 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002742 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002743 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002744}
2745
2746ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2747 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2748 ConsumeBrace(); // consume the l_square.
2749 while (Tok.isNot(tok::r_brace)) {
2750 // Parse the comma separated key : value expressions.
2751 ExprResult KeyExpr;
2752 {
2753 ColonProtectionRAIIObject X(*this);
2754 KeyExpr = ParseAssignmentExpression();
2755 if (KeyExpr.isInvalid()) {
2756 // We must manually skip to a '}', otherwise the expression skipper will
2757 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2758 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002759 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002760 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002761 }
2762 }
2763
Alp Tokerec543272013-12-24 09:48:30 +00002764 if (!TryConsumeToken(tok::colon)) {
2765 Diag(Tok, diag::err_expected) << tok::colon;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002766 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002767 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002768 }
2769
2770 ExprResult ValueExpr(ParseAssignmentExpression());
2771 if (ValueExpr.isInvalid()) {
2772 // We must manually skip to a '}', otherwise the expression skipper will
2773 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2774 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002775 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002776 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002777 }
2778
2779 // Parse the ellipsis that designates this as a pack expansion.
2780 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00002781 if (getLangOpts().CPlusPlus)
2782 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2783
Ted Kremeneke65b0862012-03-06 20:05:56 +00002784 // We have a valid expression. Collect it in a vector so we can
2785 // build the argument list.
2786 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002787 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002788 };
2789 Elements.push_back(Element);
2790
2791 if (Tok.is(tok::comma))
2792 ConsumeToken(); // Eat the ','.
2793 else if (Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002794 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
2795 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002796 }
2797 SourceLocation EndLoc = ConsumeBrace();
2798
2799 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002800 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2801 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002802}
2803
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002804/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002805/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002806ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002807Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002808 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002809
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002810 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002811
Chris Lattner197a3012008-08-05 06:19:09 +00002812 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002813 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2814
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002815 BalancedDelimiterTracker T(*this, tok::l_paren);
2816 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002817
Douglas Gregor220cac52009-02-18 17:45:20 +00002818 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002819
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002820 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002821
Douglas Gregor220cac52009-02-18 17:45:20 +00002822 if (Ty.isInvalid())
2823 return ExprError();
2824
Nico Webera7c7e602012-12-31 00:28:03 +00002825 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2826 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002827}
Anders Carlssone01493d2007-08-23 15:25:28 +00002828
2829/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002830/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002831ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002832Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002833 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002834
Chris Lattner197a3012008-08-05 06:19:09 +00002835 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002836 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2837
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002838 BalancedDelimiterTracker T(*this, tok::l_paren);
2839 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002840
Chris Lattner197a3012008-08-05 06:19:09 +00002841 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00002842 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002843
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002844 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002845 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002846
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002847 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002848
Nico Webera7c7e602012-12-31 00:28:03 +00002849 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2850 T.getOpenLocation(), ProtoIdLoc,
2851 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002852}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002853
2854/// objc-selector-expression
2855/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002856ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002857 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002858
Chris Lattner197a3012008-08-05 06:19:09 +00002859 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002860 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2861
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002862 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002863 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002864
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002865 BalancedDelimiterTracker T(*this, tok::l_paren);
2866 T.consumeOpen();
2867
Douglas Gregor67c692c2010-08-26 15:07:07 +00002868 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002869 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002870 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002871 return ExprError();
2872 }
2873
Chris Lattner4f472a32009-04-11 18:13:45 +00002874 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002875 if (!SelIdent && // missing selector name.
2876 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00002877 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002878
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002879 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002880 unsigned nColons = 0;
2881 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002882 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002883 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2884 ++nColons;
2885 KeyIdents.push_back(0);
2886 } else if (Tok.isNot(tok::colon))
Alp Toker35d87032013-12-30 23:29:50 +00002887 return ExprError(Diag(Tok, diag::err_expected) << tok::colon);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002888
Chris Lattner1ba64452010-08-27 22:32:41 +00002889 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002890 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002891 if (Tok.is(tok::r_paren))
2892 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002893
2894 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002895 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002896 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002897 return ExprError();
2898 }
2899
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002900 // Check for another keyword selector.
2901 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002902 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002903 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002904 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002905 break;
2906 }
Steve Naroff152dd812007-12-05 22:21:29 +00002907 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002908 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002909 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00002910 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2911 T.getOpenLocation(),
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002912 T.getCloseLocation());
Gabor Greif24032f12007-10-19 15:38:32 +00002913 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002914
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002915void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2916 // MCDecl might be null due to error in method or c-function prototype, etc.
2917 Decl *MCDecl = LM.D;
2918 bool skip = MCDecl &&
2919 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2920 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2921 if (skip)
2922 return;
2923
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002924 // Save the current token position.
2925 SourceLocation OrigLoc = Tok.getLocation();
2926
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002927 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2928 // Append the current token at the end of the new token stream so that it
2929 // doesn't get lost.
2930 LM.Toks.push_back(Tok);
2931 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2932
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002933 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00002934 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002935
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002936 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2937 Tok.is(tok::colon)) &&
2938 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002939 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002940 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002941 parseMethod
2942 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2943 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002944
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002945 // Tell the actions module that we have entered a method or c-function definition
2946 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00002947 if (parseMethod)
2948 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2949 else
2950 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002951 if (Tok.is(tok::kw_try))
2952 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002953 else {
2954 if (Tok.is(tok::colon))
2955 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002956 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002957 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00002958
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002959 if (Tok.getLocation() != OrigLoc) {
2960 // Due to parsing error, we either went over the cached tokens or
2961 // there are still cached tokens left. If it's the latter case skip the
2962 // leftover tokens.
2963 // Since this is an uncommon situation that should be avoided, use the
2964 // expensive isBeforeInTranslationUnit call.
2965 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2966 OrigLoc))
2967 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2968 ConsumeAnyToken();
2969 }
2970
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002971 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002972}