blob: 5e308a6b575542a8846a41350a75acfec1312f08 [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)) {
Chris Lattnerbd638922006-11-10 05:19:25 +0000110 Diag(Tok, diag::err_expected_ident);
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)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000198 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000199 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000200 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000201
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000203 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000204 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000205 if (Tok.is(tok::l_paren) &&
206 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000207
208 BalancedDelimiterTracker T(*this, tok::l_paren);
209 T.consumeOpen();
210
211 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000212 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000213 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000214 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000215 cutOffParsing();
216 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000217 }
218
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000219 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000220 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 categoryId = Tok.getIdentifierInfo();
222 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000223 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000224 else if (!getLangOpts().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000225 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000226 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000227 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000228
229 T.consumeClose();
230 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000231 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000232
233 if (!attrs.empty()) { // categories don't support attributes.
234 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
235 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000237
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000238 // Next, we need to check for any protocol references.
239 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000240 SmallVector<Decl *, 8> ProtocolRefs;
241 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000242 if (Tok.is(tok::less) &&
243 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000244 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000245 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000246
John McCall48871652010-08-21 09:40:31 +0000247 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000248 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000249 nameId, nameLoc,
250 categoryId, categoryLoc,
251 ProtocolRefs.data(),
252 ProtocolRefs.size(),
253 ProtocolLocs.data(),
254 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000255
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000256 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000257 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000258
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000259 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000260 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000261 }
262 // Parse a class interface.
263 IdentifierInfo *superClassId = 0;
264 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000265
Chris Lattner0ef13522007-10-09 17:51:17 +0000266 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000267 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000268
269 // Code completion of superclass names.
270 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000271 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000272 cutOffParsing();
273 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000274 }
275
Chris Lattner0ef13522007-10-09 17:51:17 +0000276 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000277 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000278 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000279 }
280 superClassId = Tok.getIdentifierInfo();
281 superClassLoc = ConsumeToken();
282 }
283 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 SmallVector<Decl *, 8> ProtocolRefs;
285 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000286 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000287 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000288 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
289 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000290 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000291
John McCall48871652010-08-21 09:40:31 +0000292 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000293 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000294 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000295 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000296 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000297 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattner0ef13522007-10-09 17:51:17 +0000299 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000300 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000301
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000302 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000303 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000304}
305
John McCall064d77b2009-12-03 22:31:13 +0000306/// The Objective-C property callback. This should be defined where
307/// it's used, but instead it's been lifted to here to support VS2005.
308struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie68e081d2011-12-20 02:48:34 +0000309private:
310 virtual void anchor();
311public:
John McCall064d77b2009-12-03 22:31:13 +0000312 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000313 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000314 ObjCDeclSpec &OCDS;
315 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000316 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000317 tok::ObjCKeywordKind MethodImplKind;
318
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000319 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000320 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000321 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000322 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000323 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000324 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000325 MethodImplKind(MethodImplKind) {
326 }
327
Eli Friedman934dbbf2012-08-08 23:53:27 +0000328 void invoke(ParsingFieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000329 if (FD.D.getIdentifier() == 0) {
330 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
331 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000332 return;
John McCall064d77b2009-12-03 22:31:13 +0000333 }
334 if (FD.BitfieldSize) {
335 P.Diag(AtLoc, diag::err_objc_property_bitfield)
336 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000337 return;
John McCall064d77b2009-12-03 22:31:13 +0000338 }
339
340 // Install the property declarator into interfaceDecl.
341 IdentifierInfo *SelName =
342 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
343
344 Selector GetterSel =
345 P.PP.getSelectorTable().getNullarySelector(SelName);
346 IdentifierInfo *SetterName = OCDS.getSetterName();
347 Selector SetterSel;
348 if (SetterName)
349 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
350 else
Adrian Prantla4ce9062013-06-07 22:29:12 +0000351 SetterSel =
352 SelectorTable::constructSetterSelector(P.PP.getIdentifierTable(),
353 P.PP.getSelectorTable(),
354 FD.D.getIdentifier());
John McCall064d77b2009-12-03 22:31:13 +0000355 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000356 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000357 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
358 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000359 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000360 &isOverridingProperty,
361 MethodImplKind);
362 if (!isOverridingProperty)
363 Props.push_back(Property);
364
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000365 FD.complete(Property);
John McCall064d77b2009-12-03 22:31:13 +0000366 }
367};
368
David Blaikie68e081d2011-12-20 02:48:34 +0000369void Parser::ObjCPropertyCallback::anchor() {
370}
371
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000372/// objc-interface-decl-list:
373/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000374/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000375/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000376/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000377/// objc-interface-decl-list declaration
378/// objc-interface-decl-list ';'
379///
Steve Naroff99264b42007-08-22 16:35:03 +0000380/// objc-method-requirement: [OBJC2]
381/// @required
382/// @optional
383///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000384void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
385 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000386 SmallVector<Decl *, 32> allMethods;
387 SmallVector<Decl *, 16> allProperties;
388 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000389 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremenekc7c64312010-01-07 01:20:12 +0000391 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000392
Steve Naroff99264b42007-08-22 16:35:03 +0000393 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000394 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000395 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000396 if (Decl *methodPrototype =
397 ParseObjCMethodPrototype(MethodImplKind, false))
398 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000399 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
400 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000401 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
402 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
403 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
404 if (Tok.is(tok::semi))
405 ConsumeToken();
406 }
Steve Naroff99264b42007-08-22 16:35:03 +0000407 continue;
408 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000409 if (Tok.is(tok::l_paren)) {
410 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000411 ParseObjCMethodDecl(Tok.getLocation(),
412 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000413 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000414 continue;
415 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000416 // Ignore excess semicolons.
417 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000418 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000419 continue;
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnerda9fb152008-10-20 06:10:06 +0000422 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000423 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000424 break;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Douglas Gregorf1934162010-01-13 21:24:21 +0000426 // Code completion within an Objective-C interface.
427 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000428 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000429 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000430 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000431 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000432 }
433
Chris Lattner038a3e32008-10-20 05:46:22 +0000434 // If we don't have an @ directive, parse it as a function definition.
435 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000436 // The code below does not consume '}'s because it is afraid of eating the
437 // end of a namespace. Because of the way this code is structured, an
438 // erroneous r_brace would cause an infinite loop if not handled here.
439 if (Tok.is(tok::r_brace))
440 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000441 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000442 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000443 continue;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner038a3e32008-10-20 05:46:22 +0000446 // Otherwise, we have an @ directive, eat the @.
447 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000448 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000449 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000450 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000451 }
452
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000453 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000455 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000456 AtEnd.setBegin(AtLoc);
457 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000458 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000459 } else if (DirectiveKind == tok::objc_not_keyword) {
460 Diag(Tok, diag::err_objc_unknown_at);
461 SkipUntil(tok::semi);
462 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000463 }
Mike Stump11289f42009-09-09 15:08:12 +0000464
Chris Lattnerda9fb152008-10-20 06:10:06 +0000465 // Eat the identifier.
466 ConsumeToken();
467
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000468 switch (DirectiveKind) {
469 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000470 // FIXME: If someone forgets an @end on a protocol, this loop will
471 // continue to eat up tons of stuff and spew lots of nonsense errors. It
472 // would probably be better to bail out if we saw an @class or @interface
473 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000474 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000475 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000476 SkipUntil(tok::r_brace, tok::at);
477 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000478
479 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000480 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000481 Diag(AtLoc, diag::err_objc_missing_end)
482 << FixItHint::CreateInsertion(AtLoc, "@end\n");
483 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
484 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000485 ConsumeToken();
486 break;
487
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000488 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000489 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000490 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000491 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000492 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000493 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000494 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000495 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000496 break;
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000498 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000499 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000500 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000501
Chris Lattner038a3e32008-10-20 05:46:22 +0000502 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000503 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000504 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000505 if (Tok.is(tok::l_paren)) {
506 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000507 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000508 }
Mike Stump11289f42009-09-09 15:08:12 +0000509
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000510 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000511 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000512
Chris Lattner038a3e32008-10-20 05:46:22 +0000513 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000514 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +0000515 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000516
John McCall405988b2011-03-26 01:53:26 +0000517 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000518 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000519 }
Steve Naroff99264b42007-08-22 16:35:03 +0000520 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000521
522 // We break out of the big loop in two cases: when we see @end or when we see
523 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000524 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000525 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000526 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000527 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000528 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000529 } else {
530 Diag(Tok, diag::err_objc_missing_end)
531 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
532 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
533 << (int) Actions.getObjCContainerKind();
534 AtEnd.setBegin(Tok.getLocation());
535 AtEnd.setEnd(Tok.getLocation());
536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000538 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000539 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000540 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000541}
542
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000543/// Parse property attribute declarations.
544///
545/// property-attr-decl: '(' property-attrlist ')'
546/// property-attrlist:
547/// property-attribute
548/// property-attrlist ',' property-attribute
549/// property-attribute:
550/// getter '=' identifier
551/// setter '=' identifier ':'
552/// readonly
553/// readwrite
554/// assign
555/// retain
556/// copy
557/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000558/// atomic
559/// strong
560/// weak
561/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000562///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000563void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000564 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000565 BalancedDelimiterTracker T(*this, tok::l_paren);
566 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000568 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000569 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000570 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000571 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000572 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000573 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner76619232008-10-20 07:22:18 +0000575 // If this is not an identifier at all, bail out early.
576 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000577 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000578 return;
579 }
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner1db33542008-10-20 07:37:22 +0000581 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner68e48682008-11-20 04:42:34 +0000583 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000584 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000585 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000586 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000587 else if (II->isStr("unsafe_unretained"))
588 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000589 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000590 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000591 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000592 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000593 else if (II->isStr("strong"))
594 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000595 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000596 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000597 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000598 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000599 else if (II->isStr("atomic"))
600 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000601 else if (II->isStr("weak"))
602 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000603 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000604 bool IsSetter = II->getNameStart()[0] == 's';
605
Chris Lattner825bca12008-10-20 07:39:53 +0000606 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000607 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
608 diag::err_objc_expected_equal_for_getter;
609
610 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000611 return;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Douglas Gregorc8537c52009-11-19 07:41:15 +0000613 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000614 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000615 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000616 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000617 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000618 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000619 }
620
Anders Carlssonfe15a782010-10-02 17:45:21 +0000621
622 SourceLocation SelLoc;
623 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
624
625 if (!SelIdent) {
626 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
627 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000628 SkipUntil(tok::r_paren);
629 return;
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Anders Carlssonfe15a782010-10-02 17:45:21 +0000632 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000633 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000634 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000635
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000636 if (ExpectAndConsume(tok::colon,
637 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000638 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000639 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000640 } else {
641 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000642 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000643 }
Chris Lattner825bca12008-10-20 07:39:53 +0000644 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000645 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000646 SkipUntil(tok::r_paren);
647 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner1db33542008-10-20 07:37:22 +0000650 if (Tok.isNot(tok::comma))
651 break;
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner1db33542008-10-20 07:37:22 +0000653 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000654 }
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000656 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000657}
658
Steve Naroff09bf8152007-09-06 21:24:23 +0000659/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000660/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000661/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000662///
663/// objc-instance-method: '-'
664/// objc-class-method: '+'
665///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000666/// objc-method-attributes: [OBJC2]
667/// __attribute__((deprecated))
668///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000669Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000670 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000671 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000672
Mike Stump11289f42009-09-09 15:08:12 +0000673 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000674 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000675 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000676 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000677 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000678 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000679 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000680}
681
682/// objc-selector:
683/// identifier
684/// one of
685/// enum struct union if else while do for switch case default
686/// break continue return goto asm sizeof typeof __alignof
687/// unsigned long const short volatile signed restrict _Complex
688/// in out inout bycopy byref oneway int char float double void _Bool
689///
Chris Lattner4f472a32009-04-11 18:13:45 +0000690IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000691
Chris Lattner5700fab2007-10-07 02:00:24 +0000692 switch (Tok.getKind()) {
693 default:
694 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000695 case tok::ampamp:
696 case tok::ampequal:
697 case tok::amp:
698 case tok::pipe:
699 case tok::tilde:
700 case tok::exclaim:
701 case tok::exclaimequal:
702 case tok::pipepipe:
703 case tok::pipeequal:
704 case tok::caret:
705 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000706 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000707 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000708 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
709 Tok.setKind(tok::identifier);
710 SelectorLoc = ConsumeToken();
711 return II;
712 }
713 return 0;
714 }
715
Chris Lattner5700fab2007-10-07 02:00:24 +0000716 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000717 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000718 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000719 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000720 case tok::kw_break:
721 case tok::kw_case:
722 case tok::kw_catch:
723 case tok::kw_char:
724 case tok::kw_class:
725 case tok::kw_const:
726 case tok::kw_const_cast:
727 case tok::kw_continue:
728 case tok::kw_default:
729 case tok::kw_delete:
730 case tok::kw_do:
731 case tok::kw_double:
732 case tok::kw_dynamic_cast:
733 case tok::kw_else:
734 case tok::kw_enum:
735 case tok::kw_explicit:
736 case tok::kw_export:
737 case tok::kw_extern:
738 case tok::kw_false:
739 case tok::kw_float:
740 case tok::kw_for:
741 case tok::kw_friend:
742 case tok::kw_goto:
743 case tok::kw_if:
744 case tok::kw_inline:
745 case tok::kw_int:
746 case tok::kw_long:
747 case tok::kw_mutable:
748 case tok::kw_namespace:
749 case tok::kw_new:
750 case tok::kw_operator:
751 case tok::kw_private:
752 case tok::kw_protected:
753 case tok::kw_public:
754 case tok::kw_register:
755 case tok::kw_reinterpret_cast:
756 case tok::kw_restrict:
757 case tok::kw_return:
758 case tok::kw_short:
759 case tok::kw_signed:
760 case tok::kw_sizeof:
761 case tok::kw_static:
762 case tok::kw_static_cast:
763 case tok::kw_struct:
764 case tok::kw_switch:
765 case tok::kw_template:
766 case tok::kw_this:
767 case tok::kw_throw:
768 case tok::kw_true:
769 case tok::kw_try:
770 case tok::kw_typedef:
771 case tok::kw_typeid:
772 case tok::kw_typename:
773 case tok::kw_typeof:
774 case tok::kw_union:
775 case tok::kw_unsigned:
776 case tok::kw_using:
777 case tok::kw_virtual:
778 case tok::kw_void:
779 case tok::kw_volatile:
780 case tok::kw_wchar_t:
781 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000782 case tok::kw__Bool:
783 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000784 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000785 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000786 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000787 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000788 }
Steve Naroff99264b42007-08-22 16:35:03 +0000789}
790
Fariborz Jahanian83615522008-01-02 22:54:34 +0000791/// objc-for-collection-in: 'in'
792///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000793bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000794 // FIXME: May have to do additional look-ahead to only allow for
795 // valid tokens following an 'in'; such as an identifier, unary operators,
796 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000797 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000798 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000799}
800
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000801/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000802/// qualifier list and builds their bitmask representation in the input
803/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000804///
805/// objc-type-qualifiers:
806/// objc-type-qualifier
807/// objc-type-qualifiers objc-type-qualifier
808///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000809void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000810 Declarator::TheContext Context) {
811 assert(Context == Declarator::ObjCParameterContext ||
812 Context == Declarator::ObjCResultContext);
813
Chris Lattner61511e12007-12-12 06:56:32 +0000814 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000815 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000816 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000817 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000818 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000819 }
820
Chris Lattner5e530bc2007-12-27 19:57:00 +0000821 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000822 return;
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner61511e12007-12-12 06:56:32 +0000824 const IdentifierInfo *II = Tok.getIdentifierInfo();
825 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000826 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000827 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000828
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000829 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000830 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000831 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000832 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
833 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
834 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
835 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
836 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
837 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000838 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000839 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000840 ConsumeToken();
841 II = 0;
842 break;
843 }
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner61511e12007-12-12 06:56:32 +0000845 // If this wasn't a recognized qualifier, bail out.
846 if (II) return;
847 }
848}
849
John McCalla55902b2011-10-01 09:56:14 +0000850/// Take all the decl attributes out of the given list and add
851/// them to the given attribute set.
852static void takeDeclAttributes(ParsedAttributes &attrs,
853 AttributeList *list) {
854 while (list) {
855 AttributeList *cur = list;
856 list = cur->getNext();
857
858 if (!cur->isUsedAsTypeAttr()) {
859 // Clear out the next pointer. We're really completely
860 // destroying the internal invariants of the declarator here,
861 // but it doesn't matter because we're done with it.
862 cur->setNext(0);
863 attrs.add(cur);
864 }
865 }
866}
867
868/// takeDeclAttributes - Take all the decl attributes from the given
869/// declarator and add them to the given list.
870static void takeDeclAttributes(ParsedAttributes &attrs,
871 Declarator &D) {
872 // First, take ownership of all attributes.
873 attrs.getPool().takeAllFrom(D.getAttributePool());
874 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
875
876 // Now actually move the attributes over.
877 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
878 takeDeclAttributes(attrs, D.getAttributes());
879 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
880 takeDeclAttributes(attrs,
881 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
882}
883
Chris Lattner61511e12007-12-12 06:56:32 +0000884/// objc-type-name:
885/// '(' objc-type-qualifiers[opt] type-name ')'
886/// '(' objc-type-qualifiers[opt] ')'
887///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000888ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000889 Declarator::TheContext context,
890 ParsedAttributes *paramAttrs) {
891 assert(context == Declarator::ObjCParameterContext ||
892 context == Declarator::ObjCResultContext);
893 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
894
Chris Lattner0ef13522007-10-09 17:51:17 +0000895 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000896
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000897 BalancedDelimiterTracker T(*this, tok::l_paren);
898 T.consumeOpen();
899
Chris Lattner2ebb1782008-08-23 01:48:03 +0000900 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000901 ObjCDeclContextSwitch ObjCDC(*this);
902
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000903 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000904 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000905
John McCallba7bf592010-08-24 05:47:05 +0000906 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000907 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000908 // Parse an abstract declarator.
909 DeclSpec declSpec(AttrFactory);
910 declSpec.setObjCQualifiers(&DS);
911 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +0000912 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +0000913 Declarator declarator(declSpec, context);
914 ParseDeclarator(declarator);
915
916 // If that's not invalid, extract a type.
917 if (!declarator.isInvalidType()) {
918 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
919 if (!type.isInvalid())
920 Ty = type.get();
921
922 // If we're parsing a parameter, steal all the decl attributes
923 // and add them to the decl spec.
924 if (context == Declarator::ObjCParameterContext)
925 takeDeclAttributes(*paramAttrs, declarator);
926 }
927 } else if (context == Declarator::ObjCResultContext &&
928 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000929 if (!Ident_instancetype)
930 Ident_instancetype = PP.getIdentifierInfo("instancetype");
931
932 if (Tok.getIdentifierInfo() == Ident_instancetype) {
933 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
934 ConsumeToken();
935 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000936 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000937
Steve Naroff90255b42008-10-21 14:15:04 +0000938 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000939 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000940 else if (Tok.getLocation() == TypeStartLoc) {
941 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000942 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000943 SkipUntil(tok::r_paren);
944 } else {
945 // Otherwise, we found *something*, but didn't get a ')' in the right
946 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000947 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000948 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000949 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000950}
951
952/// objc-method-decl:
953/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000954/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000955/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000956/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000957///
958/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000959/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000960/// objc-keyword-selector objc-keyword-decl
961///
962/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000963/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
964/// objc-selector ':' objc-keyword-attributes[opt] identifier
965/// ':' objc-type-name objc-keyword-attributes[opt] identifier
966/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000967///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000968/// objc-parmlist:
969/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000970///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000971/// objc-parms:
972/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000973///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000974/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000975/// , ...
976///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000977/// objc-keyword-attributes: [OBJC2]
978/// __attribute__((unused))
979///
John McCall48871652010-08-21 09:40:31 +0000980Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000981 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000982 tok::ObjCKeywordKind MethodImplKind,
983 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000984 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000985
Douglas Gregor636a61e2010-04-07 00:21:17 +0000986 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000987 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000988 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000989 cutOffParsing();
990 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000991 }
992
Chris Lattner2ebb1782008-08-23 01:48:03 +0000993 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000994 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000995 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000996 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000997 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000998
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000999 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001000 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001001 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001002 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001003
Douglas Gregor636a61e2010-04-07 00:21:17 +00001004 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001005 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001006 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001007 cutOffParsing();
1008 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001009 }
1010
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001011 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001012 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001013 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001014
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001015 // An unnamed colon is valid.
1016 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001017 Diag(Tok, diag::err_expected_selector_for_method)
1018 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001019 // Skip until we get a ; or @.
1020 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCall48871652010-08-21 09:40:31 +00001021 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001024 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001025 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001026 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001027 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001028 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner5700fab2007-10-07 02:00:24 +00001030 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001031 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001032 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001033 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001034 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001035 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001036 methodAttrs.getList(), MethodImplKind,
1037 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001038 PD.complete(Result);
1039 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001040 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001041
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001042 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001043 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001044 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001045 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1046 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001047
1048 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001049 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001050 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001051 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner5700fab2007-10-07 02:00:24 +00001053 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001054 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001055 Diag(Tok, diag::err_expected_colon);
1056 break;
1057 }
1058 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001059
John McCallba7bf592010-08-24 05:47:05 +00001060 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001061 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001062 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1063 Declarator::ObjCParameterContext,
1064 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001065
Chris Lattner5700fab2007-10-07 02:00:24 +00001066 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001067 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001068 ArgInfo.ArgAttrs = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001069 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001070 MaybeParseGNUAttributes(paramAttrs);
1071 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001072 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001073
Douglas Gregor45879692010-07-08 23:37:41 +00001074 // Code completion for the next piece of the selector.
1075 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001076 KeyIdents.push_back(SelIdent);
1077 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1078 mType == tok::minus,
1079 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001080 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001081 cutOffParsing();
1082 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001083 }
1084
Chris Lattner0ef13522007-10-09 17:51:17 +00001085 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001086 Diag(Tok, diag::err_expected_ident); // missing argument name.
1087 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001088 }
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001090 ArgInfo.Name = Tok.getIdentifierInfo();
1091 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001092 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001093
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001094 ArgInfos.push_back(ArgInfo);
1095 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001096 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001097
John McCall084e83d2011-03-24 11:26:52 +00001098 // Make sure the attributes persist.
1099 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1100
Douglas Gregor95887f92010-07-08 23:20:03 +00001101 // Code completion for the next piece of the selector.
1102 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001103 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1104 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001105 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001106 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001107 cutOffParsing();
1108 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001109 }
1110
Chris Lattner5700fab2007-10-07 02:00:24 +00001111 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001112 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001113 if (!SelIdent && Tok.isNot(tok::colon))
1114 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001115 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001116 SourceLocation ColonLoc = Tok.getLocation();
1117 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001118 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1119 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1120 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001121 }
1122 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001123 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001126 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001127 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001128 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001129 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001130 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001131 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001132 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001133 ConsumeToken();
1134 break;
1135 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001136 if (!cStyleParamWarned) {
1137 Diag(Tok, diag::warn_cstyle_param);
1138 cStyleParamWarned = true;
1139 }
John McCall084e83d2011-03-24 11:26:52 +00001140 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001141 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001142 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001143 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1144 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001145 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001146 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001147 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1148 ParmDecl.getIdentifierLoc(),
1149 Param,
1150 0));
Chris Lattner5700fab2007-10-07 02:00:24 +00001151 }
Mike Stump11289f42009-09-09 15:08:12 +00001152
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001153 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001154 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001155 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001156 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001157
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001158 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001159 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001160
Chris Lattner5700fab2007-10-07 02:00:24 +00001161 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1162 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001163 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001164 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001165 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001166 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001167 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001168 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001169 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001170
John McCall28a6aea2009-11-04 02:18:39 +00001171 PD.complete(Result);
1172 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001173}
1174
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001175/// objc-protocol-refs:
1176/// '<' identifier-list '>'
1177///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001178bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001179ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1180 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001181 bool WarnOnDeclarations,
1182 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001183 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001184
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001185 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001186
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001187 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001188
Chris Lattner3bbae002008-07-26 04:03:38 +00001189 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001190 if (Tok.is(tok::code_completion)) {
1191 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1192 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001193 cutOffParsing();
1194 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001195 }
1196
Chris Lattner3bbae002008-07-26 04:03:38 +00001197 if (Tok.isNot(tok::identifier)) {
1198 Diag(Tok, diag::err_expected_ident);
1199 SkipUntil(tok::greater);
1200 return true;
1201 }
1202 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1203 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001204 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001205 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner3bbae002008-07-26 04:03:38 +00001207 if (Tok.isNot(tok::comma))
1208 break;
1209 ConsumeToken();
1210 }
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattner3bbae002008-07-26 04:03:38 +00001212 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001213 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001214 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001215
Chris Lattner3bbae002008-07-26 04:03:38 +00001216 // Convert the list of protocols identifiers into a list of protocol decls.
1217 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1218 &ProtocolIdents[0], ProtocolIdents.size(),
1219 Protocols);
1220 return false;
1221}
1222
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001223/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1224/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001225bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001226 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001227 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001228 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001229 SmallVector<Decl *, 8> ProtocolDecl;
1230 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001231 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1232 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001233 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1234 ProtocolLocs.data(), LAngleLoc);
1235 if (EndProtoLoc.isValid())
1236 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001237 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001238}
1239
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001240void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1241 BalancedDelimiterTracker &T,
1242 SmallVectorImpl<Decl *> &AllIvarDecls,
1243 bool RBraceMissing) {
1244 if (!RBraceMissing)
1245 T.consumeClose();
1246
1247 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1248 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1249 Actions.ActOnObjCContainerFinishDefinition();
1250 // Call ActOnFields() even if we don't have any decls. This is useful
1251 // for code rewriting tools that need to be aware of the empty list.
1252 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1253 AllIvarDecls,
1254 T.getOpenLocation(), T.getCloseLocation(), 0);
1255}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001256
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001257/// objc-class-instance-variables:
1258/// '{' objc-instance-variable-decl-list[opt] '}'
1259///
1260/// objc-instance-variable-decl-list:
1261/// objc-visibility-spec
1262/// objc-instance-variable-decl ';'
1263/// ';'
1264/// objc-instance-variable-decl-list objc-visibility-spec
1265/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1266/// objc-instance-variable-decl-list ';'
1267///
1268/// objc-visibility-spec:
1269/// @private
1270/// @protected
1271/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001272/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001273///
1274/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001275/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001276///
John McCall48871652010-08-21 09:40:31 +00001277void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001278 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001279 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001280 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001281 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001282
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001283 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001284 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001285
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001286 BalancedDelimiterTracker T(*this, tok::l_brace);
1287 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001288 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001289 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001290 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001291
Steve Naroff00433d32007-08-21 21:17:12 +00001292 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001293 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001294 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001295 continue;
1296 }
Mike Stump11289f42009-09-09 15:08:12 +00001297
Steve Naroff00433d32007-08-21 21:17:12 +00001298 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001299 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001300 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001301
1302 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001303 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001304 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001305 }
1306
Steve Naroff7c348172007-08-23 18:16:40 +00001307 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001308 case tok::objc_private:
1309 case tok::objc_public:
1310 case tok::objc_protected:
1311 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001312 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001313 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001314 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001315
1316 case tok::objc_end:
1317 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001318 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1319 Tok.setKind(tok::at);
1320 Tok.setLength(1);
1321 PP.EnterToken(Tok);
1322 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1323 T, AllIvarDecls, true);
1324 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001325
1326 default:
1327 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1328 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001329 }
1330 }
Mike Stump11289f42009-09-09 15:08:12 +00001331
Douglas Gregor48d46252010-01-13 21:54:15 +00001332 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001333 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001334 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001335 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001336 }
1337
John McCallcfefb6d2009-11-03 02:38:08 +00001338 struct ObjCIvarCallback : FieldCallback {
1339 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001340 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001341 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001342 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001343
John McCall48871652010-08-21 09:40:31 +00001344 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001345 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001346 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1347 }
1348
Eli Friedman934dbbf2012-08-08 23:53:27 +00001349 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001350 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001351 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001352 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001353 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001354 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001355 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001356 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001357 if (Field)
1358 AllIvarDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001359 FD.complete(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001360 }
1361 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001362
Chris Lattnera12405b2008-04-10 06:46:29 +00001363 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001364 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001365 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001366
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001368 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001369 } else {
1370 Diag(Tok, diag::err_expected_semi_decl_list);
1371 // Skip to end of block or statement
1372 SkipUntil(tok::r_brace, true, true);
1373 }
1374 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001375 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1376 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001377 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001378}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001379
1380/// objc-protocol-declaration:
1381/// objc-protocol-definition
1382/// objc-protocol-forward-reference
1383///
1384/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001385/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001386/// objc-protocol-refs[opt]
1387/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001388/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001389///
1390/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001391/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001392///
James Dennett1355bd12012-06-11 06:19:40 +00001393/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001394/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001395/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001396Parser::DeclGroupPtrTy
1397Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1398 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001399 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001400 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1401 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001402
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001403 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001404 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001405 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001406 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001407 }
1408
Nico Weber69a79142013-04-04 00:15:10 +00001409 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001410
Chris Lattner0ef13522007-10-09 17:51:17 +00001411 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001412 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001413 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001414 }
1415 // Save the protocol name, then consume it.
1416 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1417 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001418
Chris Lattner0ef13522007-10-09 17:51:17 +00001419 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001420 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001421 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001422 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001423 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001424 }
Mike Stump11289f42009-09-09 15:08:12 +00001425
Erik Verbruggenf9887852011-12-08 09:58:43 +00001426 CheckNestedObjCContexts(AtLoc);
1427
Chris Lattner0ef13522007-10-09 17:51:17 +00001428 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001429 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001430 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1431
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001432 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001433 while (1) {
1434 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001435 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001436 Diag(Tok, diag::err_expected_ident);
1437 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001438 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001439 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001440 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1441 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001442 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001443
Chris Lattner0ef13522007-10-09 17:51:17 +00001444 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001445 break;
1446 }
1447 // Consume the ';'.
1448 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001449 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001450
Steve Naroff93eb5f12007-10-10 17:32:04 +00001451 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001452 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001453 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001454 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001455 }
Mike Stump11289f42009-09-09 15:08:12 +00001456
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001457 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001458 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001459
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001460 SmallVector<Decl *, 8> ProtocolRefs;
1461 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001462 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001463 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1464 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001465 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001466
John McCall48871652010-08-21 09:40:31 +00001467 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001468 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001469 ProtocolRefs.data(),
1470 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001471 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001472 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001473
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001474 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001475 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001476}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001477
1478/// objc-implementation:
1479/// objc-class-implementation-prologue
1480/// objc-category-implementation-prologue
1481///
1482/// objc-class-implementation-prologue:
1483/// @implementation identifier objc-superclass[opt]
1484/// objc-class-instance-variables[opt]
1485///
1486/// objc-category-implementation-prologue:
1487/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001488Parser::DeclGroupPtrTy
1489Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001490 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1491 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001492 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001493 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001494
Douglas Gregor49c22a72009-11-18 16:26:39 +00001495 // Code completion after '@implementation'.
1496 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001497 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001498 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001499 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001500 }
1501
Nico Weber69a79142013-04-04 00:15:10 +00001502 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001503
Chris Lattner0ef13522007-10-09 17:51:17 +00001504 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001505 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001506 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001507 }
1508 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001509 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001510 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001511 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001512
1513 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001514 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001515 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001516 SourceLocation categoryLoc, rparenLoc;
1517 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001518
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001519 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001520 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001521 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001522 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001523 }
1524
Chris Lattner0ef13522007-10-09 17:51:17 +00001525 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001526 categoryId = Tok.getIdentifierInfo();
1527 categoryLoc = ConsumeToken();
1528 } else {
1529 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001530 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001531 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001532 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001533 Diag(Tok, diag::err_expected_rparen);
1534 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001535 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001536 }
1537 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001538 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1539 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1540 AttributeFactory attr;
1541 DeclSpec DS(attr);
1542 (void)ParseObjCProtocolQualifiers(DS);
1543 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001544 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001545 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001546 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001547
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001548 } else {
1549 // We have a class implementation
1550 SourceLocation superClassLoc;
1551 IdentifierInfo *superClassId = 0;
1552 if (Tok.is(tok::colon)) {
1553 // We have a super class
1554 ConsumeToken();
1555 if (Tok.isNot(tok::identifier)) {
1556 Diag(Tok, diag::err_expected_ident); // missing super class name.
1557 return DeclGroupPtrTy();
1558 }
1559 superClassId = Tok.getIdentifierInfo();
1560 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001561 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001562 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1563 AtLoc, nameId, nameLoc,
1564 superClassId, superClassLoc);
1565
1566 if (Tok.is(tok::l_brace)) // we have ivars
1567 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001568 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1569 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1570 // try to recover.
1571 AttributeFactory attr;
1572 DeclSpec DS(attr);
1573 (void)ParseObjCProtocolQualifiers(DS);
1574 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001575 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001576 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001577
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001578 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001579
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001580 {
1581 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1582 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1583 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001584 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001585 MaybeParseMicrosoftAttributes(attrs);
1586 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1587 DeclGroupRef DG = DGP.get();
1588 DeclsInGroup.append(DG.begin(), DG.end());
1589 }
1590 }
1591 }
1592
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001593 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001594}
Steve Naroff33a1e802007-10-29 21:38:07 +00001595
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001596Parser::DeclGroupPtrTy
1597Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001598 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1599 "ParseObjCAtEndDeclaration(): Expected @end");
1600 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001601 if (CurParsedObjCImpl)
1602 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001603 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001604 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001605 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001606 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001607}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001608
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001609Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1610 if (!Finished) {
1611 finish(P.Tok.getLocation());
1612 if (P.Tok.is(tok::eof)) {
1613 P.Diag(P.Tok, diag::err_objc_missing_end)
1614 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1615 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1616 << Sema::OCK_Implementation;
1617 }
1618 }
1619 P.CurParsedObjCImpl = 0;
1620 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001621}
1622
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001623void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1624 assert(!Finished);
1625 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1626 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001627 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1628 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001629
1630 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1631
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001632 if (HasCFunction)
1633 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1634 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1635 false/*c-functions*/);
1636
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001637 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001638 for (LateParsedObjCMethodContainer::iterator
1639 I = LateParsedObjCMethods.begin(),
1640 E = LateParsedObjCMethods.end(); I != E; ++I)
1641 delete *I;
1642 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001643
1644 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001645}
1646
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001647/// compatibility-alias-decl:
1648/// @compatibility_alias alias-name class-name ';'
1649///
John McCall48871652010-08-21 09:40:31 +00001650Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001651 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1652 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1653 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001654 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001655 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001656 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001657 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001658 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1659 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001660 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001661 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001662 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001663 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001664 IdentifierInfo *classId = Tok.getIdentifierInfo();
1665 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001666 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1667 "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001668 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1669 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001670}
1671
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001672/// property-synthesis:
1673/// @synthesize property-ivar-list ';'
1674///
1675/// property-ivar-list:
1676/// property-ivar
1677/// property-ivar-list ',' property-ivar
1678///
1679/// property-ivar:
1680/// identifier
1681/// identifier '=' identifier
1682///
John McCall48871652010-08-21 09:40:31 +00001683Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001684 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001685 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001686 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001687
Douglas Gregor88e72a02009-11-18 19:45:45 +00001688 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001689 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001690 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001691 cutOffParsing();
1692 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001693 }
1694
Douglas Gregor88e72a02009-11-18 19:45:45 +00001695 if (Tok.isNot(tok::identifier)) {
1696 Diag(Tok, diag::err_synthesized_property_name);
1697 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001698 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001699 }
1700
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001701 IdentifierInfo *propertyIvar = 0;
1702 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1703 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001704 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001705 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001706 // property '=' ivar-name
1707 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001708
1709 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001710 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001711 cutOffParsing();
1712 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001713 }
1714
Chris Lattner0ef13522007-10-09 17:51:17 +00001715 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001716 Diag(Tok, diag::err_expected_ident);
1717 break;
1718 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001719 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001720 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001721 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001722 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001723 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001724 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001725 break;
1726 ConsumeToken(); // consume ','
1727 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001728 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001729 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001730}
1731
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001732/// property-dynamic:
1733/// @dynamic property-list
1734///
1735/// property-list:
1736/// identifier
1737/// property-list ',' identifier
1738///
John McCall48871652010-08-21 09:40:31 +00001739Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001740 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1741 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001742 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001743 while (true) {
1744 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001745 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001746 cutOffParsing();
1747 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001748 }
1749
1750 if (Tok.isNot(tok::identifier)) {
1751 Diag(Tok, diag::err_expected_ident);
1752 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001753 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001754 }
1755
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001756 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1757 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001758 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001759 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001760
Chris Lattner0ef13522007-10-09 17:51:17 +00001761 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001762 break;
1763 ConsumeToken(); // consume ','
1764 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001765 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001766 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001767}
Mike Stump11289f42009-09-09 15:08:12 +00001768
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001769/// objc-throw-statement:
1770/// throw expression[opt];
1771///
John McCalldadc5752010-08-24 06:29:42 +00001772StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1773 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001774 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001775 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001776 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001777 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001778 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001779 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001780 }
1781 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001782 // consume ';'
1783 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001784 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001785}
1786
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001787/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001788/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001789///
John McCalldadc5752010-08-24 06:29:42 +00001790StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001791Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001792 ConsumeToken(); // consume synchronized
1793 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001794 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001795 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001796 }
John McCalld9bb7432011-07-27 21:50:02 +00001797
1798 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001799 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001800 ExprResult operand(ParseExpression());
1801
1802 if (Tok.is(tok::r_paren)) {
1803 ConsumeParen(); // ')'
1804 } else {
1805 if (!operand.isInvalid())
1806 Diag(Tok, diag::err_expected_rparen);
1807
1808 // Skip forward until we see a left brace, but don't consume it.
1809 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001810 }
John McCalld9bb7432011-07-27 21:50:02 +00001811
1812 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001813 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001814 if (!operand.isInvalid())
1815 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001816 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001817 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001818
John McCalld9bb7432011-07-27 21:50:02 +00001819 // Check the @synchronized operand now.
1820 if (!operand.isInvalid())
1821 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001822
John McCalld9bb7432011-07-27 21:50:02 +00001823 // Parse the compound statement within a new scope.
1824 ParseScope bodyScope(this, Scope::DeclScope);
1825 StmtResult body(ParseCompoundStatementBody());
1826 bodyScope.Exit();
1827
1828 // If there was a semantic or parse error earlier with the
1829 // operand, fail now.
1830 if (operand.isInvalid())
1831 return StmtError();
1832
1833 if (body.isInvalid())
1834 body = Actions.ActOnNullStmt(Tok.getLocation());
1835
1836 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001837}
1838
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001839/// objc-try-catch-statement:
1840/// @try compound-statement objc-catch-list[opt]
1841/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1842///
1843/// objc-catch-list:
1844/// @catch ( parameter-declaration ) compound-statement
1845/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1846/// catch-parameter-declaration:
1847/// parameter-declaration
1848/// '...' [OBJC2]
1849///
John McCalldadc5752010-08-24 06:29:42 +00001850StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001851 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001852
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001853 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001854 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001855 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001856 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001857 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001858 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001859 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001860 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001861 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001862 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001863 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001864 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001865
Chris Lattner0ef13522007-10-09 17:51:17 +00001866 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001867 // At this point, we need to lookahead to determine if this @ is the start
1868 // of an @catch or @finally. We don't want to consume the @ token if this
1869 // is an @try or @encode or something else.
1870 Token AfterAt = GetLookAheadToken(1);
1871 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1872 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1873 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001874
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001875 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001876 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001877 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001878 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001879 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001880 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001881 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001882 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001883 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001884 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001885 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001886 ParseDeclarator(ParmDecl);
1887
Douglas Gregore11ee112010-04-23 23:01:43 +00001888 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001889 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001890 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001891 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001892 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001893
Steve Naroff65a00892009-04-07 22:56:58 +00001894 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001895
Steve Naroff65a00892009-04-07 22:56:58 +00001896 if (Tok.is(tok::r_paren))
1897 RParenLoc = ConsumeParen();
1898 else // Skip over garbage, until we get to ')'. Eat the ')'.
1899 SkipUntil(tok::r_paren, true, false);
1900
John McCalldadc5752010-08-24 06:29:42 +00001901 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001902 if (Tok.is(tok::l_brace))
1903 CatchBody = ParseCompoundStatementBody();
1904 else
1905 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001906 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001907 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001908
John McCalldadc5752010-08-24 06:29:42 +00001909 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001910 RParenLoc,
1911 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001912 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001913 if (!Catch.isInvalid())
1914 CatchStmts.push_back(Catch.release());
1915
Steve Naroffe6016792008-02-05 21:27:35 +00001916 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001917 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1918 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001919 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001920 }
1921 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001922 } else {
1923 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001924 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001925 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001926
John McCalldadc5752010-08-24 06:29:42 +00001927 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001928 if (Tok.is(tok::l_brace))
1929 FinallyBody = ParseCompoundStatementBody();
1930 else
1931 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001932 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001933 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001934 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001935 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001936 catch_or_finally_seen = true;
1937 break;
1938 }
1939 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001940 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001941 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001942 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001943 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001944
John McCallb268a282010-08-23 23:25:46 +00001945 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001946 CatchStmts,
John McCallb268a282010-08-23 23:25:46 +00001947 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001948}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001949
John McCall31168b02011-06-15 23:02:42 +00001950/// objc-autoreleasepool-statement:
1951/// @autoreleasepool compound-statement
1952///
1953StmtResult
1954Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1955 ConsumeToken(); // consume autoreleasepool
1956 if (Tok.isNot(tok::l_brace)) {
1957 Diag(Tok, diag::err_expected_lbrace);
1958 return StmtError();
1959 }
1960 // Enter a scope to hold everything within the compound stmt. Compound
1961 // statements can always hold declarations.
1962 ParseScope BodyScope(this, Scope::DeclScope);
1963
1964 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1965
1966 BodyScope.Exit();
1967 if (AutoreleasePoolBody.isInvalid())
1968 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1969 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1970 AutoreleasePoolBody.take());
1971}
1972
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001973/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1974/// for later parsing.
1975void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1976 LexedMethod* LM = new LexedMethod(this, MDecl);
1977 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1978 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001979 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001980 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001981 if (Tok.is(tok::kw_try)) {
1982 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00001983 if (Tok.is(tok::colon)) {
1984 Toks.push_back(Tok);
1985 ConsumeToken();
1986 while (Tok.isNot(tok::l_brace)) {
1987 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1988 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1989 }
1990 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001991 Toks.push_back(Tok); // also store '{'
1992 }
1993 else if (Tok.is(tok::colon)) {
1994 ConsumeToken();
1995 while (Tok.isNot(tok::l_brace)) {
1996 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1997 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1998 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001999 Toks.push_back(Tok); // also store '{'
2000 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002001 ConsumeBrace();
2002 // Consume everything up to (and including) the matching right brace.
2003 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002004 while (Tok.is(tok::kw_catch)) {
2005 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2006 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2007 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002008}
2009
Steve Naroff09bf8152007-09-06 21:24:23 +00002010/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002011///
John McCall48871652010-08-21 09:40:31 +00002012Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002013 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002014
John McCallfaf5fb42010-08-26 23:41:50 +00002015 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2016 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002017
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002018 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002019 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002020 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002021 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002022 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002023 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002024 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002025 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002026
Steve Naroffbb875722007-11-11 19:54:21 +00002027 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002028 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002029 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002030
Steve Naroffbb875722007-11-11 19:54:21 +00002031 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2032 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00002033
Steve Naroffbb875722007-11-11 19:54:21 +00002034 // If we didn't find the '{', bail out.
2035 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00002036 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002037 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002038
2039 if (!MDecl) {
2040 ConsumeBrace();
2041 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2042 return 0;
2043 }
2044
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002045 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002046 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002047 assert (CurParsedObjCImpl
2048 && "ParseObjCMethodDefinition - Method out of @implementation");
2049 // Consume the tokens and store them for later parsing.
2050 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002051 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002052}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002053
John McCalldadc5752010-08-24 06:29:42 +00002054StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002055 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002056 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002057 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002058 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002059 }
2060
2061 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002062 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002063
2064 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002065 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002066
2067 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002068 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002069
2070 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2071 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002072
John McCalldadc5752010-08-24 06:29:42 +00002073 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002074 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002075 // If the expression is invalid, skip ahead to the next semicolon. Not
2076 // doing this opens us up to the possibility of infinite loops if
2077 // ParseExpression does not consume any tokens.
2078 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002079 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002080 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002081
Steve Naroffe6016792008-02-05 21:27:35 +00002082 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002083 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002084 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002085}
2086
John McCalldadc5752010-08-24 06:29:42 +00002087ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002088 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002089 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002090 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002091 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002092 return ExprError();
2093
Ted Kremeneke65b0862012-03-06 20:05:56 +00002094 case tok::minus:
2095 case tok::plus: {
2096 tok::TokenKind Kind = Tok.getKind();
2097 SourceLocation OpLoc = ConsumeToken();
2098
2099 if (!Tok.is(tok::numeric_constant)) {
2100 const char *Symbol = 0;
2101 switch (Kind) {
2102 case tok::minus: Symbol = "-"; break;
2103 case tok::plus: Symbol = "+"; break;
2104 default: llvm_unreachable("missing unary operator case");
2105 }
2106 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2107 << Symbol;
2108 return ExprError();
2109 }
2110
2111 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2112 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002113 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002114 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002115 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002116
2117 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2118 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002119 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002120
2121 return ParsePostfixExpressionSuffix(
2122 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2123 }
2124
Chris Lattnere002fbe2007-12-12 01:04:12 +00002125 case tok::string_literal: // primary-expression: string-literal
2126 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002127 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002128
2129 case tok::char_constant:
2130 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2131
2132 case tok::numeric_constant:
2133 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2134
2135 case tok::kw_true: // Objective-C++, etc.
2136 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2137 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2138 case tok::kw_false: // Objective-C++, etc.
2139 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2140 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2141
2142 case tok::l_square:
2143 // Objective-C array literal
2144 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2145
2146 case tok::l_brace:
2147 // Objective-C dictionary literal
2148 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2149
Patrick Beard0caa3942012-04-19 00:25:12 +00002150 case tok::l_paren:
2151 // Objective-C boxed expression
2152 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2153
Chris Lattnere002fbe2007-12-12 01:04:12 +00002154 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002155 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002156 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002157
Chris Lattner197a3012008-08-05 06:19:09 +00002158 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2159 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002160 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002161 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002162 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002163 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002164 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002165 default: {
2166 const char *str = 0;
2167 if (GetLookAheadToken(1).is(tok::l_brace)) {
2168 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2169 str =
2170 ch == 't' ? "try"
2171 : (ch == 'f' ? "finally"
2172 : (ch == 'a' ? "autoreleasepool" : 0));
2173 }
2174 if (str) {
2175 SourceLocation kwLoc = Tok.getLocation();
2176 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2177 FixItHint::CreateReplacement(kwLoc, str));
2178 }
2179 else
2180 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2181 }
Chris Lattner197a3012008-08-05 06:19:09 +00002182 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002183 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002184}
2185
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002186/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002187///
2188/// This routine parses the receiver of a message send in
2189/// Objective-C++ either as a type or as an expression. Note that this
2190/// routine must not be called to parse a send to 'super', since it
2191/// has no way to return such a result.
2192///
2193/// \param IsExpr Whether the receiver was parsed as an expression.
2194///
2195/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2196/// IsExpr is true), the parsed expression. If the receiver was parsed
2197/// as a type (\c IsExpr is false), the parsed type.
2198///
2199/// \returns True if an error occurred during parsing or semantic
2200/// analysis, in which case the arguments do not have valid
2201/// values. Otherwise, returns false for a successful parse.
2202///
2203/// objc-receiver: [C++]
2204/// 'super' [not parsed here]
2205/// expression
2206/// simple-type-specifier
2207/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002208bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002209 InMessageExpressionRAIIObject InMessage(*this, true);
2210
Douglas Gregor8d4de672010-04-21 22:36:40 +00002211 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2212 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2213 TryAnnotateTypeOrScopeToken();
2214
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002215 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002216 // objc-receiver:
2217 // expression
John McCalldadc5752010-08-24 06:29:42 +00002218 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002219 if (Receiver.isInvalid())
2220 return true;
2221
2222 IsExpr = true;
2223 TypeOrExpr = Receiver.take();
2224 return false;
2225 }
2226
2227 // objc-receiver:
2228 // typename-specifier
2229 // simple-type-specifier
2230 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002231 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002232 ParseCXXSimpleTypeSpecifier(DS);
2233
2234 if (Tok.is(tok::l_paren)) {
2235 // If we see an opening parentheses at this point, we are
2236 // actually parsing an expression that starts with a
2237 // function-style cast, e.g.,
2238 //
2239 // postfix-expression:
2240 // simple-type-specifier ( expression-list [opt] )
2241 // typename-specifier ( expression-list [opt] )
2242 //
2243 // Parse the remainder of this case, then the (optional)
2244 // postfix-expression suffix, followed by the (optional)
2245 // right-hand side of the binary expression. We have an
2246 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002247 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002248 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002249 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002250 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002251 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002252 if (Receiver.isInvalid())
2253 return true;
2254
2255 IsExpr = true;
2256 TypeOrExpr = Receiver.take();
2257 return false;
2258 }
2259
2260 // We have a class message. Turn the simple-type-specifier or
2261 // typename-specifier we parsed into a type and parse the
2262 // remainder of the class message.
2263 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002264 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002265 if (Type.isInvalid())
2266 return true;
2267
2268 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002269 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002270 return false;
2271}
2272
Douglas Gregor990ccac2010-05-31 14:40:22 +00002273/// \brief Determine whether the parser is currently referring to a an
2274/// Objective-C message send, using a simplified heuristic to avoid overhead.
2275///
2276/// This routine will only return true for a subset of valid message-send
2277/// expressions.
2278bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002279 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002280 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002281 return GetLookAheadToken(1).is(tok::identifier) &&
2282 GetLookAheadToken(2).is(tok::identifier);
2283}
2284
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002285bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002286 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002287 InMessageExpression)
2288 return false;
2289
2290
2291 ParsedType Type;
2292
2293 if (Tok.is(tok::annot_typename))
2294 Type = getTypeAnnotation(Tok);
2295 else if (Tok.is(tok::identifier))
2296 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2297 getCurScope());
2298 else
2299 return false;
2300
2301 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2302 const Token &AfterNext = GetLookAheadToken(2);
2303 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2304 if (Tok.is(tok::identifier))
2305 TryAnnotateTypeOrScopeToken();
2306
2307 return Tok.is(tok::annot_typename);
2308 }
2309 }
2310
2311 return false;
2312}
2313
Mike Stump11289f42009-09-09 15:08:12 +00002314/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002315/// '[' objc-receiver objc-message-args ']'
2316///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002317/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002318/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002319/// expression
2320/// class-name
2321/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002322///
John McCalldadc5752010-08-24 06:29:42 +00002323ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002324 assert(Tok.is(tok::l_square) && "'[' expected");
2325 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2326
Douglas Gregora817a192010-05-27 23:06:34 +00002327 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002328 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002329 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002330 return ExprError();
2331 }
2332
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002333 InMessageExpressionRAIIObject InMessage(*this, true);
2334
David Blaikiebbafb8a2012-03-11 07:00:24 +00002335 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002336 // We completely separate the C and C++ cases because C++ requires
2337 // more complicated (read: slower) parsing.
2338
2339 // Handle send to super.
2340 // FIXME: This doesn't benefit from the same typo-correction we
2341 // get in Objective-C.
2342 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002343 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002344 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2345 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002346
2347 // Parse the receiver, which is either a type or an expression.
2348 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002349 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002350 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2351 SkipUntil(tok::r_square);
2352 return ExprError();
2353 }
2354
2355 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002356 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2357 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002358 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002359
2360 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002361 ParsedType::getFromOpaquePtr(TypeOrExpr),
2362 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002363 }
2364
2365 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002366 IdentifierInfo *Name = Tok.getIdentifierInfo();
2367 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002368 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002369 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002370 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002371 NextToken().is(tok::period),
2372 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002373 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002374 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2375 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002376
John McCallfaf5fb42010-08-26 23:41:50 +00002377 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002378 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002379 SkipUntil(tok::r_square);
2380 return ExprError();
2381 }
2382
Douglas Gregore5798dc2010-04-21 20:38:13 +00002383 ConsumeToken(); // the type name
2384
2385 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002386 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002387
John McCallfaf5fb42010-08-26 23:41:50 +00002388 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002389 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002390 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002391 }
Chris Lattner8f697062008-01-25 18:59:06 +00002392 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002393
2394 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002395 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002396 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002397 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002398 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002399 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002400
John McCallba7bf592010-08-24 05:47:05 +00002401 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2402 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002403}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002404
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002405/// \brief Parse the remainder of an Objective-C message following the
2406/// '[' objc-receiver.
2407///
2408/// This routine handles sends to super, class messages (sent to a
2409/// class name), and instance messages (sent to an object), and the
2410/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2411/// ReceiverExpr, respectively. Only one of these parameters may have
2412/// a valid value.
2413///
2414/// \param LBracLoc The location of the opening '['.
2415///
2416/// \param SuperLoc If this is a send to 'super', the location of the
2417/// 'super' keyword that indicates a send to the superclass.
2418///
2419/// \param ReceiverType If this is a class message, the type of the
2420/// class we are sending a message to.
2421///
2422/// \param ReceiverExpr If this is an instance message, the expression
2423/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002424///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002425/// objc-message-args:
2426/// objc-selector
2427/// objc-keywordarg-list
2428///
2429/// objc-keywordarg-list:
2430/// objc-keywordarg
2431/// objc-keywordarg-list objc-keywordarg
2432///
Mike Stump11289f42009-09-09 15:08:12 +00002433/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002434/// selector-name[opt] ':' objc-keywordexpr
2435///
2436/// objc-keywordexpr:
2437/// nonempty-expr-list
2438///
2439/// nonempty-expr-list:
2440/// assignment-expression
2441/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002442///
John McCalldadc5752010-08-24 06:29:42 +00002443ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002444Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002445 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002446 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002447 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002448 InMessageExpressionRAIIObject InMessage(*this, true);
2449
Steve Naroffeae65032009-11-07 02:08:14 +00002450 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002451 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002452 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002453 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002454 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002455 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002456 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002457 else
John McCallb268a282010-08-23 23:25:46 +00002458 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002459 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002460 cutOffParsing();
2461 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002462 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002463
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002464 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002465 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002466 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002467
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002468 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002469 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002470 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002471
Chris Lattner0ef13522007-10-09 17:51:17 +00002472 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002473 while (1) {
2474 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002475 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002476 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002477
Chris Lattner0ef13522007-10-09 17:51:17 +00002478 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002479 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002480 // We must manually skip to a ']', otherwise the expression skipper will
2481 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2482 // the enclosing expression.
2483 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002484 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002485 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002486
Steve Narofff73590d2007-09-27 14:38:14 +00002487 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002488 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002489
2490 if (Tok.is(tok::code_completion)) {
2491 if (SuperLoc.isValid())
2492 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002493 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002494 /*AtArgumentEpression=*/true);
2495 else if (ReceiverType)
2496 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002497 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002498 /*AtArgumentEpression=*/true);
2499 else
2500 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002501 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002502 /*AtArgumentEpression=*/true);
2503
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002504 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002505 return ExprError();
2506 }
2507
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002508 ExprResult Expr;
2509 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2510 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2511 Expr = ParseBraceInitializer();
2512 } else
2513 Expr = ParseAssignmentExpression();
2514
2515 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002516 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002517 // We must manually skip to a ']', otherwise the expression skipper will
2518 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2519 // the enclosing expression.
2520 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002521 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002522 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002523
Steve Naroff486760a2007-09-17 20:25:27 +00002524 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002525 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002526
Douglas Gregor1b605f72009-11-19 01:08:35 +00002527 // Code completion after each argument.
2528 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002529 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002530 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002531 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002532 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002533 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002534 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002535 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002536 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002537 else
John McCallb268a282010-08-23 23:25:46 +00002538 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002539 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002540 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002541 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002542 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002543 }
2544
Steve Naroff486760a2007-09-17 20:25:27 +00002545 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002546 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002547 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002548 break;
2549 // We have a selector or a colon, continue parsing.
2550 }
2551 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002552 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002553 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002554 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002555 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002556 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002557 if (Tok.is(tok::colon)) {
2558 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2559 FixItHint::CreateRemoval(commaLoc);
2560 }
Chris Lattner197a3012008-08-05 06:19:09 +00002561 // We must manually skip to a ']', otherwise the expression skipper will
2562 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2563 // the enclosing expression.
2564 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002565 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002566 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002567
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002568 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002569 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002570 }
2571 } else if (!selIdent) {
2572 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002573
Chris Lattner197a3012008-08-05 06:19:09 +00002574 // We must manually skip to a ']', otherwise the expression skipper will
2575 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2576 // the enclosing expression.
2577 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002578 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002579 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002580
Chris Lattner0ef13522007-10-09 17:51:17 +00002581 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002582 if (Tok.is(tok::identifier))
2583 Diag(Tok, diag::err_expected_colon);
2584 else
2585 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002586 // We must manually skip to a ']', otherwise the expression skipper will
2587 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2588 // the enclosing expression.
2589 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002590 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002591 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002592
Chris Lattner8f697062008-01-25 18:59:06 +00002593 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002594
Steve Naroffe61bfa82007-10-05 18:42:47 +00002595 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002596 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002597 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002598 KeyLocs.push_back(Loc);
2599 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002600 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002601
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002602 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002603 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002604 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002605 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002606 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002607 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002608 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002609 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002610}
2611
John McCalldadc5752010-08-24 06:29:42 +00002612ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2613 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002614 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002615
Chris Lattnere002fbe2007-12-12 01:04:12 +00002616 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2617 // expressions. At this point, we know that the only valid thing that starts
2618 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002619 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002620 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002621 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002622 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002623
Chris Lattnere002fbe2007-12-12 01:04:12 +00002624 while (Tok.is(tok::at)) {
2625 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002626
Sebastian Redlc13f2682008-12-09 20:22:58 +00002627 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002628 if (!isTokenStringLiteral())
2629 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002630
John McCalldadc5752010-08-24 06:29:42 +00002631 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002632 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002633 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002634
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002635 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002636 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002637
Nico Webera7c7e602012-12-31 00:28:03 +00002638 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2639 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002640}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002641
Ted Kremeneke65b0862012-03-06 20:05:56 +00002642/// ParseObjCBooleanLiteral -
2643/// objc-scalar-literal : '@' boolean-keyword
2644/// ;
2645/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2646/// ;
2647ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2648 bool ArgValue) {
2649 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2650 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2651}
2652
2653/// ParseObjCCharacterLiteral -
2654/// objc-scalar-literal : '@' character-literal
2655/// ;
2656ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2657 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2658 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002659 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002660 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002661 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002662 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002663}
2664
2665/// ParseObjCNumericLiteral -
2666/// objc-scalar-literal : '@' scalar-literal
2667/// ;
2668/// scalar-literal : | numeric-constant /* any numeric constant. */
2669/// ;
2670ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2671 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2672 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002673 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002674 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002675 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002676 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002677}
2678
Patrick Beard0caa3942012-04-19 00:25:12 +00002679/// ParseObjCBoxedExpr -
2680/// objc-box-expression:
2681/// @( assignment-expression )
2682ExprResult
2683Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2684 if (Tok.isNot(tok::l_paren))
2685 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2686
2687 BalancedDelimiterTracker T(*this, tok::l_paren);
2688 T.consumeOpen();
2689 ExprResult ValueExpr(ParseAssignmentExpression());
2690 if (T.consumeClose())
2691 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002692
2693 if (ValueExpr.isInvalid())
2694 return ExprError();
2695
Patrick Beard0caa3942012-04-19 00:25:12 +00002696 // Wrap the sub-expression in a parenthesized expression, to distinguish
2697 // a boxed expression from a literal.
2698 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2699 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Webera7c7e602012-12-31 00:28:03 +00002700 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2701 ValueExpr.take());
Patrick Beard0caa3942012-04-19 00:25:12 +00002702}
2703
Ted Kremeneke65b0862012-03-06 20:05:56 +00002704ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002705 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002706 ConsumeBracket(); // consume the l_square.
2707
2708 while (Tok.isNot(tok::r_square)) {
2709 // Parse list of array element expressions (all must be id types).
2710 ExprResult Res(ParseAssignmentExpression());
2711 if (Res.isInvalid()) {
2712 // We must manually skip to a ']', otherwise the expression skipper will
2713 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2714 // the enclosing expression.
2715 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002716 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002717 }
2718
2719 // Parse the ellipsis that indicates a pack expansion.
2720 if (Tok.is(tok::ellipsis))
2721 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2722 if (Res.isInvalid())
2723 return true;
2724
2725 ElementExprs.push_back(Res.release());
2726
2727 if (Tok.is(tok::comma))
2728 ConsumeToken(); // Eat the ','.
2729 else if (Tok.isNot(tok::r_square))
2730 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2731 }
2732 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002733 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002734 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002735}
2736
2737ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2738 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2739 ConsumeBrace(); // consume the l_square.
2740 while (Tok.isNot(tok::r_brace)) {
2741 // Parse the comma separated key : value expressions.
2742 ExprResult KeyExpr;
2743 {
2744 ColonProtectionRAIIObject X(*this);
2745 KeyExpr = ParseAssignmentExpression();
2746 if (KeyExpr.isInvalid()) {
2747 // We must manually skip to a '}', otherwise the expression skipper will
2748 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2749 // the enclosing expression.
2750 SkipUntil(tok::r_brace);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002751 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002752 }
2753 }
2754
2755 if (Tok.is(tok::colon)) {
2756 ConsumeToken();
2757 } else {
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002758 Diag(Tok, diag::err_expected_colon);
2759 SkipUntil(tok::r_brace);
2760 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002761 }
2762
2763 ExprResult ValueExpr(ParseAssignmentExpression());
2764 if (ValueExpr.isInvalid()) {
2765 // We must manually skip to a '}', otherwise the expression skipper will
2766 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2767 // the enclosing expression.
2768 SkipUntil(tok::r_brace);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002769 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002770 }
2771
2772 // Parse the ellipsis that designates this as a pack expansion.
2773 SourceLocation EllipsisLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002774 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremeneke65b0862012-03-06 20:05:56 +00002775 EllipsisLoc = ConsumeToken();
2776
2777 // We have a valid expression. Collect it in a vector so we can
2778 // build the argument list.
2779 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002780 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002781 };
2782 Elements.push_back(Element);
2783
2784 if (Tok.is(tok::comma))
2785 ConsumeToken(); // Eat the ','.
2786 else if (Tok.isNot(tok::r_brace))
2787 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2788 }
2789 SourceLocation EndLoc = ConsumeBrace();
2790
2791 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002792 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2793 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002794}
2795
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002796/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002797/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002798ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002799Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002800 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002801
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002802 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002803
Chris Lattner197a3012008-08-05 06:19:09 +00002804 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002805 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2806
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002807 BalancedDelimiterTracker T(*this, tok::l_paren);
2808 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002809
Douglas Gregor220cac52009-02-18 17:45:20 +00002810 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002811
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002812 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002813
Douglas Gregor220cac52009-02-18 17:45:20 +00002814 if (Ty.isInvalid())
2815 return ExprError();
2816
Nico Webera7c7e602012-12-31 00:28:03 +00002817 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2818 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002819}
Anders Carlssone01493d2007-08-23 15:25:28 +00002820
2821/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002822/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002823ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002824Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002825 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002826
Chris Lattner197a3012008-08-05 06:19:09 +00002827 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002828 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2829
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002830 BalancedDelimiterTracker T(*this, tok::l_paren);
2831 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002832
Chris Lattner197a3012008-08-05 06:19:09 +00002833 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002834 return ExprError(Diag(Tok, diag::err_expected_ident));
2835
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002836 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002837 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002838
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002839 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002840
Nico Webera7c7e602012-12-31 00:28:03 +00002841 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2842 T.getOpenLocation(), ProtoIdLoc,
2843 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002844}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002845
2846/// objc-selector-expression
2847/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002848ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002849 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002850
Chris Lattner197a3012008-08-05 06:19:09 +00002851 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002852 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2853
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002854 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002855 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002856
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002857 BalancedDelimiterTracker T(*this, tok::l_paren);
2858 T.consumeOpen();
2859
Douglas Gregor67c692c2010-08-26 15:07:07 +00002860 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002861 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002862 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002863 return ExprError();
2864 }
2865
Chris Lattner4f472a32009-04-11 18:13:45 +00002866 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002867 if (!SelIdent && // missing selector name.
2868 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002869 return ExprError(Diag(Tok, diag::err_expected_ident));
2870
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002871 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002872 unsigned nColons = 0;
2873 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002874 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002875 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2876 ++nColons;
2877 KeyIdents.push_back(0);
2878 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002879 return ExprError(Diag(Tok, diag::err_expected_colon));
2880
Chris Lattner1ba64452010-08-27 22:32:41 +00002881 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002882 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002883 if (Tok.is(tok::r_paren))
2884 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002885
2886 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002887 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002888 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002889 return ExprError();
2890 }
2891
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002892 // Check for another keyword selector.
2893 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002894 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002895 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002896 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002897 break;
2898 }
Steve Naroff152dd812007-12-05 22:21:29 +00002899 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002900 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002901 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00002902 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2903 T.getOpenLocation(),
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002904 T.getCloseLocation());
Gabor Greif24032f12007-10-19 15:38:32 +00002905 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002906
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002907void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2908 // MCDecl might be null due to error in method or c-function prototype, etc.
2909 Decl *MCDecl = LM.D;
2910 bool skip = MCDecl &&
2911 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2912 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2913 if (skip)
2914 return;
2915
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002916 // Save the current token position.
2917 SourceLocation OrigLoc = Tok.getLocation();
2918
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002919 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2920 // Append the current token at the end of the new token stream so that it
2921 // doesn't get lost.
2922 LM.Toks.push_back(Tok);
2923 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2924
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002925 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00002926 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002927
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002928 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2929 Tok.is(tok::colon)) &&
2930 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002931 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002932 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002933 parseMethod
2934 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2935 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002936
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002937 // Tell the actions module that we have entered a method or c-function definition
2938 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00002939 if (parseMethod)
2940 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2941 else
2942 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002943 if (Tok.is(tok::kw_try))
2944 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002945 else {
2946 if (Tok.is(tok::colon))
2947 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002948 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002949 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00002950
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002951 if (Tok.getLocation() != OrigLoc) {
2952 // Due to parsing error, we either went over the cached tokens or
2953 // there are still cached tokens left. If it's the latter case skip the
2954 // leftover tokens.
2955 // Since this is an uncommon situation that should be avoided, use the
2956 // expensive isBeforeInTranslationUnit call.
2957 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2958 OrigLoc))
2959 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2960 ConsumeAnyToken();
2961 }
2962
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002963 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002964}