blob: 4adba4f5c5539cdd9c8cadc74f4485bb10183096 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallVector.h"
Fariborz Jahanian08602352012-09-17 19:15:26 +000022#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Nico Weber9f4f5f12013-04-03 17:36:11 +000025/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Webere145bfd2013-04-04 00:15:10 +000026void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber9f4f5f12013-04-03 17:36:11 +000027 ParsedAttributes attrs(AttrFactory);
28 if (Tok.is(tok::kw___attribute)) {
Nico Webere145bfd2013-04-04 00:15:10 +000029 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
30 Diag(Tok, diag::err_objc_postfix_attribute_hint)
31 << (Kind == tok::objc_protocol);
32 else
33 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber9f4f5f12013-04-03 17:36:11 +000034 ParseGNUAttributes(attrs);
35 }
36}
Reid Spencer5f016e22007-07-11 17:01:13 +000037
Chris Lattner891dca62008-12-08 21:53:24 +000038/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000039/// external-declaration: [C99 6.9]
40/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000041/// [OBJC] objc-class-declaration
42/// [OBJC] objc-alias-declaration
43/// [OBJC] objc-protocol-definition
44/// [OBJC] objc-method-definition
45/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000046Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000047 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregorc464ae82009-12-07 09:27:33 +000049 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000050 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000051 cutOffParsing();
52 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000053 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000054
55 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000056 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000057 case tok::objc_class:
58 return ParseObjCAtClassDeclaration(AtLoc);
John McCall7f040a92010-12-24 02:08:15 +000059 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000060 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
62 break;
John McCall7f040a92010-12-24 02:08:15 +000063 }
64 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000065 ParsedAttributes attrs(AttrFactory);
Douglas Gregorbd9482d2012-01-01 21:23:57 +000066 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall7f040a92010-12-24 02:08:15 +000067 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000068 case tok::objc_implementation:
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +000069 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000070 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000071 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000072 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000073 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
74 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000075 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000076 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
77 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000078 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000079 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
80 break;
Douglas Gregor1b257af2012-12-11 22:11:52 +000081 case tok::objc_import:
David Blaikie4e4d0842012-03-11 07:00:24 +000082 if (getLangOpts().Modules)
Douglas Gregor94ad28b2012-01-03 18:24:14 +000083 return ParseModuleImport(AtLoc);
84
85 // Fall through
86
Chris Lattner5ffb14b2008-08-23 02:02:23 +000087 default:
88 Diag(AtLoc, diag::err_unexpected_at);
89 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000090 SingleDecl = 0;
91 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000092 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000093 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
96///
Mike Stump1eb44332009-09-09 15:08:12 +000097/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000099///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000100Parser::DeclGroupPtrTy
101Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 SmallVector<IdentifierInfo *, 8> ClassNames;
104 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +0000105
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 while (1) {
Nico Webere145bfd2013-04-04 00:15:10 +0000108 MaybeSkipAttributes(tok::objc_class);
Chris Lattnerdf195262007-10-09 17:51:17 +0000109 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 Diag(Tok, diag::err_expected_ident);
111 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000112 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +0000115 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattnerdf195262007-10-09 17:51:17 +0000118 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 ConsumeToken();
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // Consume the ';'.
125 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000126 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenekc09cba62009-11-17 23:12:20 +0000128 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
129 ClassLocs.data(),
130 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000131}
132
Erik Verbruggend64251f2011-12-06 09:25:23 +0000133void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
134{
135 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
136 if (ock == Sema::OCK_None)
137 return;
138
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000139 Decl *Decl = Actions.getObjCDeclContext();
140 if (CurParsedObjCImpl) {
141 CurParsedObjCImpl->finish(AtLoc);
142 } else {
143 Actions.ActOnAtEnd(getCurScope(), AtLoc);
144 }
Erik Verbruggend64251f2011-12-06 09:25:23 +0000145 Diag(AtLoc, diag::err_objc_missing_end)
146 << FixItHint::CreateInsertion(AtLoc, "@end\n");
147 if (Decl)
148 Diag(Decl->getLocStart(), diag::note_objc_container_start)
149 << (int) ock;
Erik Verbruggend64251f2011-12-06 09:25:23 +0000150}
151
Steve Naroffdac269b2007-08-20 21:31:48 +0000152///
153/// objc-interface:
154/// objc-class-interface-attributes[opt] objc-class-interface
155/// objc-category-interface
156///
157/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000158/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000159/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000160/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000161/// objc-interface-decl-list
162/// @end
163///
164/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000165/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000166/// objc-protocol-refs[opt]
167/// objc-interface-decl-list
168/// @end
169///
170/// objc-superclass:
171/// ':' identifier
172///
173/// objc-class-interface-attributes:
174/// __attribute__((visibility("default")))
175/// __attribute__((visibility("hidden")))
176/// __attribute__((deprecated))
177/// __attribute__((unavailable))
178/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardb2f68202012-04-06 18:12:22 +0000179/// __attribute__((objc_root_class))
Steve Naroffdac269b2007-08-20 21:31:48 +0000180///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000181Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000182 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000183 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000185 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000186 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000188 // Code completion after '@interface'.
189 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000190 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000191 cutOffParsing();
192 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000193 }
194
Nico Webere145bfd2013-04-04 00:15:10 +0000195 MaybeSkipAttributes(tok::objc_interface);
Nico Weber9f4f5f12013-04-03 17:36:11 +0000196
Chris Lattnerdf195262007-10-09 17:51:17 +0000197 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000198 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000199 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000200 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000201
Steve Naroffdac269b2007-08-20 21:31:48 +0000202 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000203 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000205 if (Tok.is(tok::l_paren) &&
206 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000207
208 BalancedDelimiterTracker T(*this, tok::l_paren);
209 T.consumeOpen();
210
211 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000212 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000213 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000214 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000215 cutOffParsing();
216 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000217 }
218
Steve Naroff527fe232007-08-23 19:56:30 +0000219 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000220 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000221 categoryId = Tok.getIdentifierInfo();
222 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000223 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000224 else if (!getLangOpts().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000225 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000226 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000227 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000228
229 T.consumeClose();
230 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000231 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000232
233 if (!attrs.empty()) { // categories don't support attributes.
234 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
235 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000236 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000237
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000238 // Next, we need to check for any protocol references.
239 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000240 SmallVector<Decl *, 8> ProtocolRefs;
241 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000242 if (Tok.is(tok::less) &&
243 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000244 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000245 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
John McCalld226f652010-08-21 09:40:31 +0000247 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000248 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000249 nameId, nameLoc,
250 categoryId, categoryLoc,
251 ProtocolRefs.data(),
252 ProtocolRefs.size(),
253 ProtocolLocs.data(),
254 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000255
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000256 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000257 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000258
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000259 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000260 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000261 }
262 // Parse a class interface.
263 IdentifierInfo *superClassId = 0;
264 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000265
Chris Lattnerdf195262007-10-09 17:51:17 +0000266 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000267 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000268
269 // Code completion of superclass names.
270 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000271 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000272 cutOffParsing();
273 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000274 }
275
Chris Lattnerdf195262007-10-09 17:51:17 +0000276 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000277 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000278 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000279 }
280 superClassId = Tok.getIdentifierInfo();
281 superClassLoc = ConsumeToken();
282 }
283 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284 SmallVector<Decl *, 8> ProtocolRefs;
285 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000286 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000287 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000288 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
289 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000290 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
John McCalld226f652010-08-21 09:40:31 +0000292 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000293 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000294 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000295 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000296 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000297 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattnerdf195262007-10-09 17:51:17 +0000299 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000300 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000301
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000302 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000303 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000304}
305
John McCalld0014542009-12-03 22:31:13 +0000306/// The Objective-C property callback. This should be defined where
307/// it's used, but instead it's been lifted to here to support VS2005.
308struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000309private:
310 virtual void anchor();
311public:
John McCalld0014542009-12-03 22:31:13 +0000312 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000313 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000314 ObjCDeclSpec &OCDS;
315 SourceLocation AtLoc;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000316 SourceLocation LParenLoc;
John McCalld0014542009-12-03 22:31:13 +0000317 tok::ObjCKeywordKind MethodImplKind;
318
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000319 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000320 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000321 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000322 SourceLocation LParenLoc,
John McCalld0014542009-12-03 22:31:13 +0000323 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000324 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCalld0014542009-12-03 22:31:13 +0000325 MethodImplKind(MethodImplKind) {
326 }
327
Eli Friedmandcdff462012-08-08 23:53:27 +0000328 void invoke(ParsingFieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000329 if (FD.D.getIdentifier() == 0) {
330 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
331 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000332 return;
John McCalld0014542009-12-03 22:31:13 +0000333 }
334 if (FD.BitfieldSize) {
335 P.Diag(AtLoc, diag::err_objc_property_bitfield)
336 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000337 return;
John McCalld0014542009-12-03 22:31:13 +0000338 }
339
340 // Install the property declarator into interfaceDecl.
341 IdentifierInfo *SelName =
342 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
343
344 Selector GetterSel =
345 P.PP.getSelectorTable().getNullarySelector(SelName);
346 IdentifierInfo *SetterName = OCDS.getSetterName();
347 Selector SetterSel;
348 if (SetterName)
349 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
350 else
351 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
352 P.PP.getSelectorTable(),
353 FD.D.getIdentifier());
354 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000355 Decl *Property =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000356 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
357 FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000358 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000359 &isOverridingProperty,
360 MethodImplKind);
361 if (!isOverridingProperty)
362 Props.push_back(Property);
363
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000364 FD.complete(Property);
John McCalld0014542009-12-03 22:31:13 +0000365 }
366};
367
David Blaikie99ba9e32011-12-20 02:48:34 +0000368void Parser::ObjCPropertyCallback::anchor() {
369}
370
Steve Naroffdac269b2007-08-20 21:31:48 +0000371/// objc-interface-decl-list:
372/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000373/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000374/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000375/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000376/// objc-interface-decl-list declaration
377/// objc-interface-decl-list ';'
378///
Steve Naroff294494e2007-08-22 16:35:03 +0000379/// objc-method-requirement: [OBJC2]
380/// @required
381/// @optional
382///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000383void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
384 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000385 SmallVector<Decl *, 32> allMethods;
386 SmallVector<Decl *, 16> allProperties;
387 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000388 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek782f2f52010-01-07 01:20:12 +0000390 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000391
Steve Naroff294494e2007-08-22 16:35:03 +0000392 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000393 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000394 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniand30ec702012-07-26 17:32:28 +0000395 if (Decl *methodPrototype =
396 ParseObjCMethodPrototype(MethodImplKind, false))
397 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000398 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
399 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000400 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
401 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
402 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
403 if (Tok.is(tok::semi))
404 ConsumeToken();
405 }
Steve Naroff294494e2007-08-22 16:35:03 +0000406 continue;
407 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000408 if (Tok.is(tok::l_paren)) {
409 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000410 ParseObjCMethodDecl(Tok.getLocation(),
411 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000412 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000413 continue;
414 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000415 // Ignore excess semicolons.
416 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000417 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000418 continue;
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattnerbc662af2008-10-20 06:10:06 +0000421 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000422 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000423 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000425 // Code completion within an Objective-C interface.
426 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000427 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000428 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallf312b1e2010-08-26 23:41:50 +0000429 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000430 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000431 }
432
Chris Lattnere82a10f2008-10-20 05:46:22 +0000433 // If we don't have an @ directive, parse it as a function definition.
434 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000435 // The code below does not consume '}'s because it is afraid of eating the
436 // end of a namespace. Because of the way this code is structured, an
437 // erroneous r_brace would cause an infinite loop if not handled here.
438 if (Tok.is(tok::r_brace))
439 break;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000440 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000441 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000442 continue;
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnere82a10f2008-10-20 05:46:22 +0000445 // Otherwise, we have an @ directive, eat the @.
446 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000447 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000448 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000449 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000450 }
451
Chris Lattnera2449b22008-10-20 05:57:40 +0000452 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattnera2449b22008-10-20 05:57:40 +0000454 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000455 AtEnd.setBegin(AtLoc);
456 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000457 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000458 } else if (DirectiveKind == tok::objc_not_keyword) {
459 Diag(Tok, diag::err_objc_unknown_at);
460 SkipUntil(tok::semi);
461 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattnerbc662af2008-10-20 06:10:06 +0000464 // Eat the identifier.
465 ConsumeToken();
466
Chris Lattnera2449b22008-10-20 05:57:40 +0000467 switch (DirectiveKind) {
468 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000469 // FIXME: If someone forgets an @end on a protocol, this loop will
470 // continue to eat up tons of stuff and spew lots of nonsense errors. It
471 // would probably be better to bail out if we saw an @class or @interface
472 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000473 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000474 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000475 SkipUntil(tok::r_brace, tok::at);
476 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000477
478 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000479 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000480 Diag(AtLoc, diag::err_objc_missing_end)
481 << FixItHint::CreateInsertion(AtLoc, "@end\n");
482 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
483 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000484 ConsumeToken();
485 break;
486
Chris Lattnera2449b22008-10-20 05:57:40 +0000487 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000488 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000489 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000490 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000491 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000492 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000493 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000494 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000495 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Chris Lattnera2449b22008-10-20 05:57:40 +0000497 case tok::objc_property:
David Blaikie4e4d0842012-03-11 07:00:24 +0000498 if (!getLangOpts().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000499 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000500
Chris Lattnere82a10f2008-10-20 05:46:22 +0000501 ObjCDeclSpec OCDS;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000502 SourceLocation LParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000503 // Parse property attribute list, if any.
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000504 if (Tok.is(tok::l_paren)) {
505 LParenLoc = Tok.getLocation();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000506 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000509 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000510 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000511
Chris Lattnere82a10f2008-10-20 05:46:22 +0000512 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000513 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +0000514 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
John McCall7da19ea2011-03-26 01:53:26 +0000516 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000517 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000518 }
Steve Naroff294494e2007-08-22 16:35:03 +0000519 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000520
521 // We break out of the big loop in two cases: when we see @end or when we see
522 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000523 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000524 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000525 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000526 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000527 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000528 } else {
529 Diag(Tok, diag::err_objc_missing_end)
530 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
531 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
532 << (int) Actions.getObjCContainerKind();
533 AtEnd.setBegin(Tok.getLocation());
534 AtEnd.setEnd(Tok.getLocation());
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnera2449b22008-10-20 05:57:40 +0000537 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000538 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000539 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000540 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000541 allProperties.data(), allProperties.size(),
542 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000543}
544
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000545/// Parse property attribute declarations.
546///
547/// property-attr-decl: '(' property-attrlist ')'
548/// property-attrlist:
549/// property-attribute
550/// property-attrlist ',' property-attribute
551/// property-attribute:
552/// getter '=' identifier
553/// setter '=' identifier ':'
554/// readonly
555/// readwrite
556/// assign
557/// retain
558/// copy
559/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000560/// atomic
561/// strong
562/// weak
563/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000564///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000565void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000566 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000567 BalancedDelimiterTracker T(*this, tok::l_paren);
568 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000570 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000571 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000572 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000573 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000574 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000575 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000577 // If this is not an identifier at all, bail out early.
578 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000579 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000580 return;
581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner156b0612008-10-20 07:37:22 +0000583 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner92e62b02008-11-20 04:42:34 +0000585 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000586 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000587 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000588 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000589 else if (II->isStr("unsafe_unretained"))
590 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000591 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000592 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000593 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000594 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000595 else if (II->isStr("strong"))
596 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000597 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000598 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000599 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000600 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000601 else if (II->isStr("atomic"))
602 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000603 else if (II->isStr("weak"))
604 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000605 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000606 bool IsSetter = II->getNameStart()[0] == 's';
607
Chris Lattnere00da7c2008-10-20 07:39:53 +0000608 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000609 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
610 diag::err_objc_expected_equal_for_getter;
611
612 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000613 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Douglas Gregor4ad96852009-11-19 07:41:15 +0000615 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000616 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000617 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000618 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000619 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000620 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000621 }
622
Anders Carlsson42499be2010-10-02 17:45:21 +0000623
624 SourceLocation SelLoc;
625 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
626
627 if (!SelIdent) {
628 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
629 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000630 SkipUntil(tok::r_paren);
631 return;
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Anders Carlsson42499be2010-10-02 17:45:21 +0000634 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000635 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000636 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000638 if (ExpectAndConsume(tok::colon,
639 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000640 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000641 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000642 } else {
643 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000644 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000645 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000646 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000647 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000648 SkipUntil(tok::r_paren);
649 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner156b0612008-10-20 07:37:22 +0000652 if (Tok.isNot(tok::comma))
653 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattner156b0612008-10-20 07:37:22 +0000655 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000658 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000659}
660
Steve Naroff3536b442007-09-06 21:24:23 +0000661/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000662/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000663/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000664///
665/// objc-instance-method: '-'
666/// objc-class-method: '+'
667///
Steve Naroff4985ace2007-08-22 18:35:33 +0000668/// objc-method-attributes: [OBJC2]
669/// __attribute__((deprecated))
670///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000671Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000672 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000673 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000674
Mike Stump1eb44332009-09-09 15:08:12 +0000675 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000676 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000677 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000678 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000679 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000680 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000681 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000682}
683
684/// objc-selector:
685/// identifier
686/// one of
687/// enum struct union if else while do for switch case default
688/// break continue return goto asm sizeof typeof __alignof
689/// unsigned long const short volatile signed restrict _Complex
690/// in out inout bycopy byref oneway int char float double void _Bool
691///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000692IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000693
Chris Lattnerff384912007-10-07 02:00:24 +0000694 switch (Tok.getKind()) {
695 default:
696 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000697 case tok::ampamp:
698 case tok::ampequal:
699 case tok::amp:
700 case tok::pipe:
701 case tok::tilde:
702 case tok::exclaim:
703 case tok::exclaimequal:
704 case tok::pipepipe:
705 case tok::pipeequal:
706 case tok::caret:
707 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000708 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000709 if (isLetter(ThisTok[0])) {
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000710 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
711 Tok.setKind(tok::identifier);
712 SelectorLoc = ConsumeToken();
713 return II;
714 }
715 return 0;
716 }
717
Chris Lattnerff384912007-10-07 02:00:24 +0000718 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000719 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000720 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000721 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000722 case tok::kw_break:
723 case tok::kw_case:
724 case tok::kw_catch:
725 case tok::kw_char:
726 case tok::kw_class:
727 case tok::kw_const:
728 case tok::kw_const_cast:
729 case tok::kw_continue:
730 case tok::kw_default:
731 case tok::kw_delete:
732 case tok::kw_do:
733 case tok::kw_double:
734 case tok::kw_dynamic_cast:
735 case tok::kw_else:
736 case tok::kw_enum:
737 case tok::kw_explicit:
738 case tok::kw_export:
739 case tok::kw_extern:
740 case tok::kw_false:
741 case tok::kw_float:
742 case tok::kw_for:
743 case tok::kw_friend:
744 case tok::kw_goto:
745 case tok::kw_if:
746 case tok::kw_inline:
747 case tok::kw_int:
748 case tok::kw_long:
749 case tok::kw_mutable:
750 case tok::kw_namespace:
751 case tok::kw_new:
752 case tok::kw_operator:
753 case tok::kw_private:
754 case tok::kw_protected:
755 case tok::kw_public:
756 case tok::kw_register:
757 case tok::kw_reinterpret_cast:
758 case tok::kw_restrict:
759 case tok::kw_return:
760 case tok::kw_short:
761 case tok::kw_signed:
762 case tok::kw_sizeof:
763 case tok::kw_static:
764 case tok::kw_static_cast:
765 case tok::kw_struct:
766 case tok::kw_switch:
767 case tok::kw_template:
768 case tok::kw_this:
769 case tok::kw_throw:
770 case tok::kw_true:
771 case tok::kw_try:
772 case tok::kw_typedef:
773 case tok::kw_typeid:
774 case tok::kw_typename:
775 case tok::kw_typeof:
776 case tok::kw_union:
777 case tok::kw_unsigned:
778 case tok::kw_using:
779 case tok::kw_virtual:
780 case tok::kw_void:
781 case tok::kw_volatile:
782 case tok::kw_wchar_t:
783 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000784 case tok::kw__Bool:
785 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000786 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000787 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000788 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000789 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000790 }
Steve Naroff294494e2007-08-22 16:35:03 +0000791}
792
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000793/// objc-for-collection-in: 'in'
794///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000795bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000796 // FIXME: May have to do additional look-ahead to only allow for
797 // valid tokens following an 'in'; such as an identifier, unary operators,
798 // '[' etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000799 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000800 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000801}
802
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000804/// qualifier list and builds their bitmask representation in the input
805/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000806///
807/// objc-type-qualifiers:
808/// objc-type-qualifier
809/// objc-type-qualifiers objc-type-qualifier
810///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000811void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000812 Declarator::TheContext Context) {
813 assert(Context == Declarator::ObjCParameterContext ||
814 Context == Declarator::ObjCResultContext);
815
Chris Lattnere8b724d2007-12-12 06:56:32 +0000816 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000817 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000818 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000819 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000820 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000821 }
822
Chris Lattnercb53b362007-12-27 19:57:00 +0000823 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000824 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattnere8b724d2007-12-12 06:56:32 +0000826 const IdentifierInfo *II = Tok.getIdentifierInfo();
827 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000829 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000831 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000832 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000833 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000834 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
835 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
836 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
837 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
838 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
839 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000840 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000841 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000842 ConsumeToken();
843 II = 0;
844 break;
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnere8b724d2007-12-12 06:56:32 +0000847 // If this wasn't a recognized qualifier, bail out.
848 if (II) return;
849 }
850}
851
John McCallcdda47f2011-10-01 09:56:14 +0000852/// Take all the decl attributes out of the given list and add
853/// them to the given attribute set.
854static void takeDeclAttributes(ParsedAttributes &attrs,
855 AttributeList *list) {
856 while (list) {
857 AttributeList *cur = list;
858 list = cur->getNext();
859
860 if (!cur->isUsedAsTypeAttr()) {
861 // Clear out the next pointer. We're really completely
862 // destroying the internal invariants of the declarator here,
863 // but it doesn't matter because we're done with it.
864 cur->setNext(0);
865 attrs.add(cur);
866 }
867 }
868}
869
870/// takeDeclAttributes - Take all the decl attributes from the given
871/// declarator and add them to the given list.
872static void takeDeclAttributes(ParsedAttributes &attrs,
873 Declarator &D) {
874 // First, take ownership of all attributes.
875 attrs.getPool().takeAllFrom(D.getAttributePool());
876 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
877
878 // Now actually move the attributes over.
879 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
880 takeDeclAttributes(attrs, D.getAttributes());
881 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
882 takeDeclAttributes(attrs,
883 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
884}
885
Chris Lattnere8b724d2007-12-12 06:56:32 +0000886/// objc-type-name:
887/// '(' objc-type-qualifiers[opt] type-name ')'
888/// '(' objc-type-qualifiers[opt] ')'
889///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000890ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000891 Declarator::TheContext context,
892 ParsedAttributes *paramAttrs) {
893 assert(context == Declarator::ObjCParameterContext ||
894 context == Declarator::ObjCResultContext);
895 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
896
Chris Lattnerdf195262007-10-09 17:51:17 +0000897 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000899 BalancedDelimiterTracker T(*this, tok::l_paren);
900 T.consumeOpen();
901
Chris Lattnere8904e92008-08-23 01:48:03 +0000902 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000903 ObjCDeclContextSwitch ObjCDC(*this);
904
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000905 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000906 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000907
John McCallb3d87482010-08-24 05:47:05 +0000908 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000909 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000910 // Parse an abstract declarator.
911 DeclSpec declSpec(AttrFactory);
912 declSpec.setObjCQualifiers(&DS);
913 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanian6d1de1b2012-05-29 21:52:45 +0000914 declSpec.SetRangeEnd(Tok.getLocation());
John McCallcdda47f2011-10-01 09:56:14 +0000915 Declarator declarator(declSpec, context);
916 ParseDeclarator(declarator);
917
918 // If that's not invalid, extract a type.
919 if (!declarator.isInvalidType()) {
920 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
921 if (!type.isInvalid())
922 Ty = type.get();
923
924 // If we're parsing a parameter, steal all the decl attributes
925 // and add them to the decl spec.
926 if (context == Declarator::ObjCParameterContext)
927 takeDeclAttributes(*paramAttrs, declarator);
928 }
929 } else if (context == Declarator::ObjCResultContext &&
930 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000931 if (!Ident_instancetype)
932 Ident_instancetype = PP.getIdentifierInfo("instancetype");
933
934 if (Tok.getIdentifierInfo() == Ident_instancetype) {
935 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
936 ConsumeToken();
937 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000938 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000939
Steve Naroffd7333c22008-10-21 14:15:04 +0000940 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000941 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000942 else if (Tok.getLocation() == TypeStartLoc) {
943 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000944 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000945 SkipUntil(tok::r_paren);
946 } else {
947 // Otherwise, we found *something*, but didn't get a ')' in the right
948 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000949 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000950 }
Steve Narofff28b2642007-09-05 23:30:30 +0000951 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000952}
953
954/// objc-method-decl:
955/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000956/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000957/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000958/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000959///
960/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000961/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000962/// objc-keyword-selector objc-keyword-decl
963///
964/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000965/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
966/// objc-selector ':' objc-keyword-attributes[opt] identifier
967/// ':' objc-type-name objc-keyword-attributes[opt] identifier
968/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000969///
Steve Naroff4985ace2007-08-22 18:35:33 +0000970/// objc-parmlist:
971/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000972///
Steve Naroff4985ace2007-08-22 18:35:33 +0000973/// objc-parms:
974/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000975///
Steve Naroff4985ace2007-08-22 18:35:33 +0000976/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000977/// , ...
978///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000979/// objc-keyword-attributes: [OBJC2]
980/// __attribute__((unused))
981///
John McCalld226f652010-08-21 09:40:31 +0000982Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000983 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000984 tok::ObjCKeywordKind MethodImplKind,
985 bool MethodDefinition) {
John McCall92576642012-05-07 06:16:41 +0000986 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall54abf7d2009-11-04 02:18:39 +0000987
Douglas Gregore8f5a172010-04-07 00:21:17 +0000988 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000989 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000990 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000991 cutOffParsing();
992 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000993 }
994
Chris Lattnere8904e92008-08-23 01:48:03 +0000995 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000996 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000998 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000999 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Ted Kremenek9e049352010-02-18 23:05:16 +00001001 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00001002 ParsedAttributes methodAttrs(AttrFactory);
David Blaikie4e4d0842012-03-11 07:00:24 +00001003 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001004 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +00001005
Douglas Gregore8f5a172010-04-07 00:21:17 +00001006 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001007 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001008 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001009 cutOffParsing();
1010 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +00001011 }
1012
Ted Kremenek9e049352010-02-18 23:05:16 +00001013 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +00001014 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001015 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +00001016
Steve Naroff84c43102009-02-11 20:43:13 +00001017 // An unnamed colon is valid.
1018 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001019 Diag(Tok, diag::err_expected_selector_for_method)
1020 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniand30ec702012-07-26 17:32:28 +00001021 // Skip until we get a ; or @.
1022 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCalld226f652010-08-21 09:40:31 +00001023 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +00001024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattner5f9e2722011-07-23 10:55:15 +00001026 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001027 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001028 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001029 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001030 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattnerff384912007-10-07 02:00:24 +00001032 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001033 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001034 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001035 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001036 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001037 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001038 methodAttrs.getList(), MethodImplKind,
1039 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001040 PD.complete(Result);
1041 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001042 }
Steve Narofff28b2642007-09-05 23:30:30 +00001043
Chris Lattner5f9e2722011-07-23 10:55:15 +00001044 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001045 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001046 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smith3a2b7a12013-01-28 22:42:45 +00001047 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1048 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001049
1050 AttributePool allParamAttrs(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001051 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001052 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001053 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattnerff384912007-10-07 02:00:24 +00001055 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001056 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001057 Diag(Tok, diag::err_expected_colon);
1058 break;
1059 }
1060 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001061
John McCallb3d87482010-08-24 05:47:05 +00001062 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001063 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001064 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1065 Declarator::ObjCParameterContext,
1066 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001067
Chris Lattnerff384912007-10-07 02:00:24 +00001068 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001069 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001070 ArgInfo.ArgAttrs = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001071 if (getLangOpts().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001072 MaybeParseGNUAttributes(paramAttrs);
1073 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001074 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001075
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001076 // Code completion for the next piece of the selector.
1077 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001078 KeyIdents.push_back(SelIdent);
1079 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1080 mType == tok::minus,
1081 /*AtParameterName=*/true,
1082 ReturnType,
1083 KeyIdents.data(),
1084 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001085 cutOffParsing();
1086 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001087 }
1088
Chris Lattnerdf195262007-10-09 17:51:17 +00001089 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001090 Diag(Tok, diag::err_expected_ident); // missing argument name.
1091 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattnere294d3f2009-04-11 18:57:04 +00001094 ArgInfo.Name = Tok.getIdentifierInfo();
1095 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001096 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Chris Lattnere294d3f2009-04-11 18:57:04 +00001098 ArgInfos.push_back(ArgInfo);
1099 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001100 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001101
John McCall0b7e6782011-03-24 11:26:52 +00001102 // Make sure the attributes persist.
1103 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1104
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001105 // Code completion for the next piece of the selector.
1106 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001107 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1108 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001109 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001110 ReturnType,
1111 KeyIdents.data(),
1112 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001113 cutOffParsing();
1114 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001115 }
1116
Chris Lattnerff384912007-10-07 02:00:24 +00001117 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001118 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenekb648cf02012-09-12 16:50:35 +00001119 if (!SelIdent && Tok.isNot(tok::colon))
1120 break;
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001121 if (!SelIdent) {
Fariborz Jahanian08602352012-09-17 19:15:26 +00001122 SourceLocation ColonLoc = Tok.getLocation();
1123 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001124 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1125 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1126 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanian08602352012-09-17 19:15:26 +00001127 }
1128 }
Chris Lattnerff384912007-10-07 02:00:24 +00001129 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Steve Naroff335eafa2007-11-15 12:35:21 +00001132 bool isVariadic = false;
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001133 bool cStyleParamWarned = false;
Chris Lattnerff384912007-10-07 02:00:24 +00001134 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001135 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001136 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001137 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001138 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001139 ConsumeToken();
1140 break;
1141 }
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001142 if (!cStyleParamWarned) {
1143 Diag(Tok, diag::warn_cstyle_param);
1144 cStyleParamWarned = true;
1145 }
John McCall0b7e6782011-03-24 11:26:52 +00001146 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001147 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001148 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001149 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1150 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001151 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001152 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001153 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1154 ParmDecl.getIdentifierLoc(),
1155 Param,
1156 0));
Chris Lattnerff384912007-10-07 02:00:24 +00001157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001159 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001160 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001161 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001162 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001163
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001164 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001165 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001166
Chris Lattnerff384912007-10-07 02:00:24 +00001167 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1168 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001169 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001170 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001171 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001172 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001173 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001174 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001175 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001176
John McCall54abf7d2009-11-04 02:18:39 +00001177 PD.complete(Result);
1178 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001179}
1180
Steve Naroffdac269b2007-08-20 21:31:48 +00001181/// objc-protocol-refs:
1182/// '<' identifier-list '>'
1183///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001184bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001185ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1186 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001187 bool WarnOnDeclarations,
1188 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001189 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001191 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Chris Lattner5f9e2722011-07-23 10:55:15 +00001193 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Chris Lattnere13b9592008-07-26 04:03:38 +00001195 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001196 if (Tok.is(tok::code_completion)) {
1197 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1198 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001199 cutOffParsing();
1200 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001201 }
1202
Chris Lattnere13b9592008-07-26 04:03:38 +00001203 if (Tok.isNot(tok::identifier)) {
1204 Diag(Tok, diag::err_expected_ident);
1205 SkipUntil(tok::greater);
1206 return true;
1207 }
1208 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1209 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001210 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001211 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Chris Lattnere13b9592008-07-26 04:03:38 +00001213 if (Tok.isNot(tok::comma))
1214 break;
1215 ConsumeToken();
1216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Chris Lattnere13b9592008-07-26 04:03:38 +00001218 // Consume the '>'.
Nico Weberb707a472012-12-14 18:22:38 +00001219 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattnere13b9592008-07-26 04:03:38 +00001220 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Chris Lattnere13b9592008-07-26 04:03:38 +00001222 // Convert the list of protocols identifiers into a list of protocol decls.
1223 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1224 &ProtocolIdents[0], ProtocolIdents.size(),
1225 Protocols);
1226 return false;
1227}
1228
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001229/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1230/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001231bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001232 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikie4e4d0842012-03-11 07:00:24 +00001233 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001234 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001235 SmallVector<Decl *, 8> ProtocolDecl;
1236 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001237 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1238 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001239 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1240 ProtocolLocs.data(), LAngleLoc);
1241 if (EndProtoLoc.isValid())
1242 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001243 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001244}
1245
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001246void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1247 BalancedDelimiterTracker &T,
1248 SmallVectorImpl<Decl *> &AllIvarDecls,
1249 bool RBraceMissing) {
1250 if (!RBraceMissing)
1251 T.consumeClose();
1252
1253 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1254 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1255 Actions.ActOnObjCContainerFinishDefinition();
1256 // Call ActOnFields() even if we don't have any decls. This is useful
1257 // for code rewriting tools that need to be aware of the empty list.
1258 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1259 AllIvarDecls,
1260 T.getOpenLocation(), T.getCloseLocation(), 0);
1261}
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001262
Steve Naroffdac269b2007-08-20 21:31:48 +00001263/// objc-class-instance-variables:
1264/// '{' objc-instance-variable-decl-list[opt] '}'
1265///
1266/// objc-instance-variable-decl-list:
1267/// objc-visibility-spec
1268/// objc-instance-variable-decl ';'
1269/// ';'
1270/// objc-instance-variable-decl-list objc-visibility-spec
1271/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1272/// objc-instance-variable-decl-list ';'
1273///
1274/// objc-visibility-spec:
1275/// @private
1276/// @protected
1277/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001278/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001279///
1280/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001281/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001282///
John McCalld226f652010-08-21 09:40:31 +00001283void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001284 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001285 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001286 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001287 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001288
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001289 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001290 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001291
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001292 BalancedDelimiterTracker T(*this, tok::l_brace);
1293 T.consumeOpen();
Steve Naroffddbff782007-08-21 21:17:12 +00001294 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001295 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001296 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Steve Naroffddbff782007-08-21 21:17:12 +00001298 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001299 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001300 ConsumeExtraSemi(InstanceVariableList);
Steve Naroffddbff782007-08-21 21:17:12 +00001301 continue;
1302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Steve Naroffddbff782007-08-21 21:17:12 +00001304 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001305 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001306 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001307
1308 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001309 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001310 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001311 }
1312
Steve Naroff861cf3e2007-08-23 18:16:40 +00001313 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001314 case tok::objc_private:
1315 case tok::objc_public:
1316 case tok::objc_protected:
1317 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001318 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001319 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001320 continue;
Fariborz Jahanian695031c2013-03-20 18:45:49 +00001321
1322 case tok::objc_end:
1323 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001324 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1325 Tok.setKind(tok::at);
1326 Tok.setLength(1);
1327 PP.EnterToken(Tok);
1328 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1329 T, AllIvarDecls, true);
1330 return;
Fariborz Jahanian695031c2013-03-20 18:45:49 +00001331
1332 default:
1333 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1334 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001335 }
1336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001338 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001339 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001340 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001341 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001342 }
1343
John McCallbdd563e2009-11-03 02:38:08 +00001344 struct ObjCIvarCallback : FieldCallback {
1345 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001346 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001347 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001348 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001349
John McCalld226f652010-08-21 09:40:31 +00001350 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001351 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001352 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1353 }
1354
Eli Friedmandcdff462012-08-08 23:53:27 +00001355 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001356 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001357 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001358 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001359 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001360 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001361 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001362 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001363 if (Field)
1364 AllIvarDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001365 FD.complete(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001366 }
1367 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001368
Chris Lattnere1359422008-04-10 06:46:29 +00001369 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001370 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001371 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Chris Lattnerdf195262007-10-09 17:51:17 +00001373 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001374 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001375 } else {
1376 Diag(Tok, diag::err_expected_semi_decl_list);
1377 // Skip to end of block or statement
1378 SkipUntil(tok::r_brace, true, true);
1379 }
1380 }
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001381 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1382 T, AllIvarDecls, false);
Steve Naroffddbff782007-08-21 21:17:12 +00001383 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001384}
Steve Naroffdac269b2007-08-20 21:31:48 +00001385
1386/// objc-protocol-declaration:
1387/// objc-protocol-definition
1388/// objc-protocol-forward-reference
1389///
1390/// objc-protocol-definition:
James Dennett17d26a62012-06-11 06:19:40 +00001391/// \@protocol identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001392/// objc-protocol-refs[opt]
1393/// objc-interface-decl-list
James Dennett17d26a62012-06-11 06:19:40 +00001394/// \@end
Steve Naroffdac269b2007-08-20 21:31:48 +00001395///
1396/// objc-protocol-forward-reference:
James Dennett17d26a62012-06-11 06:19:40 +00001397/// \@protocol identifier-list ';'
Steve Naroffdac269b2007-08-20 21:31:48 +00001398///
James Dennett17d26a62012-06-11 06:19:40 +00001399/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001400/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001401/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001402Parser::DeclGroupPtrTy
1403Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1404 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001405 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001406 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1407 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor083128f2009-11-18 04:49:41 +00001409 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001410 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001411 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001412 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001413 }
1414
Nico Webere145bfd2013-04-04 00:15:10 +00001415 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber9f4f5f12013-04-03 17:36:11 +00001416
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001418 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001419 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001420 }
1421 // Save the protocol name, then consume it.
1422 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1423 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Chris Lattnerdf195262007-10-09 17:51:17 +00001425 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001426 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001427 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001428 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001429 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001432 CheckNestedObjCContexts(AtLoc);
1433
Chris Lattnerdf195262007-10-09 17:51:17 +00001434 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001435 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001436 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1437
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001438 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001439 while (1) {
1440 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001441 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001442 Diag(Tok, diag::err_expected_ident);
1443 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001444 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001445 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001446 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1447 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001448 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Chris Lattnerdf195262007-10-09 17:51:17 +00001450 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001451 break;
1452 }
1453 // Consume the ';'.
1454 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001455 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Steve Naroffe440eb82007-10-10 17:32:04 +00001457 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001458 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001459 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001460 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001461 }
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001463 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001464 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001465
Chris Lattner5f9e2722011-07-23 10:55:15 +00001466 SmallVector<Decl *, 8> ProtocolRefs;
1467 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001468 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001469 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1470 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001471 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001472
John McCalld226f652010-08-21 09:40:31 +00001473 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001474 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001475 ProtocolRefs.data(),
1476 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001477 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001478 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001479
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001480 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001481 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001482}
Steve Naroffdac269b2007-08-20 21:31:48 +00001483
1484/// objc-implementation:
1485/// objc-class-implementation-prologue
1486/// objc-category-implementation-prologue
1487///
1488/// objc-class-implementation-prologue:
1489/// @implementation identifier objc-superclass[opt]
1490/// objc-class-instance-variables[opt]
1491///
1492/// objc-category-implementation-prologue:
1493/// @implementation identifier ( identifier )
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001494Parser::DeclGroupPtrTy
1495Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001496 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1497 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001498 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001499 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001501 // Code completion after '@implementation'.
1502 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001503 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001504 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001505 return DeclGroupPtrTy();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001506 }
1507
Nico Webere145bfd2013-04-04 00:15:10 +00001508 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber9f4f5f12013-04-03 17:36:11 +00001509
Chris Lattnerdf195262007-10-09 17:51:17 +00001510 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001511 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001512 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001513 }
1514 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001515 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001516 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001517 Decl *ObjCImpDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001518
1519 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001520 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001521 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001522 SourceLocation categoryLoc, rparenLoc;
1523 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001525 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001526 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001527 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001528 return DeclGroupPtrTy();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001529 }
1530
Chris Lattnerdf195262007-10-09 17:51:17 +00001531 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001532 categoryId = Tok.getIdentifierInfo();
1533 categoryLoc = ConsumeToken();
1534 } else {
1535 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001536 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001537 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001538 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001539 Diag(Tok, diag::err_expected_rparen);
1540 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001541 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001542 }
1543 rparenLoc = ConsumeParen();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001544 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001545 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001546 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001547
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001548 } else {
1549 // We have a class implementation
1550 SourceLocation superClassLoc;
1551 IdentifierInfo *superClassId = 0;
1552 if (Tok.is(tok::colon)) {
1553 // We have a super class
1554 ConsumeToken();
1555 if (Tok.isNot(tok::identifier)) {
1556 Diag(Tok, diag::err_expected_ident); // missing super class name.
1557 return DeclGroupPtrTy();
1558 }
1559 superClassId = Tok.getIdentifierInfo();
1560 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001561 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001562 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1563 AtLoc, nameId, nameLoc,
1564 superClassId, superClassLoc);
1565
1566 if (Tok.is(tok::l_brace)) // we have ivars
1567 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001568 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001569 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001571 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001572
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001573 {
1574 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1575 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1576 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001577 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001578 MaybeParseMicrosoftAttributes(attrs);
1579 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1580 DeclGroupRef DG = DGP.get();
1581 DeclsInGroup.append(DG.begin(), DG.end());
1582 }
1583 }
1584 }
1585
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001586 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001587}
Steve Naroff60fccee2007-10-29 21:38:07 +00001588
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001589Parser::DeclGroupPtrTy
1590Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001591 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1592 "ParseObjCAtEndDeclaration(): Expected @end");
1593 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001594 if (CurParsedObjCImpl)
1595 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001596 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001597 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001598 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001599 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001600}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001601
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001602Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1603 if (!Finished) {
1604 finish(P.Tok.getLocation());
1605 if (P.Tok.is(tok::eof)) {
1606 P.Diag(P.Tok, diag::err_objc_missing_end)
1607 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1608 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1609 << Sema::OCK_Implementation;
1610 }
1611 }
1612 P.CurParsedObjCImpl = 0;
1613 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001614}
1615
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001616void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1617 assert(!Finished);
1618 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1619 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001620 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1621 true/*Methods*/);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001622
1623 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1624
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001625 if (HasCFunction)
1626 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1627 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1628 false/*c-functions*/);
1629
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001630 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001631 for (LateParsedObjCMethodContainer::iterator
1632 I = LateParsedObjCMethods.begin(),
1633 E = LateParsedObjCMethods.end(); I != E; ++I)
1634 delete *I;
1635 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001636
1637 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001638}
1639
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001640/// compatibility-alias-decl:
1641/// @compatibility_alias alias-name class-name ';'
1642///
John McCalld226f652010-08-21 09:40:31 +00001643Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001644 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1645 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1646 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001647 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001648 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001649 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001650 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001651 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1652 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001653 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001654 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001655 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001656 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001657 IdentifierInfo *classId = Tok.getIdentifierInfo();
1658 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001659 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1660 "@compatibility_alias");
Richard Smithde01b7a2012-08-08 23:32:13 +00001661 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1662 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001663}
1664
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001665/// property-synthesis:
1666/// @synthesize property-ivar-list ';'
1667///
1668/// property-ivar-list:
1669/// property-ivar
1670/// property-ivar-list ',' property-ivar
1671///
1672/// property-ivar:
1673/// identifier
1674/// identifier '=' identifier
1675///
John McCalld226f652010-08-21 09:40:31 +00001676Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001677 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1678 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001679 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Douglas Gregorb328c422009-11-18 19:45:45 +00001681 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001682 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001683 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001684 cutOffParsing();
1685 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001686 }
1687
Douglas Gregorb328c422009-11-18 19:45:45 +00001688 if (Tok.isNot(tok::identifier)) {
1689 Diag(Tok, diag::err_synthesized_property_name);
1690 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001691 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001692 }
1693
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001694 IdentifierInfo *propertyIvar = 0;
1695 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1696 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001697 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001698 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001699 // property '=' ivar-name
1700 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001701
1702 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001703 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001704 cutOffParsing();
1705 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001706 }
1707
Chris Lattnerdf195262007-10-09 17:51:17 +00001708 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001709 Diag(Tok, diag::err_expected_ident);
1710 break;
1711 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001712 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001713 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001714 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001715 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001716 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001717 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001718 break;
1719 ConsumeToken(); // consume ','
1720 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001721 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001722 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001723}
1724
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001725/// property-dynamic:
1726/// @dynamic property-list
1727///
1728/// property-list:
1729/// identifier
1730/// property-list ',' identifier
1731///
John McCalld226f652010-08-21 09:40:31 +00001732Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001733 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1734 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001735 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001736 while (true) {
1737 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001738 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001739 cutOffParsing();
1740 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001741 }
1742
1743 if (Tok.isNot(tok::identifier)) {
1744 Diag(Tok, diag::err_expected_ident);
1745 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001746 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001747 }
1748
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001749 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1750 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001751 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001752 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001753
Chris Lattnerdf195262007-10-09 17:51:17 +00001754 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001755 break;
1756 ConsumeToken(); // consume ','
1757 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001758 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001759 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001760}
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001762/// objc-throw-statement:
1763/// throw expression[opt];
1764///
John McCall60d7b3a2010-08-24 06:29:42 +00001765StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1766 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001767 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001768 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001769 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001770 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001771 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001772 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001773 }
1774 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001775 // consume ';'
1776 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001777 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001778}
1779
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001780/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001781/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001782///
John McCall60d7b3a2010-08-24 06:29:42 +00001783StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001784Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001785 ConsumeToken(); // consume synchronized
1786 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001787 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001788 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001789 }
John McCall07524032011-07-27 21:50:02 +00001790
1791 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001792 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001793 ExprResult operand(ParseExpression());
1794
1795 if (Tok.is(tok::r_paren)) {
1796 ConsumeParen(); // ')'
1797 } else {
1798 if (!operand.isInvalid())
1799 Diag(Tok, diag::err_expected_rparen);
1800
1801 // Skip forward until we see a left brace, but don't consume it.
1802 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001803 }
John McCall07524032011-07-27 21:50:02 +00001804
1805 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001806 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001807 if (!operand.isInvalid())
1808 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001809 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001810 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001811
John McCall07524032011-07-27 21:50:02 +00001812 // Check the @synchronized operand now.
1813 if (!operand.isInvalid())
1814 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001815
John McCall07524032011-07-27 21:50:02 +00001816 // Parse the compound statement within a new scope.
1817 ParseScope bodyScope(this, Scope::DeclScope);
1818 StmtResult body(ParseCompoundStatementBody());
1819 bodyScope.Exit();
1820
1821 // If there was a semantic or parse error earlier with the
1822 // operand, fail now.
1823 if (operand.isInvalid())
1824 return StmtError();
1825
1826 if (body.isInvalid())
1827 body = Actions.ActOnNullStmt(Tok.getLocation());
1828
1829 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001830}
1831
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001832/// objc-try-catch-statement:
1833/// @try compound-statement objc-catch-list[opt]
1834/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1835///
1836/// objc-catch-list:
1837/// @catch ( parameter-declaration ) compound-statement
1838/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1839/// catch-parameter-declaration:
1840/// parameter-declaration
1841/// '...' [OBJC2]
1842///
John McCall60d7b3a2010-08-24 06:29:42 +00001843StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001844 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001845
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001846 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001847 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001848 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001849 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001850 }
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001851 StmtVector CatchStmts;
John McCall60d7b3a2010-08-24 06:29:42 +00001852 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001853 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001854 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001855 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001856 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001857 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001858
Chris Lattnerdf195262007-10-09 17:51:17 +00001859 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001860 // At this point, we need to lookahead to determine if this @ is the start
1861 // of an @catch or @finally. We don't want to consume the @ token if this
1862 // is an @try or @encode or something else.
1863 Token AfterAt = GetLookAheadToken(1);
1864 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1865 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1866 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001867
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001868 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001869 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001870 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001871 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001872 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001873 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001874 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001875 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001876 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001877 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001878 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001879 ParseDeclarator(ParmDecl);
1880
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001881 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001882 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001883 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001884 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001885 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Steve Naroff93a25952009-04-07 22:56:58 +00001887 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Steve Naroff93a25952009-04-07 22:56:58 +00001889 if (Tok.is(tok::r_paren))
1890 RParenLoc = ConsumeParen();
1891 else // Skip over garbage, until we get to ')'. Eat the ')'.
1892 SkipUntil(tok::r_paren, true, false);
1893
John McCall60d7b3a2010-08-24 06:29:42 +00001894 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001895 if (Tok.is(tok::l_brace))
1896 CatchBody = ParseCompoundStatementBody();
1897 else
1898 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001899 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001900 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001901
John McCall60d7b3a2010-08-24 06:29:42 +00001902 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001903 RParenLoc,
1904 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001905 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001906 if (!Catch.isInvalid())
1907 CatchStmts.push_back(Catch.release());
1908
Steve Naroff64515f32008-02-05 21:27:35 +00001909 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001910 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1911 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001912 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001913 }
1914 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001915 } else {
1916 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001917 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001918 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001919
John McCall60d7b3a2010-08-24 06:29:42 +00001920 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001921 if (Tok.is(tok::l_brace))
1922 FinallyBody = ParseCompoundStatementBody();
1923 else
1924 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001925 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001926 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001927 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001928 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001929 catch_or_finally_seen = true;
1930 break;
1931 }
1932 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001933 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001934 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001935 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001936 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001937
John McCall9ae2f072010-08-23 23:25:46 +00001938 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001939 CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001940 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001941}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001942
John McCallf85e1932011-06-15 23:02:42 +00001943/// objc-autoreleasepool-statement:
1944/// @autoreleasepool compound-statement
1945///
1946StmtResult
1947Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1948 ConsumeToken(); // consume autoreleasepool
1949 if (Tok.isNot(tok::l_brace)) {
1950 Diag(Tok, diag::err_expected_lbrace);
1951 return StmtError();
1952 }
1953 // Enter a scope to hold everything within the compound stmt. Compound
1954 // statements can always hold declarations.
1955 ParseScope BodyScope(this, Scope::DeclScope);
1956
1957 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1958
1959 BodyScope.Exit();
1960 if (AutoreleasePoolBody.isInvalid())
1961 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1962 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1963 AutoreleasePoolBody.take());
1964}
1965
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001966/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1967/// for later parsing.
1968void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1969 LexedMethod* LM = new LexedMethod(this, MDecl);
1970 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1971 CachedTokens &Toks = LM->Toks;
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001972 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001973 Toks.push_back(Tok);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001974 if (Tok.is(tok::kw_try)) {
1975 ConsumeToken();
Fariborz Jahaniandbd69452012-08-10 20:34:17 +00001976 if (Tok.is(tok::colon)) {
1977 Toks.push_back(Tok);
1978 ConsumeToken();
1979 while (Tok.isNot(tok::l_brace)) {
1980 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1981 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1982 }
1983 }
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001984 Toks.push_back(Tok); // also store '{'
1985 }
1986 else if (Tok.is(tok::colon)) {
1987 ConsumeToken();
1988 while (Tok.isNot(tok::l_brace)) {
1989 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1990 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1991 }
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001992 Toks.push_back(Tok); // also store '{'
1993 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001994 ConsumeBrace();
1995 // Consume everything up to (and including) the matching right brace.
1996 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001997 while (Tok.is(tok::kw_catch)) {
1998 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1999 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2000 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002001}
2002
Steve Naroff3536b442007-09-06 21:24:23 +00002003/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002004///
John McCalld226f652010-08-21 09:40:31 +00002005Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002006 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00002007
John McCallf312b1e2010-08-26 23:41:50 +00002008 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2009 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002011 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00002012 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002013 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00002014 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00002015 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00002016 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002017 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00002018 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002019
Steve Naroff409be832007-11-11 19:54:21 +00002020 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00002021 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002022 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Steve Naroff409be832007-11-11 19:54:21 +00002024 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2025 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Steve Naroff409be832007-11-11 19:54:21 +00002027 // If we didn't find the '{', bail out.
2028 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00002029 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002030 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002031
2032 if (!MDecl) {
2033 ConsumeBrace();
2034 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2035 return 0;
2036 }
2037
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002038 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002039 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahanianc9b97092012-08-09 17:15:00 +00002040 assert (CurParsedObjCImpl
2041 && "ParseObjCMethodDefinition - Method out of @implementation");
2042 // Consume the tokens and store them for later parsing.
2043 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff71c0a952007-11-13 23:01:27 +00002044 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00002045}
Anders Carlsson55085182007-08-21 17:43:55 +00002046
John McCall60d7b3a2010-08-24 06:29:42 +00002047StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002048 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002049 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002050 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002051 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00002052 }
2053
2054 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00002055 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002056
2057 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00002058 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002059
2060 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00002061 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00002062
2063 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2064 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002065
John McCall60d7b3a2010-08-24 06:29:42 +00002066 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002067 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00002068 // If the expression is invalid, skip ahead to the next semicolon. Not
2069 // doing this opens us up to the possibility of infinite loops if
2070 // ParseExpression does not consume any tokens.
2071 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00002072 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00002073 }
Chris Lattner5d803162009-12-07 16:33:19 +00002074
Steve Naroff64515f32008-02-05 21:27:35 +00002075 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00002076 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +00002077 return Actions.ActOnExprStmt(Res);
Steve Naroff64515f32008-02-05 21:27:35 +00002078}
2079
John McCall60d7b3a2010-08-24 06:29:42 +00002080ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002081 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002082 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002083 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002084 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002085 return ExprError();
2086
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002087 case tok::minus:
2088 case tok::plus: {
2089 tok::TokenKind Kind = Tok.getKind();
2090 SourceLocation OpLoc = ConsumeToken();
2091
2092 if (!Tok.is(tok::numeric_constant)) {
2093 const char *Symbol = 0;
2094 switch (Kind) {
2095 case tok::minus: Symbol = "-"; break;
2096 case tok::plus: Symbol = "+"; break;
2097 default: llvm_unreachable("missing unary operator case");
2098 }
2099 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2100 << Symbol;
2101 return ExprError();
2102 }
2103
2104 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2105 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002106 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002107 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002108 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002109
2110 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2111 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002112 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002113
2114 return ParsePostfixExpressionSuffix(
2115 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2116 }
2117
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002118 case tok::string_literal: // primary-expression: string-literal
2119 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002120 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002121
2122 case tok::char_constant:
2123 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2124
2125 case tok::numeric_constant:
2126 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2127
2128 case tok::kw_true: // Objective-C++, etc.
2129 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2130 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2131 case tok::kw_false: // Objective-C++, etc.
2132 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2133 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2134
2135 case tok::l_square:
2136 // Objective-C array literal
2137 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2138
2139 case tok::l_brace:
2140 // Objective-C dictionary literal
2141 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2142
Patrick Beardeb382ec2012-04-19 00:25:12 +00002143 case tok::l_paren:
2144 // Objective-C boxed expression
2145 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2146
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002147 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002148 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002149 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002150
Chris Lattner4fef81d2008-08-05 06:19:09 +00002151 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2152 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002153 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002154 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002155 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002156 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002157 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian6749ae12012-07-09 20:00:35 +00002158 default: {
2159 const char *str = 0;
2160 if (GetLookAheadToken(1).is(tok::l_brace)) {
2161 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2162 str =
2163 ch == 't' ? "try"
2164 : (ch == 'f' ? "finally"
2165 : (ch == 'a' ? "autoreleasepool" : 0));
2166 }
2167 if (str) {
2168 SourceLocation kwLoc = Tok.getLocation();
2169 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2170 FixItHint::CreateReplacement(kwLoc, str));
2171 }
2172 else
2173 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2174 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002175 }
Anders Carlsson55085182007-08-21 17:43:55 +00002176 }
Anders Carlsson55085182007-08-21 17:43:55 +00002177}
2178
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002179/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002180///
2181/// This routine parses the receiver of a message send in
2182/// Objective-C++ either as a type or as an expression. Note that this
2183/// routine must not be called to parse a send to 'super', since it
2184/// has no way to return such a result.
2185///
2186/// \param IsExpr Whether the receiver was parsed as an expression.
2187///
2188/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2189/// IsExpr is true), the parsed expression. If the receiver was parsed
2190/// as a type (\c IsExpr is false), the parsed type.
2191///
2192/// \returns True if an error occurred during parsing or semantic
2193/// analysis, in which case the arguments do not have valid
2194/// values. Otherwise, returns false for a successful parse.
2195///
2196/// objc-receiver: [C++]
2197/// 'super' [not parsed here]
2198/// expression
2199/// simple-type-specifier
2200/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002201bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002202 InMessageExpressionRAIIObject InMessage(*this, true);
2203
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002204 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2205 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2206 TryAnnotateTypeOrScopeToken();
2207
Kaelyn Uhrain7bf33402012-06-15 23:45:51 +00002208 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002209 // objc-receiver:
2210 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002211 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002212 if (Receiver.isInvalid())
2213 return true;
2214
2215 IsExpr = true;
2216 TypeOrExpr = Receiver.take();
2217 return false;
2218 }
2219
2220 // objc-receiver:
2221 // typename-specifier
2222 // simple-type-specifier
2223 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002224 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002225 ParseCXXSimpleTypeSpecifier(DS);
2226
2227 if (Tok.is(tok::l_paren)) {
2228 // If we see an opening parentheses at this point, we are
2229 // actually parsing an expression that starts with a
2230 // function-style cast, e.g.,
2231 //
2232 // postfix-expression:
2233 // simple-type-specifier ( expression-list [opt] )
2234 // typename-specifier ( expression-list [opt] )
2235 //
2236 // Parse the remainder of this case, then the (optional)
2237 // postfix-expression suffix, followed by the (optional)
2238 // right-hand side of the binary expression. We have an
2239 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002240 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002241 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002242 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002243 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002244 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002245 if (Receiver.isInvalid())
2246 return true;
2247
2248 IsExpr = true;
2249 TypeOrExpr = Receiver.take();
2250 return false;
2251 }
2252
2253 // We have a class message. Turn the simple-type-specifier or
2254 // typename-specifier we parsed into a type and parse the
2255 // remainder of the class message.
2256 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002257 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002258 if (Type.isInvalid())
2259 return true;
2260
2261 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002262 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002263 return false;
2264}
2265
Douglas Gregor1b730e82010-05-31 14:40:22 +00002266/// \brief Determine whether the parser is currently referring to a an
2267/// Objective-C message send, using a simplified heuristic to avoid overhead.
2268///
2269/// This routine will only return true for a subset of valid message-send
2270/// expressions.
2271bool Parser::isSimpleObjCMessageExpression() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002272 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002273 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002274 return GetLookAheadToken(1).is(tok::identifier) &&
2275 GetLookAheadToken(2).is(tok::identifier);
2276}
2277
Douglas Gregor9497a732010-09-16 01:51:54 +00002278bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002279 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregor9497a732010-09-16 01:51:54 +00002280 InMessageExpression)
2281 return false;
2282
2283
2284 ParsedType Type;
2285
2286 if (Tok.is(tok::annot_typename))
2287 Type = getTypeAnnotation(Tok);
2288 else if (Tok.is(tok::identifier))
2289 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2290 getCurScope());
2291 else
2292 return false;
2293
2294 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2295 const Token &AfterNext = GetLookAheadToken(2);
2296 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2297 if (Tok.is(tok::identifier))
2298 TryAnnotateTypeOrScopeToken();
2299
2300 return Tok.is(tok::annot_typename);
2301 }
2302 }
2303
2304 return false;
2305}
2306
Mike Stump1eb44332009-09-09 15:08:12 +00002307/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002308/// '[' objc-receiver objc-message-args ']'
2309///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002310/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002311/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002312/// expression
2313/// class-name
2314/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002315///
John McCall60d7b3a2010-08-24 06:29:42 +00002316ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002317 assert(Tok.is(tok::l_square) && "'[' expected");
2318 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2319
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002320 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002321 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002322 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002323 return ExprError();
2324 }
2325
Douglas Gregor0fbda682010-09-15 14:51:05 +00002326 InMessageExpressionRAIIObject InMessage(*this, true);
2327
David Blaikie4e4d0842012-03-11 07:00:24 +00002328 if (getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002329 // We completely separate the C and C++ cases because C++ requires
2330 // more complicated (read: slower) parsing.
2331
2332 // Handle send to super.
2333 // FIXME: This doesn't benefit from the same typo-correction we
2334 // get in Objective-C.
2335 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002336 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002337 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2338 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002339
2340 // Parse the receiver, which is either a type or an expression.
2341 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002342 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002343 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2344 SkipUntil(tok::r_square);
2345 return ExprError();
2346 }
2347
2348 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002349 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2350 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002351 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002352
2353 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002354 ParsedType::getFromOpaquePtr(TypeOrExpr),
2355 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002356 }
2357
2358 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002359 IdentifierInfo *Name = Tok.getIdentifierInfo();
2360 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002361 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002362 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002363 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002364 NextToken().is(tok::period),
2365 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002366 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002367 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2368 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002369
John McCallf312b1e2010-08-26 23:41:50 +00002370 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002371 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002372 SkipUntil(tok::r_square);
2373 return ExprError();
2374 }
2375
Douglas Gregor1569f952010-04-21 20:38:13 +00002376 ConsumeToken(); // the type name
2377
2378 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002379 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002380
John McCallf312b1e2010-08-26 23:41:50 +00002381 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002382 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002383 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002384 }
Chris Lattner699b6612008-01-25 18:59:06 +00002385 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002386
2387 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002388 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002389 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002390 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002391 return Res;
Chris Lattner699b6612008-01-25 18:59:06 +00002392 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002393
John McCallb3d87482010-08-24 05:47:05 +00002394 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2395 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002396}
Sebastian Redl1d922962008-12-13 15:32:12 +00002397
Douglas Gregor2725ca82010-04-21 19:57:20 +00002398/// \brief Parse the remainder of an Objective-C message following the
2399/// '[' objc-receiver.
2400///
2401/// This routine handles sends to super, class messages (sent to a
2402/// class name), and instance messages (sent to an object), and the
2403/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2404/// ReceiverExpr, respectively. Only one of these parameters may have
2405/// a valid value.
2406///
2407/// \param LBracLoc The location of the opening '['.
2408///
2409/// \param SuperLoc If this is a send to 'super', the location of the
2410/// 'super' keyword that indicates a send to the superclass.
2411///
2412/// \param ReceiverType If this is a class message, the type of the
2413/// class we are sending a message to.
2414///
2415/// \param ReceiverExpr If this is an instance message, the expression
2416/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002417///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002418/// objc-message-args:
2419/// objc-selector
2420/// objc-keywordarg-list
2421///
2422/// objc-keywordarg-list:
2423/// objc-keywordarg
2424/// objc-keywordarg-list objc-keywordarg
2425///
Mike Stump1eb44332009-09-09 15:08:12 +00002426/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002427/// selector-name[opt] ':' objc-keywordexpr
2428///
2429/// objc-keywordexpr:
2430/// nonempty-expr-list
2431///
2432/// nonempty-expr-list:
2433/// assignment-expression
2434/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002435///
John McCall60d7b3a2010-08-24 06:29:42 +00002436ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002437Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002438 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002439 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002440 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002441 InMessageExpressionRAIIObject InMessage(*this, true);
2442
Steve Naroffc4df6d22009-11-07 02:08:14 +00002443 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002444 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002445 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2446 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002447 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002448 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2449 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002450 else
John McCall9ae2f072010-08-23 23:25:46 +00002451 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002452 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002453 cutOffParsing();
2454 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002455 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002456
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002457 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002458 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002459 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002460
Chris Lattner5f9e2722011-07-23 10:55:15 +00002461 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002462 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002463 ExprVector KeyExprs;
Steve Naroff68d331a2007-09-27 14:38:14 +00002464
Chris Lattnerdf195262007-10-09 17:51:17 +00002465 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002466 while (1) {
2467 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002468 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002469 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002470
Chris Lattnerdf195262007-10-09 17:51:17 +00002471 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002472 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002473 // We must manually skip to a ']', otherwise the expression skipper will
2474 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2475 // the enclosing expression.
2476 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002477 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002478 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002479
Steve Naroff68d331a2007-09-27 14:38:14 +00002480 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002481 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002482
2483 if (Tok.is(tok::code_completion)) {
2484 if (SuperLoc.isValid())
2485 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2486 KeyIdents.data(),
2487 KeyIdents.size(),
2488 /*AtArgumentEpression=*/true);
2489 else if (ReceiverType)
2490 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2491 KeyIdents.data(),
2492 KeyIdents.size(),
2493 /*AtArgumentEpression=*/true);
2494 else
2495 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2496 KeyIdents.data(),
2497 KeyIdents.size(),
2498 /*AtArgumentEpression=*/true);
2499
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002500 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002501 return ExprError();
2502 }
2503
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002504 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002505 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002506 // We must manually skip to a ']', otherwise the expression skipper will
2507 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2508 // the enclosing expression.
2509 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002510 return Res;
Steve Naroff37387c92007-09-17 20:25:27 +00002511 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002512
Steve Naroff37387c92007-09-17 20:25:27 +00002513 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002514 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002515
Douglas Gregord3c68542009-11-19 01:08:35 +00002516 // Code completion after each argument.
2517 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002518 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002519 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002520 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002521 KeyIdents.size(),
2522 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002523 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002524 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002525 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002526 KeyIdents.size(),
2527 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002528 else
John McCall9ae2f072010-08-23 23:25:46 +00002529 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002530 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002531 KeyIdents.size(),
2532 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002533 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002534 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002535 }
2536
Steve Naroff37387c92007-09-17 20:25:27 +00002537 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002538 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002539 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002540 break;
2541 // We have a selector or a colon, continue parsing.
2542 }
2543 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002544 while (Tok.is(tok::comma)) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002545 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002546 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002547 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002548 if (Res.isInvalid()) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002549 if (Tok.is(tok::colon)) {
2550 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2551 FixItHint::CreateRemoval(commaLoc);
2552 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002553 // We must manually skip to a ']', otherwise the expression skipper will
2554 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2555 // the enclosing expression.
2556 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002557 return Res;
Steve Naroff49f109c2007-11-15 13:05:42 +00002558 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002559
Steve Naroff49f109c2007-11-15 13:05:42 +00002560 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002561 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002562 }
2563 } else if (!selIdent) {
2564 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002565
Chris Lattner4fef81d2008-08-05 06:19:09 +00002566 // We must manually skip to a ']', otherwise the expression skipper will
2567 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2568 // the enclosing expression.
2569 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002570 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002571 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002572
Chris Lattnerdf195262007-10-09 17:51:17 +00002573 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002574 if (Tok.is(tok::identifier))
2575 Diag(Tok, diag::err_expected_colon);
2576 else
2577 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002578 // We must manually skip to a ']', otherwise the expression skipper will
2579 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2580 // the enclosing expression.
2581 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002582 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002583 }
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002584
Chris Lattner699b6612008-01-25 18:59:06 +00002585 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002586
Steve Naroff29238a02007-10-05 18:42:47 +00002587 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002588 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002589 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002590 KeyLocs.push_back(Loc);
2591 }
Chris Lattnerff384912007-10-07 02:00:24 +00002592 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002593
Douglas Gregor2725ca82010-04-21 19:57:20 +00002594 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002595 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002596 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002597 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002598 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002599 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCall9ae2f072010-08-23 23:25:46 +00002600 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002601 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002602}
2603
John McCall60d7b3a2010-08-24 06:29:42 +00002604ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2605 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002606 if (Res.isInvalid()) return Res;
Sebastian Redl1d922962008-12-13 15:32:12 +00002607
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002608 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2609 // expressions. At this point, we know that the only valid thing that starts
2610 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002611 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002612 ExprVector AtStrings;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002613 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002614 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002615
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002616 while (Tok.is(tok::at)) {
2617 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002618
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002619 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002620 if (!isTokenStringLiteral())
2621 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002622
John McCall60d7b3a2010-08-24 06:29:42 +00002623 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002624 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002625 return Lit;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002626
Sebastian Redleffa8d12008-12-10 00:02:53 +00002627 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002628 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002629
Nico Weberd32b94d2012-12-31 00:28:03 +00002630 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2631 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00002632}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002633
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002634/// ParseObjCBooleanLiteral -
2635/// objc-scalar-literal : '@' boolean-keyword
2636/// ;
2637/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2638/// ;
2639ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2640 bool ArgValue) {
2641 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2642 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2643}
2644
2645/// ParseObjCCharacterLiteral -
2646/// objc-scalar-literal : '@' character-literal
2647/// ;
2648ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2649 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2650 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002651 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002652 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002653 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002654 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002655}
2656
2657/// ParseObjCNumericLiteral -
2658/// objc-scalar-literal : '@' scalar-literal
2659/// ;
2660/// scalar-literal : | numeric-constant /* any numeric constant. */
2661/// ;
2662ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2663 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2664 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002665 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002666 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002667 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002668 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002669}
2670
Patrick Beardeb382ec2012-04-19 00:25:12 +00002671/// ParseObjCBoxedExpr -
2672/// objc-box-expression:
2673/// @( assignment-expression )
2674ExprResult
2675Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2676 if (Tok.isNot(tok::l_paren))
2677 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2678
2679 BalancedDelimiterTracker T(*this, tok::l_paren);
2680 T.consumeOpen();
2681 ExprResult ValueExpr(ParseAssignmentExpression());
2682 if (T.consumeClose())
2683 return ExprError();
Argyrios Kyrtzidisedd27602012-05-10 20:02:36 +00002684
2685 if (ValueExpr.isInvalid())
2686 return ExprError();
2687
Patrick Beardeb382ec2012-04-19 00:25:12 +00002688 // Wrap the sub-expression in a parenthesized expression, to distinguish
2689 // a boxed expression from a literal.
2690 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2691 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Weberd32b94d2012-12-31 00:28:03 +00002692 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2693 ValueExpr.take());
Patrick Beardeb382ec2012-04-19 00:25:12 +00002694}
2695
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002696ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002697 ExprVector ElementExprs; // array elements.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002698 ConsumeBracket(); // consume the l_square.
2699
2700 while (Tok.isNot(tok::r_square)) {
2701 // Parse list of array element expressions (all must be id types).
2702 ExprResult Res(ParseAssignmentExpression());
2703 if (Res.isInvalid()) {
2704 // We must manually skip to a ']', otherwise the expression skipper will
2705 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2706 // the enclosing expression.
2707 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002708 return Res;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002709 }
2710
2711 // Parse the ellipsis that indicates a pack expansion.
2712 if (Tok.is(tok::ellipsis))
2713 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2714 if (Res.isInvalid())
2715 return true;
2716
2717 ElementExprs.push_back(Res.release());
2718
2719 if (Tok.is(tok::comma))
2720 ConsumeToken(); // Eat the ','.
2721 else if (Tok.isNot(tok::r_square))
2722 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2723 }
2724 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002725 MultiExprArg Args(ElementExprs);
Nico Weberd32b94d2012-12-31 00:28:03 +00002726 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002727}
2728
2729ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2730 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2731 ConsumeBrace(); // consume the l_square.
2732 while (Tok.isNot(tok::r_brace)) {
2733 // Parse the comma separated key : value expressions.
2734 ExprResult KeyExpr;
2735 {
2736 ColonProtectionRAIIObject X(*this);
2737 KeyExpr = ParseAssignmentExpression();
2738 if (KeyExpr.isInvalid()) {
2739 // We must manually skip to a '}', otherwise the expression skipper will
2740 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2741 // the enclosing expression.
2742 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002743 return KeyExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002744 }
2745 }
2746
2747 if (Tok.is(tok::colon)) {
2748 ConsumeToken();
2749 } else {
Fariborz Jahanian0ac0ee92013-04-18 19:37:43 +00002750 Diag(Tok, diag::err_expected_colon);
2751 SkipUntil(tok::r_brace);
2752 return ExprError();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002753 }
2754
2755 ExprResult ValueExpr(ParseAssignmentExpression());
2756 if (ValueExpr.isInvalid()) {
2757 // We must manually skip to a '}', otherwise the expression skipper will
2758 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2759 // the enclosing expression.
2760 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002761 return ValueExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002762 }
2763
2764 // Parse the ellipsis that designates this as a pack expansion.
2765 SourceLocation EllipsisLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00002766 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002767 EllipsisLoc = ConsumeToken();
2768
2769 // We have a valid expression. Collect it in a vector so we can
2770 // build the argument list.
2771 ObjCDictionaryElement Element = {
David Blaikie66874fb2013-02-21 01:47:18 +00002772 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002773 };
2774 Elements.push_back(Element);
2775
2776 if (Tok.is(tok::comma))
2777 ConsumeToken(); // Eat the ','.
2778 else if (Tok.isNot(tok::r_brace))
2779 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2780 }
2781 SourceLocation EndLoc = ConsumeBrace();
2782
2783 // Create the ObjCDictionaryLiteral.
Nico Weberd32b94d2012-12-31 00:28:03 +00002784 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2785 Elements.data(), Elements.size());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002786}
2787
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002788/// objc-encode-expression:
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002789/// \@encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002790ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002791Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002792 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002793
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002794 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002795
Chris Lattner4fef81d2008-08-05 06:19:09 +00002796 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002797 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2798
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002799 BalancedDelimiterTracker T(*this, tok::l_paren);
2800 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002801
Douglas Gregor809070a2009-02-18 17:45:20 +00002802 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002803
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002804 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002805
Douglas Gregor809070a2009-02-18 17:45:20 +00002806 if (Ty.isInvalid())
2807 return ExprError();
2808
Nico Weberd32b94d2012-12-31 00:28:03 +00002809 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2810 Ty.get(), T.getCloseLocation());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002811}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002812
2813/// objc-protocol-expression
James Dennett17d26a62012-06-11 06:19:40 +00002814/// \@protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002815ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002816Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002817 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002818
Chris Lattner4fef81d2008-08-05 06:19:09 +00002819 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002820 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2821
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002822 BalancedDelimiterTracker T(*this, tok::l_paren);
2823 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002824
Chris Lattner4fef81d2008-08-05 06:19:09 +00002825 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002826 return ExprError(Diag(Tok, diag::err_expected_ident));
2827
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002828 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002829 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002830
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002831 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002832
Nico Weberd32b94d2012-12-31 00:28:03 +00002833 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2834 T.getOpenLocation(), ProtoIdLoc,
2835 T.getCloseLocation());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002836}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002837
2838/// objc-selector-expression
2839/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002840ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002841 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002842
Chris Lattner4fef81d2008-08-05 06:19:09 +00002843 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002844 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2845
Chris Lattner5f9e2722011-07-23 10:55:15 +00002846 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002847 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002848
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002849 BalancedDelimiterTracker T(*this, tok::l_paren);
2850 T.consumeOpen();
2851
Douglas Gregor458433d2010-08-26 15:07:07 +00002852 if (Tok.is(tok::code_completion)) {
2853 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2854 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002855 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002856 return ExprError();
2857 }
2858
Chris Lattner2fc5c242009-04-11 18:13:45 +00002859 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002860 if (!SelIdent && // missing selector name.
2861 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002862 return ExprError(Diag(Tok, diag::err_expected_ident));
2863
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002864 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002865 unsigned nColons = 0;
2866 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002867 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002868 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2869 ++nColons;
2870 KeyIdents.push_back(0);
2871 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002872 return ExprError(Diag(Tok, diag::err_expected_colon));
2873
Chris Lattner5add7542010-08-27 22:32:41 +00002874 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002875 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002876 if (Tok.is(tok::r_paren))
2877 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002878
2879 if (Tok.is(tok::code_completion)) {
2880 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2881 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002882 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002883 return ExprError();
2884 }
2885
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002886 // Check for another keyword selector.
2887 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002888 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002889 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002890 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002891 break;
2892 }
Steve Naroff887407e2007-12-05 22:21:29 +00002893 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002894 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002895 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Weberd32b94d2012-12-31 00:28:03 +00002896 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2897 T.getOpenLocation(),
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002898 T.getCloseLocation());
Gabor Greif58065b22007-10-19 15:38:32 +00002899 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002900
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002901void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2902 // MCDecl might be null due to error in method or c-function prototype, etc.
2903 Decl *MCDecl = LM.D;
2904 bool skip = MCDecl &&
2905 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2906 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2907 if (skip)
2908 return;
2909
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002910 // Save the current token position.
2911 SourceLocation OrigLoc = Tok.getLocation();
2912
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002913 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2914 // Append the current token at the end of the new token stream so that it
2915 // doesn't get lost.
2916 LM.Toks.push_back(Tok);
2917 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2918
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002919 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00002920 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002921
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002922 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2923 Tok.is(tok::colon)) &&
2924 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002925 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002926 ParseScope BodyScope(this,
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002927 parseMethod
2928 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2929 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002930
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002931 // Tell the actions module that we have entered a method or c-function definition
2932 // with the specified Declarator for the method/function.
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +00002933 if (parseMethod)
2934 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2935 else
2936 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002937 if (Tok.is(tok::kw_try))
2938 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002939 else {
2940 if (Tok.is(tok::colon))
2941 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002942 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002943 }
Fariborz Jahanian69409722012-08-09 21:12:39 +00002944
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002945 if (Tok.getLocation() != OrigLoc) {
2946 // Due to parsing error, we either went over the cached tokens or
2947 // there are still cached tokens left. If it's the latter case skip the
2948 // leftover tokens.
2949 // Since this is an uncommon situation that should be avoided, use the
2950 // expensive isBeforeInTranslationUnit call.
2951 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2952 OrigLoc))
2953 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2954 ConsumeAnyToken();
2955 }
2956
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002957 return;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002958}