blob: 844a3d12e931821eeb0dc7ca084bd301d78102ae [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.
26void Parser::MaybeSkipAttributes() {
27 ParsedAttributes attrs(AttrFactory);
28 if (Tok.is(tok::kw___attribute)) {
29 Diag(Tok, diag::err_objc_postfix_attribute);
30 ParseGNUAttributes(attrs);
31 }
32}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000033
Chris Lattner3a907162008-12-08 21:53:24 +000034/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000035/// external-declaration: [C99 6.9]
36/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000037/// [OBJC] objc-class-declaration
38/// [OBJC] objc-alias-declaration
39/// [OBJC] objc-protocol-definition
40/// [OBJC] objc-method-definition
41/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000042Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000043 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000044
Douglas Gregorf48706c2009-12-07 09:27:33 +000045 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000046 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000047 cutOffParsing();
48 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000049 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000050
51 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000052 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000053 case tok::objc_class:
54 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000055 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000056 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000057 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
58 break;
John McCall53fa7142010-12-24 02:08:15 +000059 }
60 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000061 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000062 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000063 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000064 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000065 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000066 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000067 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000068 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000069 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
70 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000072 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
73 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000074 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000075 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
76 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000077 case tok::objc_import:
David Blaikiebbafb8a2012-03-11 07:00:24 +000078 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000079 return ParseModuleImport(AtLoc);
80
81 // Fall through
82
Chris Lattnerce90ef52008-08-23 02:02:23 +000083 default:
84 Diag(AtLoc, diag::err_unexpected_at);
85 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000086 SingleDecl = 0;
87 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000088 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000089 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000090}
91
92///
Mike Stump11289f42009-09-09 15:08:12 +000093/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000095///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000096Parser::DeclGroupPtrTy
97Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000099 SmallVector<IdentifierInfo *, 8> ClassNames;
100 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +0000101
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000103 while (1) {
Nico Weber04e213b2013-04-03 17:36:11 +0000104 MaybeSkipAttributes();
Chris Lattner0ef13522007-10-09 17:51:17 +0000105 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +0000106 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000107 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000108 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000109 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000110 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000111 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000112 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner0ef13522007-10-09 17:51:17 +0000114 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000115 break;
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000117 ConsumeToken();
118 }
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000120 // Consume the ';'.
121 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000122 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremeneka26da852009-11-17 23:12:20 +0000124 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
125 ClassLocs.data(),
126 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000127}
128
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000129void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
130{
131 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
132 if (ock == Sema::OCK_None)
133 return;
134
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000135 Decl *Decl = Actions.getObjCDeclContext();
136 if (CurParsedObjCImpl) {
137 CurParsedObjCImpl->finish(AtLoc);
138 } else {
139 Actions.ActOnAtEnd(getCurScope(), AtLoc);
140 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000141 Diag(AtLoc, diag::err_objc_missing_end)
142 << FixItHint::CreateInsertion(AtLoc, "@end\n");
143 if (Decl)
144 Diag(Decl->getLocStart(), diag::note_objc_container_start)
145 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000146}
147
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000148///
149/// objc-interface:
150/// objc-class-interface-attributes[opt] objc-class-interface
151/// objc-category-interface
152///
153/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000154/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000155/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000156/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000157/// objc-interface-decl-list
158/// @end
159///
160/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000161/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000162/// objc-protocol-refs[opt]
163/// objc-interface-decl-list
164/// @end
165///
166/// objc-superclass:
167/// ':' identifier
168///
169/// objc-class-interface-attributes:
170/// __attribute__((visibility("default")))
171/// __attribute__((visibility("hidden")))
172/// __attribute__((deprecated))
173/// __attribute__((unavailable))
174/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000175/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000176///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000177Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000178 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000179 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000181 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000183
Douglas Gregor49c22a72009-11-18 16:26:39 +0000184 // Code completion after '@interface'.
185 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000186 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000187 cutOffParsing();
188 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000189 }
190
Nico Weber04e213b2013-04-03 17:36:11 +0000191 MaybeSkipAttributes();
192
Chris Lattner0ef13522007-10-09 17:51:17 +0000193 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000194 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000195 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000196 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000197
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000198 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000199 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000200 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000201 if (Tok.is(tok::l_paren) &&
202 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000203
204 BalancedDelimiterTracker T(*this, tok::l_paren);
205 T.consumeOpen();
206
207 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000208 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000209 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000210 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000211 cutOffParsing();
212 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000213 }
214
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000215 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000216 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000217 categoryId = Tok.getIdentifierInfo();
218 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000219 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000220 else if (!getLangOpts().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000221 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000222 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000223 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000224
225 T.consumeClose();
226 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000227 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000228
229 if (!attrs.empty()) { // categories don't support attributes.
230 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
231 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000232 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000233
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000234 // Next, we need to check for any protocol references.
235 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000236 SmallVector<Decl *, 8> ProtocolRefs;
237 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000238 if (Tok.is(tok::less) &&
239 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000240 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000241 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000242
John McCall48871652010-08-21 09:40:31 +0000243 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000244 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000245 nameId, nameLoc,
246 categoryId, categoryLoc,
247 ProtocolRefs.data(),
248 ProtocolRefs.size(),
249 ProtocolLocs.data(),
250 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000251
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000252 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000253 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000254
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000255 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000256 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000257 }
258 // Parse a class interface.
259 IdentifierInfo *superClassId = 0;
260 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000261
Chris Lattner0ef13522007-10-09 17:51:17 +0000262 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000263 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000264
265 // Code completion of superclass names.
266 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000267 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000268 cutOffParsing();
269 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000270 }
271
Chris Lattner0ef13522007-10-09 17:51:17 +0000272 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000273 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000274 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000275 }
276 superClassId = Tok.getIdentifierInfo();
277 superClassLoc = ConsumeToken();
278 }
279 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 SmallVector<Decl *, 8> ProtocolRefs;
281 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000282 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000283 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000284 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
285 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000286 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000287
John McCall48871652010-08-21 09:40:31 +0000288 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000289 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000290 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000291 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000292 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000293 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000294
Chris Lattner0ef13522007-10-09 17:51:17 +0000295 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000296 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000297
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000298 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000299 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000300}
301
John McCall064d77b2009-12-03 22:31:13 +0000302/// The Objective-C property callback. This should be defined where
303/// it's used, but instead it's been lifted to here to support VS2005.
304struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie68e081d2011-12-20 02:48:34 +0000305private:
306 virtual void anchor();
307public:
John McCall064d77b2009-12-03 22:31:13 +0000308 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000309 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000310 ObjCDeclSpec &OCDS;
311 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000312 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000313 tok::ObjCKeywordKind MethodImplKind;
314
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000315 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000316 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000317 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000318 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000319 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000320 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000321 MethodImplKind(MethodImplKind) {
322 }
323
Eli Friedman934dbbf2012-08-08 23:53:27 +0000324 void invoke(ParsingFieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000325 if (FD.D.getIdentifier() == 0) {
326 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
327 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000328 return;
John McCall064d77b2009-12-03 22:31:13 +0000329 }
330 if (FD.BitfieldSize) {
331 P.Diag(AtLoc, diag::err_objc_property_bitfield)
332 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000333 return;
John McCall064d77b2009-12-03 22:31:13 +0000334 }
335
336 // Install the property declarator into interfaceDecl.
337 IdentifierInfo *SelName =
338 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
339
340 Selector GetterSel =
341 P.PP.getSelectorTable().getNullarySelector(SelName);
342 IdentifierInfo *SetterName = OCDS.getSetterName();
343 Selector SetterSel;
344 if (SetterName)
345 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
346 else
347 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
348 P.PP.getSelectorTable(),
349 FD.D.getIdentifier());
350 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000351 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000352 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
353 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000354 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000355 &isOverridingProperty,
356 MethodImplKind);
357 if (!isOverridingProperty)
358 Props.push_back(Property);
359
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000360 FD.complete(Property);
John McCall064d77b2009-12-03 22:31:13 +0000361 }
362};
363
David Blaikie68e081d2011-12-20 02:48:34 +0000364void Parser::ObjCPropertyCallback::anchor() {
365}
366
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000367/// objc-interface-decl-list:
368/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000369/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000370/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000371/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000372/// objc-interface-decl-list declaration
373/// objc-interface-decl-list ';'
374///
Steve Naroff99264b42007-08-22 16:35:03 +0000375/// objc-method-requirement: [OBJC2]
376/// @required
377/// @optional
378///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000379void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
380 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000381 SmallVector<Decl *, 32> allMethods;
382 SmallVector<Decl *, 16> allProperties;
383 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000384 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenekc7c64312010-01-07 01:20:12 +0000386 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000387
Steve Naroff99264b42007-08-22 16:35:03 +0000388 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000389 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000390 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000391 if (Decl *methodPrototype =
392 ParseObjCMethodPrototype(MethodImplKind, false))
393 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000394 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
395 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000396 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
397 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
398 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
399 if (Tok.is(tok::semi))
400 ConsumeToken();
401 }
Steve Naroff99264b42007-08-22 16:35:03 +0000402 continue;
403 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000404 if (Tok.is(tok::l_paren)) {
405 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000406 ParseObjCMethodDecl(Tok.getLocation(),
407 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000408 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000409 continue;
410 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000411 // Ignore excess semicolons.
412 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000413 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000414 continue;
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattnerda9fb152008-10-20 06:10:06 +0000417 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000418 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000419 break;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregorf1934162010-01-13 21:24:21 +0000421 // Code completion within an Objective-C interface.
422 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000423 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000424 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000425 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000426 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000427 }
428
Chris Lattner038a3e32008-10-20 05:46:22 +0000429 // If we don't have an @ directive, parse it as a function definition.
430 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000431 // The code below does not consume '}'s because it is afraid of eating the
432 // end of a namespace. Because of the way this code is structured, an
433 // erroneous r_brace would cause an infinite loop if not handled here.
434 if (Tok.is(tok::r_brace))
435 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000436 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000437 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000438 continue;
439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner038a3e32008-10-20 05:46:22 +0000441 // Otherwise, we have an @ directive, eat the @.
442 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000443 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000444 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000445 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000446 }
447
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000448 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000449
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000450 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000451 AtEnd.setBegin(AtLoc);
452 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000453 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000454 } else if (DirectiveKind == tok::objc_not_keyword) {
455 Diag(Tok, diag::err_objc_unknown_at);
456 SkipUntil(tok::semi);
457 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattnerda9fb152008-10-20 06:10:06 +0000460 // Eat the identifier.
461 ConsumeToken();
462
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000463 switch (DirectiveKind) {
464 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000465 // FIXME: If someone forgets an @end on a protocol, this loop will
466 // continue to eat up tons of stuff and spew lots of nonsense errors. It
467 // would probably be better to bail out if we saw an @class or @interface
468 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000469 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000470 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000471 SkipUntil(tok::r_brace, tok::at);
472 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000473
474 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000475 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000476 Diag(AtLoc, diag::err_objc_missing_end)
477 << FixItHint::CreateInsertion(AtLoc, "@end\n");
478 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
479 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000480 ConsumeToken();
481 break;
482
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000483 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000484 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000485 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000486 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000487 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000488 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000489 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000490 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000491 break;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000493 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000494 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000495 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000496
Chris Lattner038a3e32008-10-20 05:46:22 +0000497 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000498 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000499 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000500 if (Tok.is(tok::l_paren)) {
501 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000502 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000505 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000506 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000507
Chris Lattner038a3e32008-10-20 05:46:22 +0000508 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000509 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +0000510 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000511
John McCall405988b2011-03-26 01:53:26 +0000512 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000513 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000514 }
Steve Naroff99264b42007-08-22 16:35:03 +0000515 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000516
517 // We break out of the big loop in two cases: when we see @end or when we see
518 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000519 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000520 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000521 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000522 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000523 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000524 } else {
525 Diag(Tok, diag::err_objc_missing_end)
526 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
527 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
528 << (int) Actions.getObjCContainerKind();
529 AtEnd.setBegin(Tok.getLocation());
530 AtEnd.setEnd(Tok.getLocation());
531 }
Mike Stump11289f42009-09-09 15:08:12 +0000532
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000533 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000534 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000535 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000536 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000537 allProperties.data(), allProperties.size(),
538 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000539}
540
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000541/// Parse property attribute declarations.
542///
543/// property-attr-decl: '(' property-attrlist ')'
544/// property-attrlist:
545/// property-attribute
546/// property-attrlist ',' property-attribute
547/// property-attribute:
548/// getter '=' identifier
549/// setter '=' identifier ':'
550/// readonly
551/// readwrite
552/// assign
553/// retain
554/// copy
555/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000556/// atomic
557/// strong
558/// weak
559/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000560///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000561void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000562 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000563 BalancedDelimiterTracker T(*this, tok::l_paren);
564 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000565
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000566 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000567 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000568 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000569 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000570 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000571 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattner76619232008-10-20 07:22:18 +0000573 // If this is not an identifier at all, bail out early.
574 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000575 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000576 return;
577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner1db33542008-10-20 07:37:22 +0000579 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner68e48682008-11-20 04:42:34 +0000581 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000582 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000583 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000584 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000585 else if (II->isStr("unsafe_unretained"))
586 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000587 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000588 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000589 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000590 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000591 else if (II->isStr("strong"))
592 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000593 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000594 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000595 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000596 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000597 else if (II->isStr("atomic"))
598 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000599 else if (II->isStr("weak"))
600 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000601 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000602 bool IsSetter = II->getNameStart()[0] == 's';
603
Chris Lattner825bca12008-10-20 07:39:53 +0000604 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000605 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
606 diag::err_objc_expected_equal_for_getter;
607
608 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000609 return;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Douglas Gregorc8537c52009-11-19 07:41:15 +0000611 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000612 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000613 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000614 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000615 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000616 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000617 }
618
Anders Carlssonfe15a782010-10-02 17:45:21 +0000619
620 SourceLocation SelLoc;
621 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
622
623 if (!SelIdent) {
624 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
625 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000626 SkipUntil(tok::r_paren);
627 return;
628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Anders Carlssonfe15a782010-10-02 17:45:21 +0000630 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000631 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000632 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000633
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000634 if (ExpectAndConsume(tok::colon,
635 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000636 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000637 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000638 } else {
639 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000640 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000641 }
Chris Lattner825bca12008-10-20 07:39:53 +0000642 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000643 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000644 SkipUntil(tok::r_paren);
645 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Chris Lattner1db33542008-10-20 07:37:22 +0000648 if (Tok.isNot(tok::comma))
649 break;
Mike Stump11289f42009-09-09 15:08:12 +0000650
Chris Lattner1db33542008-10-20 07:37:22 +0000651 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000652 }
Mike Stump11289f42009-09-09 15:08:12 +0000653
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000654 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000655}
656
Steve Naroff09bf8152007-09-06 21:24:23 +0000657/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000658/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000659/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000660///
661/// objc-instance-method: '-'
662/// objc-class-method: '+'
663///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000664/// objc-method-attributes: [OBJC2]
665/// __attribute__((deprecated))
666///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000667Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000668 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000669 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000670
Mike Stump11289f42009-09-09 15:08:12 +0000671 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000672 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000673 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000674 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000675 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000676 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000677 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000678}
679
680/// objc-selector:
681/// identifier
682/// one of
683/// enum struct union if else while do for switch case default
684/// break continue return goto asm sizeof typeof __alignof
685/// unsigned long const short volatile signed restrict _Complex
686/// in out inout bycopy byref oneway int char float double void _Bool
687///
Chris Lattner4f472a32009-04-11 18:13:45 +0000688IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000689
Chris Lattner5700fab2007-10-07 02:00:24 +0000690 switch (Tok.getKind()) {
691 default:
692 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000693 case tok::ampamp:
694 case tok::ampequal:
695 case tok::amp:
696 case tok::pipe:
697 case tok::tilde:
698 case tok::exclaim:
699 case tok::exclaimequal:
700 case tok::pipepipe:
701 case tok::pipeequal:
702 case tok::caret:
703 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000704 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000705 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000706 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
707 Tok.setKind(tok::identifier);
708 SelectorLoc = ConsumeToken();
709 return II;
710 }
711 return 0;
712 }
713
Chris Lattner5700fab2007-10-07 02:00:24 +0000714 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000715 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000716 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000717 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000718 case tok::kw_break:
719 case tok::kw_case:
720 case tok::kw_catch:
721 case tok::kw_char:
722 case tok::kw_class:
723 case tok::kw_const:
724 case tok::kw_const_cast:
725 case tok::kw_continue:
726 case tok::kw_default:
727 case tok::kw_delete:
728 case tok::kw_do:
729 case tok::kw_double:
730 case tok::kw_dynamic_cast:
731 case tok::kw_else:
732 case tok::kw_enum:
733 case tok::kw_explicit:
734 case tok::kw_export:
735 case tok::kw_extern:
736 case tok::kw_false:
737 case tok::kw_float:
738 case tok::kw_for:
739 case tok::kw_friend:
740 case tok::kw_goto:
741 case tok::kw_if:
742 case tok::kw_inline:
743 case tok::kw_int:
744 case tok::kw_long:
745 case tok::kw_mutable:
746 case tok::kw_namespace:
747 case tok::kw_new:
748 case tok::kw_operator:
749 case tok::kw_private:
750 case tok::kw_protected:
751 case tok::kw_public:
752 case tok::kw_register:
753 case tok::kw_reinterpret_cast:
754 case tok::kw_restrict:
755 case tok::kw_return:
756 case tok::kw_short:
757 case tok::kw_signed:
758 case tok::kw_sizeof:
759 case tok::kw_static:
760 case tok::kw_static_cast:
761 case tok::kw_struct:
762 case tok::kw_switch:
763 case tok::kw_template:
764 case tok::kw_this:
765 case tok::kw_throw:
766 case tok::kw_true:
767 case tok::kw_try:
768 case tok::kw_typedef:
769 case tok::kw_typeid:
770 case tok::kw_typename:
771 case tok::kw_typeof:
772 case tok::kw_union:
773 case tok::kw_unsigned:
774 case tok::kw_using:
775 case tok::kw_virtual:
776 case tok::kw_void:
777 case tok::kw_volatile:
778 case tok::kw_wchar_t:
779 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000780 case tok::kw__Bool:
781 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000782 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000783 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000784 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000785 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000786 }
Steve Naroff99264b42007-08-22 16:35:03 +0000787}
788
Fariborz Jahanian83615522008-01-02 22:54:34 +0000789/// objc-for-collection-in: 'in'
790///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000791bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000792 // FIXME: May have to do additional look-ahead to only allow for
793 // valid tokens following an 'in'; such as an identifier, unary operators,
794 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000795 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000796 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000797}
798
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000799/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000800/// qualifier list and builds their bitmask representation in the input
801/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000802///
803/// objc-type-qualifiers:
804/// objc-type-qualifier
805/// objc-type-qualifiers objc-type-qualifier
806///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000807void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000808 Declarator::TheContext Context) {
809 assert(Context == Declarator::ObjCParameterContext ||
810 Context == Declarator::ObjCResultContext);
811
Chris Lattner61511e12007-12-12 06:56:32 +0000812 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000813 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000814 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000815 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000816 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000817 }
818
Chris Lattner5e530bc2007-12-27 19:57:00 +0000819 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000820 return;
Mike Stump11289f42009-09-09 15:08:12 +0000821
Chris Lattner61511e12007-12-12 06:56:32 +0000822 const IdentifierInfo *II = Tok.getIdentifierInfo();
823 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000824 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000825 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000826
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000827 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000828 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000829 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000830 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
831 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
832 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
833 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
834 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
835 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000836 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000837 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000838 ConsumeToken();
839 II = 0;
840 break;
841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattner61511e12007-12-12 06:56:32 +0000843 // If this wasn't a recognized qualifier, bail out.
844 if (II) return;
845 }
846}
847
John McCalla55902b2011-10-01 09:56:14 +0000848/// Take all the decl attributes out of the given list and add
849/// them to the given attribute set.
850static void takeDeclAttributes(ParsedAttributes &attrs,
851 AttributeList *list) {
852 while (list) {
853 AttributeList *cur = list;
854 list = cur->getNext();
855
856 if (!cur->isUsedAsTypeAttr()) {
857 // Clear out the next pointer. We're really completely
858 // destroying the internal invariants of the declarator here,
859 // but it doesn't matter because we're done with it.
860 cur->setNext(0);
861 attrs.add(cur);
862 }
863 }
864}
865
866/// takeDeclAttributes - Take all the decl attributes from the given
867/// declarator and add them to the given list.
868static void takeDeclAttributes(ParsedAttributes &attrs,
869 Declarator &D) {
870 // First, take ownership of all attributes.
871 attrs.getPool().takeAllFrom(D.getAttributePool());
872 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
873
874 // Now actually move the attributes over.
875 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
876 takeDeclAttributes(attrs, D.getAttributes());
877 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
878 takeDeclAttributes(attrs,
879 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
880}
881
Chris Lattner61511e12007-12-12 06:56:32 +0000882/// objc-type-name:
883/// '(' objc-type-qualifiers[opt] type-name ')'
884/// '(' objc-type-qualifiers[opt] ')'
885///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000886ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000887 Declarator::TheContext context,
888 ParsedAttributes *paramAttrs) {
889 assert(context == Declarator::ObjCParameterContext ||
890 context == Declarator::ObjCResultContext);
891 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
892
Chris Lattner0ef13522007-10-09 17:51:17 +0000893 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000895 BalancedDelimiterTracker T(*this, tok::l_paren);
896 T.consumeOpen();
897
Chris Lattner2ebb1782008-08-23 01:48:03 +0000898 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000899 ObjCDeclContextSwitch ObjCDC(*this);
900
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000901 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000902 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000903
John McCallba7bf592010-08-24 05:47:05 +0000904 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000905 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000906 // Parse an abstract declarator.
907 DeclSpec declSpec(AttrFactory);
908 declSpec.setObjCQualifiers(&DS);
909 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +0000910 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +0000911 Declarator declarator(declSpec, context);
912 ParseDeclarator(declarator);
913
914 // If that's not invalid, extract a type.
915 if (!declarator.isInvalidType()) {
916 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
917 if (!type.isInvalid())
918 Ty = type.get();
919
920 // If we're parsing a parameter, steal all the decl attributes
921 // and add them to the decl spec.
922 if (context == Declarator::ObjCParameterContext)
923 takeDeclAttributes(*paramAttrs, declarator);
924 }
925 } else if (context == Declarator::ObjCResultContext &&
926 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000927 if (!Ident_instancetype)
928 Ident_instancetype = PP.getIdentifierInfo("instancetype");
929
930 if (Tok.getIdentifierInfo() == Ident_instancetype) {
931 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
932 ConsumeToken();
933 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000934 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000935
Steve Naroff90255b42008-10-21 14:15:04 +0000936 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000937 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000938 else if (Tok.getLocation() == TypeStartLoc) {
939 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000940 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000941 SkipUntil(tok::r_paren);
942 } else {
943 // Otherwise, we found *something*, but didn't get a ')' in the right
944 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000945 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000946 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000947 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000948}
949
950/// objc-method-decl:
951/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000952/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000953/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000954/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000955///
956/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000957/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000958/// objc-keyword-selector objc-keyword-decl
959///
960/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000961/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
962/// objc-selector ':' objc-keyword-attributes[opt] identifier
963/// ':' objc-type-name objc-keyword-attributes[opt] identifier
964/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000965///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000966/// objc-parmlist:
967/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000968///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000969/// objc-parms:
970/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000971///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000972/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000973/// , ...
974///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000975/// objc-keyword-attributes: [OBJC2]
976/// __attribute__((unused))
977///
John McCall48871652010-08-21 09:40:31 +0000978Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000979 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000980 tok::ObjCKeywordKind MethodImplKind,
981 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000982 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000983
Douglas Gregor636a61e2010-04-07 00:21:17 +0000984 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000985 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000986 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000987 cutOffParsing();
988 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000989 }
990
Chris Lattner2ebb1782008-08-23 01:48:03 +0000991 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000992 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000993 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000994 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000995 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000996
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000997 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000998 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000999 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001000 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001001
Douglas Gregor636a61e2010-04-07 00:21:17 +00001002 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001003 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001004 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001005 cutOffParsing();
1006 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001007 }
1008
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001009 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001010 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001011 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001012
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001013 // An unnamed colon is valid.
1014 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001015 Diag(Tok, diag::err_expected_selector_for_method)
1016 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001017 // Skip until we get a ; or @.
1018 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCall48871652010-08-21 09:40:31 +00001019 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001020 }
Mike Stump11289f42009-09-09 15:08:12 +00001021
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001022 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001023 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001024 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001025 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001026 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattner5700fab2007-10-07 02:00:24 +00001028 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001029 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001030 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001031 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001032 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001033 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001034 methodAttrs.getList(), MethodImplKind,
1035 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001036 PD.complete(Result);
1037 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001038 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001039
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001040 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001041 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001042 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001043 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1044 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001045
1046 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001047 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001048 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001049 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner5700fab2007-10-07 02:00:24 +00001051 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001052 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001053 Diag(Tok, diag::err_expected_colon);
1054 break;
1055 }
1056 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001057
John McCallba7bf592010-08-24 05:47:05 +00001058 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001059 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001060 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1061 Declarator::ObjCParameterContext,
1062 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001063
Chris Lattner5700fab2007-10-07 02:00:24 +00001064 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001065 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001066 ArgInfo.ArgAttrs = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001067 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001068 MaybeParseGNUAttributes(paramAttrs);
1069 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001070 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001071
Douglas Gregor45879692010-07-08 23:37:41 +00001072 // Code completion for the next piece of the selector.
1073 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001074 KeyIdents.push_back(SelIdent);
1075 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1076 mType == tok::minus,
1077 /*AtParameterName=*/true,
1078 ReturnType,
1079 KeyIdents.data(),
1080 KeyIdents.size());
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,
Douglas Gregor95887f92010-07-08 23:20:03 +00001106 ReturnType,
1107 KeyIdents.data(),
1108 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001109 cutOffParsing();
1110 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001111 }
1112
Chris Lattner5700fab2007-10-07 02:00:24 +00001113 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001114 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001115 if (!SelIdent && Tok.isNot(tok::colon))
1116 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001117 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001118 SourceLocation ColonLoc = Tok.getLocation();
1119 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001120 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1121 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1122 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001123 }
1124 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001125 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001126 }
Mike Stump11289f42009-09-09 15:08:12 +00001127
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001128 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001129 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001130 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001131 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001132 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001133 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001134 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001135 ConsumeToken();
1136 break;
1137 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001138 if (!cStyleParamWarned) {
1139 Diag(Tok, diag::warn_cstyle_param);
1140 cStyleParamWarned = true;
1141 }
John McCall084e83d2011-03-24 11:26:52 +00001142 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001143 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001144 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001145 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1146 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001147 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001148 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001149 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1150 ParmDecl.getIdentifierLoc(),
1151 Param,
1152 0));
Chris Lattner5700fab2007-10-07 02:00:24 +00001153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001155 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001156 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001157 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001158 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001159
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001160 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001161 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001162
Chris Lattner5700fab2007-10-07 02:00:24 +00001163 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1164 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001165 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001166 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001167 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001168 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001169 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001170 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001171 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001172
John McCall28a6aea2009-11-04 02:18:39 +00001173 PD.complete(Result);
1174 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001175}
1176
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001177/// objc-protocol-refs:
1178/// '<' identifier-list '>'
1179///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001180bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001181ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1182 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001183 bool WarnOnDeclarations,
1184 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001185 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001186
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001187 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001188
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001189 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattner3bbae002008-07-26 04:03:38 +00001191 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001192 if (Tok.is(tok::code_completion)) {
1193 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1194 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001195 cutOffParsing();
1196 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001197 }
1198
Chris Lattner3bbae002008-07-26 04:03:38 +00001199 if (Tok.isNot(tok::identifier)) {
1200 Diag(Tok, diag::err_expected_ident);
1201 SkipUntil(tok::greater);
1202 return true;
1203 }
1204 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1205 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001206 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001207 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner3bbae002008-07-26 04:03:38 +00001209 if (Tok.isNot(tok::comma))
1210 break;
1211 ConsumeToken();
1212 }
Mike Stump11289f42009-09-09 15:08:12 +00001213
Chris Lattner3bbae002008-07-26 04:03:38 +00001214 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001215 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001216 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattner3bbae002008-07-26 04:03:38 +00001218 // Convert the list of protocols identifiers into a list of protocol decls.
1219 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1220 &ProtocolIdents[0], ProtocolIdents.size(),
1221 Protocols);
1222 return false;
1223}
1224
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001225/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1226/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001227bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001228 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001229 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001230 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001231 SmallVector<Decl *, 8> ProtocolDecl;
1232 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001233 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1234 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001235 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1236 ProtocolLocs.data(), LAngleLoc);
1237 if (EndProtoLoc.isValid())
1238 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001239 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001240}
1241
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001242void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1243 BalancedDelimiterTracker &T,
1244 SmallVectorImpl<Decl *> &AllIvarDecls,
1245 bool RBraceMissing) {
1246 if (!RBraceMissing)
1247 T.consumeClose();
1248
1249 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1250 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1251 Actions.ActOnObjCContainerFinishDefinition();
1252 // Call ActOnFields() even if we don't have any decls. This is useful
1253 // for code rewriting tools that need to be aware of the empty list.
1254 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1255 AllIvarDecls,
1256 T.getOpenLocation(), T.getCloseLocation(), 0);
1257}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001258
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001259/// objc-class-instance-variables:
1260/// '{' objc-instance-variable-decl-list[opt] '}'
1261///
1262/// objc-instance-variable-decl-list:
1263/// objc-visibility-spec
1264/// objc-instance-variable-decl ';'
1265/// ';'
1266/// objc-instance-variable-decl-list objc-visibility-spec
1267/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1268/// objc-instance-variable-decl-list ';'
1269///
1270/// objc-visibility-spec:
1271/// @private
1272/// @protected
1273/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001274/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001275///
1276/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001277/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001278///
John McCall48871652010-08-21 09:40:31 +00001279void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001280 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001281 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001282 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001283 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001284
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001285 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001286 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001287
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001288 BalancedDelimiterTracker T(*this, tok::l_brace);
1289 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001290 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001291 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001292 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001293
Steve Naroff00433d32007-08-21 21:17:12 +00001294 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001295 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001296 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001297 continue;
1298 }
Mike Stump11289f42009-09-09 15:08:12 +00001299
Steve Naroff00433d32007-08-21 21:17:12 +00001300 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001301 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001302 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001303
1304 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001305 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001306 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001307 }
1308
Steve Naroff7c348172007-08-23 18:16:40 +00001309 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001310 case tok::objc_private:
1311 case tok::objc_public:
1312 case tok::objc_protected:
1313 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001314 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001315 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001316 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001317
1318 case tok::objc_end:
1319 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001320 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1321 Tok.setKind(tok::at);
1322 Tok.setLength(1);
1323 PP.EnterToken(Tok);
1324 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1325 T, AllIvarDecls, true);
1326 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001327
1328 default:
1329 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1330 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001331 }
1332 }
Mike Stump11289f42009-09-09 15:08:12 +00001333
Douglas Gregor48d46252010-01-13 21:54:15 +00001334 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001335 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001336 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001337 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001338 }
1339
John McCallcfefb6d2009-11-03 02:38:08 +00001340 struct ObjCIvarCallback : FieldCallback {
1341 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001342 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001343 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001344 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001345
John McCall48871652010-08-21 09:40:31 +00001346 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001347 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001348 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1349 }
1350
Eli Friedman934dbbf2012-08-08 23:53:27 +00001351 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001352 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001353 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001354 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001355 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001356 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001357 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001358 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001359 if (Field)
1360 AllIvarDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001361 FD.complete(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001362 }
1363 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001364
Chris Lattnera12405b2008-04-10 06:46:29 +00001365 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001366 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001367 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001368
Chris Lattner0ef13522007-10-09 17:51:17 +00001369 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001370 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001371 } else {
1372 Diag(Tok, diag::err_expected_semi_decl_list);
1373 // Skip to end of block or statement
1374 SkipUntil(tok::r_brace, true, true);
1375 }
1376 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001377 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1378 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001379 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001380}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001381
1382/// objc-protocol-declaration:
1383/// objc-protocol-definition
1384/// objc-protocol-forward-reference
1385///
1386/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001387/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001388/// objc-protocol-refs[opt]
1389/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001390/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001391///
1392/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001393/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001394///
James Dennett1355bd12012-06-11 06:19:40 +00001395/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001396/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001397/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001398Parser::DeclGroupPtrTy
1399Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1400 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001401 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001402 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1403 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001404
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001405 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001406 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001407 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001408 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001409 }
1410
Nico Weber04e213b2013-04-03 17:36:11 +00001411 MaybeSkipAttributes();
1412
Chris Lattner0ef13522007-10-09 17:51:17 +00001413 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001414 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001415 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001416 }
1417 // Save the protocol name, then consume it.
1418 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1419 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001420
Chris Lattner0ef13522007-10-09 17:51:17 +00001421 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001422 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001423 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001424 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001425 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001426 }
Mike Stump11289f42009-09-09 15:08:12 +00001427
Erik Verbruggenf9887852011-12-08 09:58:43 +00001428 CheckNestedObjCContexts(AtLoc);
1429
Chris Lattner0ef13522007-10-09 17:51:17 +00001430 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001431 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001432 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1433
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001434 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001435 while (1) {
1436 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001437 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001438 Diag(Tok, diag::err_expected_ident);
1439 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001440 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001441 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001442 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1443 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001444 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001445
Chris Lattner0ef13522007-10-09 17:51:17 +00001446 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001447 break;
1448 }
1449 // Consume the ';'.
1450 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001451 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001452
Steve Naroff93eb5f12007-10-10 17:32:04 +00001453 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001454 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001455 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001456 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001459 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001460 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001461
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001462 SmallVector<Decl *, 8> ProtocolRefs;
1463 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001464 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001465 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1466 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001467 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001468
John McCall48871652010-08-21 09:40:31 +00001469 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001470 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001471 ProtocolRefs.data(),
1472 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001473 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001474 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001475
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001476 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001477 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001478}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001479
1480/// objc-implementation:
1481/// objc-class-implementation-prologue
1482/// objc-category-implementation-prologue
1483///
1484/// objc-class-implementation-prologue:
1485/// @implementation identifier objc-superclass[opt]
1486/// objc-class-instance-variables[opt]
1487///
1488/// objc-category-implementation-prologue:
1489/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001490Parser::DeclGroupPtrTy
1491Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001492 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1493 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001494 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001495 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001496
Douglas Gregor49c22a72009-11-18 16:26:39 +00001497 // Code completion after '@implementation'.
1498 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001499 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001500 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001501 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001502 }
1503
Nico Weber04e213b2013-04-03 17:36:11 +00001504 MaybeSkipAttributes();
1505
Chris Lattner0ef13522007-10-09 17:51:17 +00001506 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001507 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001508 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001509 }
1510 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001511 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001512 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001513 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001514
1515 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001516 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001517 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001518 SourceLocation categoryLoc, rparenLoc;
1519 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001521 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001522 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001523 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001524 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001525 }
1526
Chris Lattner0ef13522007-10-09 17:51:17 +00001527 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001528 categoryId = Tok.getIdentifierInfo();
1529 categoryLoc = ConsumeToken();
1530 } else {
1531 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001532 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001533 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001534 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001535 Diag(Tok, diag::err_expected_rparen);
1536 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001537 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001538 }
1539 rparenLoc = ConsumeParen();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001540 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001541 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001542 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001543
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001544 } else {
1545 // We have a class implementation
1546 SourceLocation superClassLoc;
1547 IdentifierInfo *superClassId = 0;
1548 if (Tok.is(tok::colon)) {
1549 // We have a super class
1550 ConsumeToken();
1551 if (Tok.isNot(tok::identifier)) {
1552 Diag(Tok, diag::err_expected_ident); // missing super class name.
1553 return DeclGroupPtrTy();
1554 }
1555 superClassId = Tok.getIdentifierInfo();
1556 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001557 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001558 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1559 AtLoc, nameId, nameLoc,
1560 superClassId, superClassLoc);
1561
1562 if (Tok.is(tok::l_brace)) // we have ivars
1563 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001564 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001565 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001566
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001567 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001568
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001569 {
1570 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1571 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1572 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001573 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001574 MaybeParseMicrosoftAttributes(attrs);
1575 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1576 DeclGroupRef DG = DGP.get();
1577 DeclsInGroup.append(DG.begin(), DG.end());
1578 }
1579 }
1580 }
1581
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001582 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001583}
Steve Naroff33a1e802007-10-29 21:38:07 +00001584
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001585Parser::DeclGroupPtrTy
1586Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001587 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1588 "ParseObjCAtEndDeclaration(): Expected @end");
1589 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001590 if (CurParsedObjCImpl)
1591 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001592 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001593 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001594 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001595 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001596}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001597
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001598Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1599 if (!Finished) {
1600 finish(P.Tok.getLocation());
1601 if (P.Tok.is(tok::eof)) {
1602 P.Diag(P.Tok, diag::err_objc_missing_end)
1603 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1604 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1605 << Sema::OCK_Implementation;
1606 }
1607 }
1608 P.CurParsedObjCImpl = 0;
1609 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001610}
1611
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001612void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1613 assert(!Finished);
1614 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1615 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001616 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1617 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001618
1619 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1620
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001621 if (HasCFunction)
1622 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1623 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1624 false/*c-functions*/);
1625
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001626 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001627 for (LateParsedObjCMethodContainer::iterator
1628 I = LateParsedObjCMethods.begin(),
1629 E = LateParsedObjCMethods.end(); I != E; ++I)
1630 delete *I;
1631 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001632
1633 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001634}
1635
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001636/// compatibility-alias-decl:
1637/// @compatibility_alias alias-name class-name ';'
1638///
John McCall48871652010-08-21 09:40:31 +00001639Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001640 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1641 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1642 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001643 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001644 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001645 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001646 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001647 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1648 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001649 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001650 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001651 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001652 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001653 IdentifierInfo *classId = Tok.getIdentifierInfo();
1654 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001655 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1656 "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001657 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1658 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001659}
1660
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001661/// property-synthesis:
1662/// @synthesize property-ivar-list ';'
1663///
1664/// property-ivar-list:
1665/// property-ivar
1666/// property-ivar-list ',' property-ivar
1667///
1668/// property-ivar:
1669/// identifier
1670/// identifier '=' identifier
1671///
John McCall48871652010-08-21 09:40:31 +00001672Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001673 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1674 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001675 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001676
Douglas Gregor88e72a02009-11-18 19:45:45 +00001677 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001678 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001679 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001680 cutOffParsing();
1681 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001682 }
1683
Douglas Gregor88e72a02009-11-18 19:45:45 +00001684 if (Tok.isNot(tok::identifier)) {
1685 Diag(Tok, diag::err_synthesized_property_name);
1686 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001687 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001688 }
1689
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001690 IdentifierInfo *propertyIvar = 0;
1691 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1692 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001693 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001694 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001695 // property '=' ivar-name
1696 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001697
1698 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001699 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001700 cutOffParsing();
1701 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001702 }
1703
Chris Lattner0ef13522007-10-09 17:51:17 +00001704 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001705 Diag(Tok, diag::err_expected_ident);
1706 break;
1707 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001708 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001709 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001710 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001711 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001712 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001713 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001714 break;
1715 ConsumeToken(); // consume ','
1716 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001717 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001718 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001719}
1720
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001721/// property-dynamic:
1722/// @dynamic property-list
1723///
1724/// property-list:
1725/// identifier
1726/// property-list ',' identifier
1727///
John McCall48871652010-08-21 09:40:31 +00001728Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001729 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1730 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001731 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001732 while (true) {
1733 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001734 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001735 cutOffParsing();
1736 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001737 }
1738
1739 if (Tok.isNot(tok::identifier)) {
1740 Diag(Tok, diag::err_expected_ident);
1741 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001742 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001743 }
1744
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001745 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1746 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001747 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001748 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001749
Chris Lattner0ef13522007-10-09 17:51:17 +00001750 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001751 break;
1752 ConsumeToken(); // consume ','
1753 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001754 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001755 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001756}
Mike Stump11289f42009-09-09 15:08:12 +00001757
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001758/// objc-throw-statement:
1759/// throw expression[opt];
1760///
John McCalldadc5752010-08-24 06:29:42 +00001761StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1762 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001763 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001764 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001765 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001766 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001767 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001768 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001769 }
1770 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001771 // consume ';'
1772 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001773 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001774}
1775
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001776/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001777/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001778///
John McCalldadc5752010-08-24 06:29:42 +00001779StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001780Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001781 ConsumeToken(); // consume synchronized
1782 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001783 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001784 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001785 }
John McCalld9bb7432011-07-27 21:50:02 +00001786
1787 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001788 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001789 ExprResult operand(ParseExpression());
1790
1791 if (Tok.is(tok::r_paren)) {
1792 ConsumeParen(); // ')'
1793 } else {
1794 if (!operand.isInvalid())
1795 Diag(Tok, diag::err_expected_rparen);
1796
1797 // Skip forward until we see a left brace, but don't consume it.
1798 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001799 }
John McCalld9bb7432011-07-27 21:50:02 +00001800
1801 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001802 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001803 if (!operand.isInvalid())
1804 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001805 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001806 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001807
John McCalld9bb7432011-07-27 21:50:02 +00001808 // Check the @synchronized operand now.
1809 if (!operand.isInvalid())
1810 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001811
John McCalld9bb7432011-07-27 21:50:02 +00001812 // Parse the compound statement within a new scope.
1813 ParseScope bodyScope(this, Scope::DeclScope);
1814 StmtResult body(ParseCompoundStatementBody());
1815 bodyScope.Exit();
1816
1817 // If there was a semantic or parse error earlier with the
1818 // operand, fail now.
1819 if (operand.isInvalid())
1820 return StmtError();
1821
1822 if (body.isInvalid())
1823 body = Actions.ActOnNullStmt(Tok.getLocation());
1824
1825 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001826}
1827
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001828/// objc-try-catch-statement:
1829/// @try compound-statement objc-catch-list[opt]
1830/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1831///
1832/// objc-catch-list:
1833/// @catch ( parameter-declaration ) compound-statement
1834/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1835/// catch-parameter-declaration:
1836/// parameter-declaration
1837/// '...' [OBJC2]
1838///
John McCalldadc5752010-08-24 06:29:42 +00001839StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001840 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001841
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001842 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001843 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001844 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001845 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001846 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001847 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001848 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001849 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001850 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001851 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001852 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001853 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001854
Chris Lattner0ef13522007-10-09 17:51:17 +00001855 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001856 // At this point, we need to lookahead to determine if this @ is the start
1857 // of an @catch or @finally. We don't want to consume the @ token if this
1858 // is an @try or @encode or something else.
1859 Token AfterAt = GetLookAheadToken(1);
1860 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1861 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1862 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001863
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001864 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001865 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001866 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001867 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001868 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001869 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001870 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001871 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001872 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001873 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001874 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001875 ParseDeclarator(ParmDecl);
1876
Douglas Gregore11ee112010-04-23 23:01:43 +00001877 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001878 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001879 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001880 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001881 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001882
Steve Naroff65a00892009-04-07 22:56:58 +00001883 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001884
Steve Naroff65a00892009-04-07 22:56:58 +00001885 if (Tok.is(tok::r_paren))
1886 RParenLoc = ConsumeParen();
1887 else // Skip over garbage, until we get to ')'. Eat the ')'.
1888 SkipUntil(tok::r_paren, true, false);
1889
John McCalldadc5752010-08-24 06:29:42 +00001890 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001891 if (Tok.is(tok::l_brace))
1892 CatchBody = ParseCompoundStatementBody();
1893 else
1894 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001895 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001896 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001897
John McCalldadc5752010-08-24 06:29:42 +00001898 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001899 RParenLoc,
1900 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001901 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001902 if (!Catch.isInvalid())
1903 CatchStmts.push_back(Catch.release());
1904
Steve Naroffe6016792008-02-05 21:27:35 +00001905 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001906 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1907 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001908 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001909 }
1910 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001911 } else {
1912 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001913 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001914 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001915
John McCalldadc5752010-08-24 06:29:42 +00001916 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001917 if (Tok.is(tok::l_brace))
1918 FinallyBody = ParseCompoundStatementBody();
1919 else
1920 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001921 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001922 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001923 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001924 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001925 catch_or_finally_seen = true;
1926 break;
1927 }
1928 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001929 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001930 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001931 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001932 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001933
John McCallb268a282010-08-23 23:25:46 +00001934 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001935 CatchStmts,
John McCallb268a282010-08-23 23:25:46 +00001936 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001937}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001938
John McCall31168b02011-06-15 23:02:42 +00001939/// objc-autoreleasepool-statement:
1940/// @autoreleasepool compound-statement
1941///
1942StmtResult
1943Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1944 ConsumeToken(); // consume autoreleasepool
1945 if (Tok.isNot(tok::l_brace)) {
1946 Diag(Tok, diag::err_expected_lbrace);
1947 return StmtError();
1948 }
1949 // Enter a scope to hold everything within the compound stmt. Compound
1950 // statements can always hold declarations.
1951 ParseScope BodyScope(this, Scope::DeclScope);
1952
1953 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1954
1955 BodyScope.Exit();
1956 if (AutoreleasePoolBody.isInvalid())
1957 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1958 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1959 AutoreleasePoolBody.take());
1960}
1961
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001962/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1963/// for later parsing.
1964void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1965 LexedMethod* LM = new LexedMethod(this, MDecl);
1966 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1967 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001968 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001969 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001970 if (Tok.is(tok::kw_try)) {
1971 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00001972 if (Tok.is(tok::colon)) {
1973 Toks.push_back(Tok);
1974 ConsumeToken();
1975 while (Tok.isNot(tok::l_brace)) {
1976 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1977 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1978 }
1979 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001980 Toks.push_back(Tok); // also store '{'
1981 }
1982 else if (Tok.is(tok::colon)) {
1983 ConsumeToken();
1984 while (Tok.isNot(tok::l_brace)) {
1985 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1986 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1987 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001988 Toks.push_back(Tok); // also store '{'
1989 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001990 ConsumeBrace();
1991 // Consume everything up to (and including) the matching right brace.
1992 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001993 while (Tok.is(tok::kw_catch)) {
1994 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1995 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1996 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001997}
1998
Steve Naroff09bf8152007-09-06 21:24:23 +00001999/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002000///
John McCall48871652010-08-21 09:40:31 +00002001Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002002 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002003
John McCallfaf5fb42010-08-26 23:41:50 +00002004 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2005 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002006
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002007 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002008 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002009 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002010 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002011 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002012 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002013 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002014 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002015
Steve Naroffbb875722007-11-11 19:54:21 +00002016 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002017 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002018 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002019
Steve Naroffbb875722007-11-11 19:54:21 +00002020 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2021 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00002022
Steve Naroffbb875722007-11-11 19:54:21 +00002023 // If we didn't find the '{', bail out.
2024 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00002025 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002026 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002027
2028 if (!MDecl) {
2029 ConsumeBrace();
2030 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2031 return 0;
2032 }
2033
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002034 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002035 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002036 assert (CurParsedObjCImpl
2037 && "ParseObjCMethodDefinition - Method out of @implementation");
2038 // Consume the tokens and store them for later parsing.
2039 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002040 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002041}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002042
John McCalldadc5752010-08-24 06:29:42 +00002043StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002044 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002045 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002046 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002047 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002048 }
2049
2050 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002051 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002052
2053 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002054 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002055
2056 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002057 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002058
2059 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2060 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002061
John McCalldadc5752010-08-24 06:29:42 +00002062 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002063 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002064 // If the expression is invalid, skip ahead to the next semicolon. Not
2065 // doing this opens us up to the possibility of infinite loops if
2066 // ParseExpression does not consume any tokens.
2067 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002068 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002069 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002070
Steve Naroffe6016792008-02-05 21:27:35 +00002071 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002072 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002073 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002074}
2075
John McCalldadc5752010-08-24 06:29:42 +00002076ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002077 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002078 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002079 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002080 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002081 return ExprError();
2082
Ted Kremeneke65b0862012-03-06 20:05:56 +00002083 case tok::minus:
2084 case tok::plus: {
2085 tok::TokenKind Kind = Tok.getKind();
2086 SourceLocation OpLoc = ConsumeToken();
2087
2088 if (!Tok.is(tok::numeric_constant)) {
2089 const char *Symbol = 0;
2090 switch (Kind) {
2091 case tok::minus: Symbol = "-"; break;
2092 case tok::plus: Symbol = "+"; break;
2093 default: llvm_unreachable("missing unary operator case");
2094 }
2095 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2096 << Symbol;
2097 return ExprError();
2098 }
2099
2100 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2101 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002102 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002103 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002104 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002105
2106 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2107 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002108 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002109
2110 return ParsePostfixExpressionSuffix(
2111 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2112 }
2113
Chris Lattnere002fbe2007-12-12 01:04:12 +00002114 case tok::string_literal: // primary-expression: string-literal
2115 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002116 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002117
2118 case tok::char_constant:
2119 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2120
2121 case tok::numeric_constant:
2122 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2123
2124 case tok::kw_true: // Objective-C++, etc.
2125 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2126 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2127 case tok::kw_false: // Objective-C++, etc.
2128 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2129 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2130
2131 case tok::l_square:
2132 // Objective-C array literal
2133 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2134
2135 case tok::l_brace:
2136 // Objective-C dictionary literal
2137 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2138
Patrick Beard0caa3942012-04-19 00:25:12 +00002139 case tok::l_paren:
2140 // Objective-C boxed expression
2141 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2142
Chris Lattnere002fbe2007-12-12 01:04:12 +00002143 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002144 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002145 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002146
Chris Lattner197a3012008-08-05 06:19:09 +00002147 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2148 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002149 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002150 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002151 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002152 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002153 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002154 default: {
2155 const char *str = 0;
2156 if (GetLookAheadToken(1).is(tok::l_brace)) {
2157 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2158 str =
2159 ch == 't' ? "try"
2160 : (ch == 'f' ? "finally"
2161 : (ch == 'a' ? "autoreleasepool" : 0));
2162 }
2163 if (str) {
2164 SourceLocation kwLoc = Tok.getLocation();
2165 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2166 FixItHint::CreateReplacement(kwLoc, str));
2167 }
2168 else
2169 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2170 }
Chris Lattner197a3012008-08-05 06:19:09 +00002171 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002172 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002173}
2174
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002175/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002176///
2177/// This routine parses the receiver of a message send in
2178/// Objective-C++ either as a type or as an expression. Note that this
2179/// routine must not be called to parse a send to 'super', since it
2180/// has no way to return such a result.
2181///
2182/// \param IsExpr Whether the receiver was parsed as an expression.
2183///
2184/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2185/// IsExpr is true), the parsed expression. If the receiver was parsed
2186/// as a type (\c IsExpr is false), the parsed type.
2187///
2188/// \returns True if an error occurred during parsing or semantic
2189/// analysis, in which case the arguments do not have valid
2190/// values. Otherwise, returns false for a successful parse.
2191///
2192/// objc-receiver: [C++]
2193/// 'super' [not parsed here]
2194/// expression
2195/// simple-type-specifier
2196/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002197bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002198 InMessageExpressionRAIIObject InMessage(*this, true);
2199
Douglas Gregor8d4de672010-04-21 22:36:40 +00002200 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2201 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2202 TryAnnotateTypeOrScopeToken();
2203
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002204 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002205 // objc-receiver:
2206 // expression
John McCalldadc5752010-08-24 06:29:42 +00002207 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002208 if (Receiver.isInvalid())
2209 return true;
2210
2211 IsExpr = true;
2212 TypeOrExpr = Receiver.take();
2213 return false;
2214 }
2215
2216 // objc-receiver:
2217 // typename-specifier
2218 // simple-type-specifier
2219 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002220 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002221 ParseCXXSimpleTypeSpecifier(DS);
2222
2223 if (Tok.is(tok::l_paren)) {
2224 // If we see an opening parentheses at this point, we are
2225 // actually parsing an expression that starts with a
2226 // function-style cast, e.g.,
2227 //
2228 // postfix-expression:
2229 // simple-type-specifier ( expression-list [opt] )
2230 // typename-specifier ( expression-list [opt] )
2231 //
2232 // Parse the remainder of this case, then the (optional)
2233 // postfix-expression suffix, followed by the (optional)
2234 // right-hand side of the binary expression. We have an
2235 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002236 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002237 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002238 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002239 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002240 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002241 if (Receiver.isInvalid())
2242 return true;
2243
2244 IsExpr = true;
2245 TypeOrExpr = Receiver.take();
2246 return false;
2247 }
2248
2249 // We have a class message. Turn the simple-type-specifier or
2250 // typename-specifier we parsed into a type and parse the
2251 // remainder of the class message.
2252 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002253 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002254 if (Type.isInvalid())
2255 return true;
2256
2257 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002258 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002259 return false;
2260}
2261
Douglas Gregor990ccac2010-05-31 14:40:22 +00002262/// \brief Determine whether the parser is currently referring to a an
2263/// Objective-C message send, using a simplified heuristic to avoid overhead.
2264///
2265/// This routine will only return true for a subset of valid message-send
2266/// expressions.
2267bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002268 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002269 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002270 return GetLookAheadToken(1).is(tok::identifier) &&
2271 GetLookAheadToken(2).is(tok::identifier);
2272}
2273
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002274bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002275 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002276 InMessageExpression)
2277 return false;
2278
2279
2280 ParsedType Type;
2281
2282 if (Tok.is(tok::annot_typename))
2283 Type = getTypeAnnotation(Tok);
2284 else if (Tok.is(tok::identifier))
2285 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2286 getCurScope());
2287 else
2288 return false;
2289
2290 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2291 const Token &AfterNext = GetLookAheadToken(2);
2292 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2293 if (Tok.is(tok::identifier))
2294 TryAnnotateTypeOrScopeToken();
2295
2296 return Tok.is(tok::annot_typename);
2297 }
2298 }
2299
2300 return false;
2301}
2302
Mike Stump11289f42009-09-09 15:08:12 +00002303/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002304/// '[' objc-receiver objc-message-args ']'
2305///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002306/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002307/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002308/// expression
2309/// class-name
2310/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002311///
John McCalldadc5752010-08-24 06:29:42 +00002312ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002313 assert(Tok.is(tok::l_square) && "'[' expected");
2314 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2315
Douglas Gregora817a192010-05-27 23:06:34 +00002316 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002317 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002318 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002319 return ExprError();
2320 }
2321
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002322 InMessageExpressionRAIIObject InMessage(*this, true);
2323
David Blaikiebbafb8a2012-03-11 07:00:24 +00002324 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002325 // We completely separate the C and C++ cases because C++ requires
2326 // more complicated (read: slower) parsing.
2327
2328 // Handle send to super.
2329 // FIXME: This doesn't benefit from the same typo-correction we
2330 // get in Objective-C.
2331 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002332 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002333 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2334 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002335
2336 // Parse the receiver, which is either a type or an expression.
2337 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002338 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002339 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2340 SkipUntil(tok::r_square);
2341 return ExprError();
2342 }
2343
2344 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002345 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2346 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002347 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002348
2349 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002350 ParsedType::getFromOpaquePtr(TypeOrExpr),
2351 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002352 }
2353
2354 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002355 IdentifierInfo *Name = Tok.getIdentifierInfo();
2356 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002357 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002358 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002359 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002360 NextToken().is(tok::period),
2361 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002362 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002363 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2364 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002365
John McCallfaf5fb42010-08-26 23:41:50 +00002366 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002367 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002368 SkipUntil(tok::r_square);
2369 return ExprError();
2370 }
2371
Douglas Gregore5798dc2010-04-21 20:38:13 +00002372 ConsumeToken(); // the type name
2373
2374 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002375 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002376
John McCallfaf5fb42010-08-26 23:41:50 +00002377 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002378 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002379 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002380 }
Chris Lattner8f697062008-01-25 18:59:06 +00002381 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002382
2383 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002384 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002385 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002386 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002387 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002388 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002389
John McCallba7bf592010-08-24 05:47:05 +00002390 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2391 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002392}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002393
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002394/// \brief Parse the remainder of an Objective-C message following the
2395/// '[' objc-receiver.
2396///
2397/// This routine handles sends to super, class messages (sent to a
2398/// class name), and instance messages (sent to an object), and the
2399/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2400/// ReceiverExpr, respectively. Only one of these parameters may have
2401/// a valid value.
2402///
2403/// \param LBracLoc The location of the opening '['.
2404///
2405/// \param SuperLoc If this is a send to 'super', the location of the
2406/// 'super' keyword that indicates a send to the superclass.
2407///
2408/// \param ReceiverType If this is a class message, the type of the
2409/// class we are sending a message to.
2410///
2411/// \param ReceiverExpr If this is an instance message, the expression
2412/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002413///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002414/// objc-message-args:
2415/// objc-selector
2416/// objc-keywordarg-list
2417///
2418/// objc-keywordarg-list:
2419/// objc-keywordarg
2420/// objc-keywordarg-list objc-keywordarg
2421///
Mike Stump11289f42009-09-09 15:08:12 +00002422/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002423/// selector-name[opt] ':' objc-keywordexpr
2424///
2425/// objc-keywordexpr:
2426/// nonempty-expr-list
2427///
2428/// nonempty-expr-list:
2429/// assignment-expression
2430/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002431///
John McCalldadc5752010-08-24 06:29:42 +00002432ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002433Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002434 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002435 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002436 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002437 InMessageExpressionRAIIObject InMessage(*this, true);
2438
Steve Naroffeae65032009-11-07 02:08:14 +00002439 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002440 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002441 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2442 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002443 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002444 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2445 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002446 else
John McCallb268a282010-08-23 23:25:46 +00002447 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002448 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002449 cutOffParsing();
2450 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002451 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002452
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002453 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002454 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002455 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002456
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002457 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002458 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002459 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002460
Chris Lattner0ef13522007-10-09 17:51:17 +00002461 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002462 while (1) {
2463 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002464 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002465 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002466
Chris Lattner0ef13522007-10-09 17:51:17 +00002467 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002468 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002469 // We must manually skip to a ']', otherwise the expression skipper will
2470 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2471 // the enclosing expression.
2472 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002473 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002474 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002475
Steve Narofff73590d2007-09-27 14:38:14 +00002476 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002477 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002478
2479 if (Tok.is(tok::code_completion)) {
2480 if (SuperLoc.isValid())
2481 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2482 KeyIdents.data(),
2483 KeyIdents.size(),
2484 /*AtArgumentEpression=*/true);
2485 else if (ReceiverType)
2486 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2487 KeyIdents.data(),
2488 KeyIdents.size(),
2489 /*AtArgumentEpression=*/true);
2490 else
2491 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2492 KeyIdents.data(),
2493 KeyIdents.size(),
2494 /*AtArgumentEpression=*/true);
2495
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002496 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002497 return ExprError();
2498 }
2499
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002500 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002501 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002502 // We must manually skip to a ']', otherwise the expression skipper will
2503 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2504 // the enclosing expression.
2505 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002506 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002507 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002508
Steve Naroff486760a2007-09-17 20:25:27 +00002509 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002510 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002511
Douglas Gregor1b605f72009-11-19 01:08:35 +00002512 // Code completion after each argument.
2513 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002514 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002515 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002516 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002517 KeyIdents.size(),
2518 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002519 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002520 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002521 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002522 KeyIdents.size(),
2523 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002524 else
John McCallb268a282010-08-23 23:25:46 +00002525 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002526 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002527 KeyIdents.size(),
2528 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002529 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002530 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002531 }
2532
Steve Naroff486760a2007-09-17 20:25:27 +00002533 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002534 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002535 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002536 break;
2537 // We have a selector or a colon, continue parsing.
2538 }
2539 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002540 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002541 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002542 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002543 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002544 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002545 if (Tok.is(tok::colon)) {
2546 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2547 FixItHint::CreateRemoval(commaLoc);
2548 }
Chris Lattner197a3012008-08-05 06:19:09 +00002549 // We must manually skip to a ']', otherwise the expression skipper will
2550 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2551 // the enclosing expression.
2552 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002553 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002554 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002555
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002556 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002557 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002558 }
2559 } else if (!selIdent) {
2560 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002561
Chris Lattner197a3012008-08-05 06:19:09 +00002562 // We must manually skip to a ']', otherwise the expression skipper will
2563 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2564 // the enclosing expression.
2565 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002566 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002567 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002568
Chris Lattner0ef13522007-10-09 17:51:17 +00002569 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002570 if (Tok.is(tok::identifier))
2571 Diag(Tok, diag::err_expected_colon);
2572 else
2573 Diag(Tok, diag::err_expected_rsquare);
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 Jahanian02447d82013-01-22 18:35:43 +00002580
Chris Lattner8f697062008-01-25 18:59:06 +00002581 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002582
Steve Naroffe61bfa82007-10-05 18:42:47 +00002583 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002584 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002585 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002586 KeyLocs.push_back(Loc);
2587 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002588 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002589
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002590 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002591 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002592 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002593 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002594 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002595 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002596 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002597 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002598}
2599
John McCalldadc5752010-08-24 06:29:42 +00002600ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2601 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002602 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002603
Chris Lattnere002fbe2007-12-12 01:04:12 +00002604 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2605 // expressions. At this point, we know that the only valid thing that starts
2606 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002607 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002608 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002609 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002610 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002611
Chris Lattnere002fbe2007-12-12 01:04:12 +00002612 while (Tok.is(tok::at)) {
2613 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002614
Sebastian Redlc13f2682008-12-09 20:22:58 +00002615 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002616 if (!isTokenStringLiteral())
2617 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002618
John McCalldadc5752010-08-24 06:29:42 +00002619 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002620 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002621 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002622
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002623 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002624 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002625
Nico Webera7c7e602012-12-31 00:28:03 +00002626 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2627 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002628}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002629
Ted Kremeneke65b0862012-03-06 20:05:56 +00002630/// ParseObjCBooleanLiteral -
2631/// objc-scalar-literal : '@' boolean-keyword
2632/// ;
2633/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2634/// ;
2635ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2636 bool ArgValue) {
2637 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2638 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2639}
2640
2641/// ParseObjCCharacterLiteral -
2642/// objc-scalar-literal : '@' character-literal
2643/// ;
2644ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2645 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2646 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002647 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002648 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002649 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002650 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002651}
2652
2653/// ParseObjCNumericLiteral -
2654/// objc-scalar-literal : '@' scalar-literal
2655/// ;
2656/// scalar-literal : | numeric-constant /* any numeric constant. */
2657/// ;
2658ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2659 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2660 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002661 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002662 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002663 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002664 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002665}
2666
Patrick Beard0caa3942012-04-19 00:25:12 +00002667/// ParseObjCBoxedExpr -
2668/// objc-box-expression:
2669/// @( assignment-expression )
2670ExprResult
2671Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2672 if (Tok.isNot(tok::l_paren))
2673 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2674
2675 BalancedDelimiterTracker T(*this, tok::l_paren);
2676 T.consumeOpen();
2677 ExprResult ValueExpr(ParseAssignmentExpression());
2678 if (T.consumeClose())
2679 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002680
2681 if (ValueExpr.isInvalid())
2682 return ExprError();
2683
Patrick Beard0caa3942012-04-19 00:25:12 +00002684 // Wrap the sub-expression in a parenthesized expression, to distinguish
2685 // a boxed expression from a literal.
2686 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2687 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Webera7c7e602012-12-31 00:28:03 +00002688 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2689 ValueExpr.take());
Patrick Beard0caa3942012-04-19 00:25:12 +00002690}
2691
Ted Kremeneke65b0862012-03-06 20:05:56 +00002692ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002693 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002694 ConsumeBracket(); // consume the l_square.
2695
2696 while (Tok.isNot(tok::r_square)) {
2697 // Parse list of array element expressions (all must be id types).
2698 ExprResult Res(ParseAssignmentExpression());
2699 if (Res.isInvalid()) {
2700 // We must manually skip to a ']', otherwise the expression skipper will
2701 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2702 // the enclosing expression.
2703 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002704 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002705 }
2706
2707 // Parse the ellipsis that indicates a pack expansion.
2708 if (Tok.is(tok::ellipsis))
2709 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2710 if (Res.isInvalid())
2711 return true;
2712
2713 ElementExprs.push_back(Res.release());
2714
2715 if (Tok.is(tok::comma))
2716 ConsumeToken(); // Eat the ','.
2717 else if (Tok.isNot(tok::r_square))
2718 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2719 }
2720 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002721 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002722 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002723}
2724
2725ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2726 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2727 ConsumeBrace(); // consume the l_square.
2728 while (Tok.isNot(tok::r_brace)) {
2729 // Parse the comma separated key : value expressions.
2730 ExprResult KeyExpr;
2731 {
2732 ColonProtectionRAIIObject X(*this);
2733 KeyExpr = ParseAssignmentExpression();
2734 if (KeyExpr.isInvalid()) {
2735 // We must manually skip to a '}', otherwise the expression skipper will
2736 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2737 // the enclosing expression.
2738 SkipUntil(tok::r_brace);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002739 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002740 }
2741 }
2742
2743 if (Tok.is(tok::colon)) {
2744 ConsumeToken();
2745 } else {
2746 return ExprError(Diag(Tok, diag::err_expected_colon));
2747 }
2748
2749 ExprResult ValueExpr(ParseAssignmentExpression());
2750 if (ValueExpr.isInvalid()) {
2751 // We must manually skip to a '}', otherwise the expression skipper will
2752 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2753 // the enclosing expression.
2754 SkipUntil(tok::r_brace);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002755 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002756 }
2757
2758 // Parse the ellipsis that designates this as a pack expansion.
2759 SourceLocation EllipsisLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002760 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremeneke65b0862012-03-06 20:05:56 +00002761 EllipsisLoc = ConsumeToken();
2762
2763 // We have a valid expression. Collect it in a vector so we can
2764 // build the argument list.
2765 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002766 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002767 };
2768 Elements.push_back(Element);
2769
2770 if (Tok.is(tok::comma))
2771 ConsumeToken(); // Eat the ','.
2772 else if (Tok.isNot(tok::r_brace))
2773 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2774 }
2775 SourceLocation EndLoc = ConsumeBrace();
2776
2777 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002778 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2779 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002780}
2781
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002782/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002783/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002784ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002785Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002786 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002787
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002788 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002789
Chris Lattner197a3012008-08-05 06:19:09 +00002790 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002791 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2792
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002793 BalancedDelimiterTracker T(*this, tok::l_paren);
2794 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002795
Douglas Gregor220cac52009-02-18 17:45:20 +00002796 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002797
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002798 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002799
Douglas Gregor220cac52009-02-18 17:45:20 +00002800 if (Ty.isInvalid())
2801 return ExprError();
2802
Nico Webera7c7e602012-12-31 00:28:03 +00002803 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2804 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002805}
Anders Carlssone01493d2007-08-23 15:25:28 +00002806
2807/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002808/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002809ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002810Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002811 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002812
Chris Lattner197a3012008-08-05 06:19:09 +00002813 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002814 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2815
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002816 BalancedDelimiterTracker T(*this, tok::l_paren);
2817 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002818
Chris Lattner197a3012008-08-05 06:19:09 +00002819 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002820 return ExprError(Diag(Tok, diag::err_expected_ident));
2821
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002822 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002823 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002824
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002825 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002826
Nico Webera7c7e602012-12-31 00:28:03 +00002827 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2828 T.getOpenLocation(), ProtoIdLoc,
2829 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002830}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002831
2832/// objc-selector-expression
2833/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002834ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002835 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002836
Chris Lattner197a3012008-08-05 06:19:09 +00002837 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002838 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2839
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002840 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002841 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002842
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002843 BalancedDelimiterTracker T(*this, tok::l_paren);
2844 T.consumeOpen();
2845
Douglas Gregor67c692c2010-08-26 15:07:07 +00002846 if (Tok.is(tok::code_completion)) {
2847 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2848 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002849 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002850 return ExprError();
2851 }
2852
Chris Lattner4f472a32009-04-11 18:13:45 +00002853 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002854 if (!SelIdent && // missing selector name.
2855 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002856 return ExprError(Diag(Tok, diag::err_expected_ident));
2857
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002858 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002859 unsigned nColons = 0;
2860 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002861 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002862 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2863 ++nColons;
2864 KeyIdents.push_back(0);
2865 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002866 return ExprError(Diag(Tok, diag::err_expected_colon));
2867
Chris Lattner1ba64452010-08-27 22:32:41 +00002868 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002869 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002870 if (Tok.is(tok::r_paren))
2871 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002872
2873 if (Tok.is(tok::code_completion)) {
2874 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2875 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002876 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002877 return ExprError();
2878 }
2879
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002880 // Check for another keyword selector.
2881 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002882 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002883 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002884 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002885 break;
2886 }
Steve Naroff152dd812007-12-05 22:21:29 +00002887 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002888 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002889 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00002890 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2891 T.getOpenLocation(),
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002892 T.getCloseLocation());
Gabor Greif24032f12007-10-19 15:38:32 +00002893 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002894
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002895void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2896 // MCDecl might be null due to error in method or c-function prototype, etc.
2897 Decl *MCDecl = LM.D;
2898 bool skip = MCDecl &&
2899 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2900 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2901 if (skip)
2902 return;
2903
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002904 // Save the current token position.
2905 SourceLocation OrigLoc = Tok.getLocation();
2906
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002907 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2908 // Append the current token at the end of the new token stream so that it
2909 // doesn't get lost.
2910 LM.Toks.push_back(Tok);
2911 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2912
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002913 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00002914 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002915
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002916 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2917 Tok.is(tok::colon)) &&
2918 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002919 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002920 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002921 parseMethod
2922 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2923 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002924
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002925 // Tell the actions module that we have entered a method or c-function definition
2926 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00002927 if (parseMethod)
2928 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2929 else
2930 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002931 if (Tok.is(tok::kw_try))
2932 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002933 else {
2934 if (Tok.is(tok::colon))
2935 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002936 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002937 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00002938
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002939 if (Tok.getLocation() != OrigLoc) {
2940 // Due to parsing error, we either went over the cached tokens or
2941 // there are still cached tokens left. If it's the latter case skip the
2942 // leftover tokens.
2943 // Since this is an uncommon situation that should be avoided, use the
2944 // expensive isBeforeInTranslationUnit call.
2945 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2946 OrigLoc))
2947 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2948 ConsumeAnyToken();
2949 }
2950
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002951 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002952}