blob: 607b953aad4120ea2df564f90b587f105c3fd780 [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();
Fariborz Jahaniane4bb7492013-05-17 17:58:11 +00001544 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1545 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1546 AttributeFactory attr;
1547 DeclSpec DS(attr);
1548 (void)ParseObjCProtocolQualifiers(DS);
1549 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001550 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001551 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001552 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001553
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001554 } else {
1555 // We have a class implementation
1556 SourceLocation superClassLoc;
1557 IdentifierInfo *superClassId = 0;
1558 if (Tok.is(tok::colon)) {
1559 // We have a super class
1560 ConsumeToken();
1561 if (Tok.isNot(tok::identifier)) {
1562 Diag(Tok, diag::err_expected_ident); // missing super class name.
1563 return DeclGroupPtrTy();
1564 }
1565 superClassId = Tok.getIdentifierInfo();
1566 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001567 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001568 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1569 AtLoc, nameId, nameLoc,
1570 superClassId, superClassLoc);
1571
1572 if (Tok.is(tok::l_brace)) // we have ivars
1573 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian51c30af2013-04-24 23:23:47 +00001574 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1575 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1576 // try to recover.
1577 AttributeFactory attr;
1578 DeclSpec DS(attr);
1579 (void)ParseObjCProtocolQualifiers(DS);
1580 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001581 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001582 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001584 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001585
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001586 {
1587 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1588 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1589 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001590 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001591 MaybeParseMicrosoftAttributes(attrs);
1592 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1593 DeclGroupRef DG = DGP.get();
1594 DeclsInGroup.append(DG.begin(), DG.end());
1595 }
1596 }
1597 }
1598
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001599 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001600}
Steve Naroff60fccee2007-10-29 21:38:07 +00001601
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001602Parser::DeclGroupPtrTy
1603Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001604 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1605 "ParseObjCAtEndDeclaration(): Expected @end");
1606 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001607 if (CurParsedObjCImpl)
1608 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001609 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001610 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001611 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001612 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001613}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001614
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001615Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1616 if (!Finished) {
1617 finish(P.Tok.getLocation());
1618 if (P.Tok.is(tok::eof)) {
1619 P.Diag(P.Tok, diag::err_objc_missing_end)
1620 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1621 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1622 << Sema::OCK_Implementation;
1623 }
1624 }
1625 P.CurParsedObjCImpl = 0;
1626 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001627}
1628
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001629void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1630 assert(!Finished);
1631 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1632 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001633 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1634 true/*Methods*/);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001635
1636 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1637
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001638 if (HasCFunction)
1639 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1640 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1641 false/*c-functions*/);
1642
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001643 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001644 for (LateParsedObjCMethodContainer::iterator
1645 I = LateParsedObjCMethods.begin(),
1646 E = LateParsedObjCMethods.end(); I != E; ++I)
1647 delete *I;
1648 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001649
1650 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001651}
1652
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001653/// compatibility-alias-decl:
1654/// @compatibility_alias alias-name class-name ';'
1655///
John McCalld226f652010-08-21 09:40:31 +00001656Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001657 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1658 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1659 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001660 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001661 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001662 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001663 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001664 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1665 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001666 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001667 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001668 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001669 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001670 IdentifierInfo *classId = Tok.getIdentifierInfo();
1671 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001672 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1673 "@compatibility_alias");
Richard Smithde01b7a2012-08-08 23:32:13 +00001674 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1675 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001676}
1677
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001678/// property-synthesis:
1679/// @synthesize property-ivar-list ';'
1680///
1681/// property-ivar-list:
1682/// property-ivar
1683/// property-ivar-list ',' property-ivar
1684///
1685/// property-ivar:
1686/// identifier
1687/// identifier '=' identifier
1688///
John McCalld226f652010-08-21 09:40:31 +00001689Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001690 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniandf1fdfd2013-04-29 15:35:35 +00001691 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001692 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregorb328c422009-11-18 19:45:45 +00001694 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001695 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001696 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001697 cutOffParsing();
1698 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001699 }
1700
Douglas Gregorb328c422009-11-18 19:45:45 +00001701 if (Tok.isNot(tok::identifier)) {
1702 Diag(Tok, diag::err_synthesized_property_name);
1703 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001704 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001705 }
1706
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001707 IdentifierInfo *propertyIvar = 0;
1708 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1709 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001710 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001711 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001712 // property '=' ivar-name
1713 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001714
1715 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001716 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001717 cutOffParsing();
1718 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001719 }
1720
Chris Lattnerdf195262007-10-09 17:51:17 +00001721 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001722 Diag(Tok, diag::err_expected_ident);
1723 break;
1724 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001725 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001726 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001727 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001728 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001729 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001730 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001731 break;
1732 ConsumeToken(); // consume ','
1733 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001734 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001735 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001736}
1737
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001738/// property-dynamic:
1739/// @dynamic property-list
1740///
1741/// property-list:
1742/// identifier
1743/// property-list ',' identifier
1744///
John McCalld226f652010-08-21 09:40:31 +00001745Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001746 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1747 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001748 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001749 while (true) {
1750 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001751 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001752 cutOffParsing();
1753 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001754 }
1755
1756 if (Tok.isNot(tok::identifier)) {
1757 Diag(Tok, diag::err_expected_ident);
1758 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001759 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001760 }
1761
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001762 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1763 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001764 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001765 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001766
Chris Lattnerdf195262007-10-09 17:51:17 +00001767 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001768 break;
1769 ConsumeToken(); // consume ','
1770 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001771 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001772 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001773}
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001775/// objc-throw-statement:
1776/// throw expression[opt];
1777///
John McCall60d7b3a2010-08-24 06:29:42 +00001778StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1779 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001780 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001781 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001782 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001783 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001784 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001785 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001786 }
1787 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001788 // consume ';'
1789 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001790 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001791}
1792
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001793/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001794/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001795///
John McCall60d7b3a2010-08-24 06:29:42 +00001796StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001797Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001798 ConsumeToken(); // consume synchronized
1799 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001800 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001801 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001802 }
John McCall07524032011-07-27 21:50:02 +00001803
1804 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001805 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001806 ExprResult operand(ParseExpression());
1807
1808 if (Tok.is(tok::r_paren)) {
1809 ConsumeParen(); // ')'
1810 } else {
1811 if (!operand.isInvalid())
1812 Diag(Tok, diag::err_expected_rparen);
1813
1814 // Skip forward until we see a left brace, but don't consume it.
1815 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001816 }
John McCall07524032011-07-27 21:50:02 +00001817
1818 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001819 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001820 if (!operand.isInvalid())
1821 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001822 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001823 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001824
John McCall07524032011-07-27 21:50:02 +00001825 // Check the @synchronized operand now.
1826 if (!operand.isInvalid())
1827 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001828
John McCall07524032011-07-27 21:50:02 +00001829 // Parse the compound statement within a new scope.
1830 ParseScope bodyScope(this, Scope::DeclScope);
1831 StmtResult body(ParseCompoundStatementBody());
1832 bodyScope.Exit();
1833
1834 // If there was a semantic or parse error earlier with the
1835 // operand, fail now.
1836 if (operand.isInvalid())
1837 return StmtError();
1838
1839 if (body.isInvalid())
1840 body = Actions.ActOnNullStmt(Tok.getLocation());
1841
1842 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001843}
1844
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001845/// objc-try-catch-statement:
1846/// @try compound-statement objc-catch-list[opt]
1847/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1848///
1849/// objc-catch-list:
1850/// @catch ( parameter-declaration ) compound-statement
1851/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1852/// catch-parameter-declaration:
1853/// parameter-declaration
1854/// '...' [OBJC2]
1855///
John McCall60d7b3a2010-08-24 06:29:42 +00001856StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001857 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001858
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001859 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001860 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001861 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001862 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001863 }
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001864 StmtVector CatchStmts;
John McCall60d7b3a2010-08-24 06:29:42 +00001865 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001866 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001867 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001868 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001869 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001870 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001871
Chris Lattnerdf195262007-10-09 17:51:17 +00001872 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001873 // At this point, we need to lookahead to determine if this @ is the start
1874 // of an @catch or @finally. We don't want to consume the @ token if this
1875 // is an @try or @encode or something else.
1876 Token AfterAt = GetLookAheadToken(1);
1877 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1878 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1879 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001880
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001881 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001882 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001883 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001884 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001885 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001886 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001887 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001888 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001889 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001890 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001891 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001892 ParseDeclarator(ParmDecl);
1893
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001894 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001895 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001896 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001897 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001898 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Steve Naroff93a25952009-04-07 22:56:58 +00001900 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Steve Naroff93a25952009-04-07 22:56:58 +00001902 if (Tok.is(tok::r_paren))
1903 RParenLoc = ConsumeParen();
1904 else // Skip over garbage, until we get to ')'. Eat the ')'.
1905 SkipUntil(tok::r_paren, true, false);
1906
John McCall60d7b3a2010-08-24 06:29:42 +00001907 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001908 if (Tok.is(tok::l_brace))
1909 CatchBody = ParseCompoundStatementBody();
1910 else
1911 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001912 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001913 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001914
John McCall60d7b3a2010-08-24 06:29:42 +00001915 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001916 RParenLoc,
1917 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001918 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001919 if (!Catch.isInvalid())
1920 CatchStmts.push_back(Catch.release());
1921
Steve Naroff64515f32008-02-05 21:27:35 +00001922 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001923 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1924 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001925 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001926 }
1927 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001928 } else {
1929 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001930 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001931 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001932
John McCall60d7b3a2010-08-24 06:29:42 +00001933 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001934 if (Tok.is(tok::l_brace))
1935 FinallyBody = ParseCompoundStatementBody();
1936 else
1937 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001938 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001939 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001940 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001941 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001942 catch_or_finally_seen = true;
1943 break;
1944 }
1945 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001946 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001947 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001948 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001949 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001950
John McCall9ae2f072010-08-23 23:25:46 +00001951 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001952 CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001953 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001954}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001955
John McCallf85e1932011-06-15 23:02:42 +00001956/// objc-autoreleasepool-statement:
1957/// @autoreleasepool compound-statement
1958///
1959StmtResult
1960Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1961 ConsumeToken(); // consume autoreleasepool
1962 if (Tok.isNot(tok::l_brace)) {
1963 Diag(Tok, diag::err_expected_lbrace);
1964 return StmtError();
1965 }
1966 // Enter a scope to hold everything within the compound stmt. Compound
1967 // statements can always hold declarations.
1968 ParseScope BodyScope(this, Scope::DeclScope);
1969
1970 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1971
1972 BodyScope.Exit();
1973 if (AutoreleasePoolBody.isInvalid())
1974 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1975 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1976 AutoreleasePoolBody.take());
1977}
1978
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001979/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1980/// for later parsing.
1981void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1982 LexedMethod* LM = new LexedMethod(this, MDecl);
1983 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1984 CachedTokens &Toks = LM->Toks;
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001985 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001986 Toks.push_back(Tok);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001987 if (Tok.is(tok::kw_try)) {
1988 ConsumeToken();
Fariborz Jahaniandbd69452012-08-10 20:34:17 +00001989 if (Tok.is(tok::colon)) {
1990 Toks.push_back(Tok);
1991 ConsumeToken();
1992 while (Tok.isNot(tok::l_brace)) {
1993 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1994 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1995 }
1996 }
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001997 Toks.push_back(Tok); // also store '{'
1998 }
1999 else if (Tok.is(tok::colon)) {
2000 ConsumeToken();
2001 while (Tok.isNot(tok::l_brace)) {
2002 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2003 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2004 }
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002005 Toks.push_back(Tok); // also store '{'
2006 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002007 ConsumeBrace();
2008 // Consume everything up to (and including) the matching right brace.
2009 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002010 while (Tok.is(tok::kw_catch)) {
2011 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2012 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2013 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002014}
2015
Steve Naroff3536b442007-09-06 21:24:23 +00002016/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002017///
John McCalld226f652010-08-21 09:40:31 +00002018Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002019 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00002020
John McCallf312b1e2010-08-26 23:41:50 +00002021 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2022 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002024 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00002025 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002026 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00002027 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00002028 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00002029 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002030 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00002031 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002032
Steve Naroff409be832007-11-11 19:54:21 +00002033 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00002034 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002035 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Steve Naroff409be832007-11-11 19:54:21 +00002037 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2038 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Steve Naroff409be832007-11-11 19:54:21 +00002040 // If we didn't find the '{', bail out.
2041 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00002042 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002043 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002044
2045 if (!MDecl) {
2046 ConsumeBrace();
2047 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2048 return 0;
2049 }
2050
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002051 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002052 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahanianc9b97092012-08-09 17:15:00 +00002053 assert (CurParsedObjCImpl
2054 && "ParseObjCMethodDefinition - Method out of @implementation");
2055 // Consume the tokens and store them for later parsing.
2056 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff71c0a952007-11-13 23:01:27 +00002057 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00002058}
Anders Carlsson55085182007-08-21 17:43:55 +00002059
John McCall60d7b3a2010-08-24 06:29:42 +00002060StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002061 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002062 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002063 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002064 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00002065 }
2066
2067 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00002068 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002069
2070 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00002071 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002072
2073 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00002074 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00002075
2076 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2077 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002078
John McCall60d7b3a2010-08-24 06:29:42 +00002079 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002080 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00002081 // If the expression is invalid, skip ahead to the next semicolon. Not
2082 // doing this opens us up to the possibility of infinite loops if
2083 // ParseExpression does not consume any tokens.
2084 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00002085 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00002086 }
Chris Lattner5d803162009-12-07 16:33:19 +00002087
Steve Naroff64515f32008-02-05 21:27:35 +00002088 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00002089 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +00002090 return Actions.ActOnExprStmt(Res);
Steve Naroff64515f32008-02-05 21:27:35 +00002091}
2092
John McCall60d7b3a2010-08-24 06:29:42 +00002093ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002094 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002095 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002096 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002097 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002098 return ExprError();
2099
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002100 case tok::minus:
2101 case tok::plus: {
2102 tok::TokenKind Kind = Tok.getKind();
2103 SourceLocation OpLoc = ConsumeToken();
2104
2105 if (!Tok.is(tok::numeric_constant)) {
2106 const char *Symbol = 0;
2107 switch (Kind) {
2108 case tok::minus: Symbol = "-"; break;
2109 case tok::plus: Symbol = "+"; break;
2110 default: llvm_unreachable("missing unary operator case");
2111 }
2112 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2113 << Symbol;
2114 return ExprError();
2115 }
2116
2117 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2118 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002119 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002120 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002121 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002122
2123 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2124 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002125 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002126
2127 return ParsePostfixExpressionSuffix(
2128 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2129 }
2130
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002131 case tok::string_literal: // primary-expression: string-literal
2132 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002133 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002134
2135 case tok::char_constant:
2136 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2137
2138 case tok::numeric_constant:
2139 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2140
2141 case tok::kw_true: // Objective-C++, etc.
2142 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2143 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2144 case tok::kw_false: // Objective-C++, etc.
2145 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2146 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2147
2148 case tok::l_square:
2149 // Objective-C array literal
2150 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2151
2152 case tok::l_brace:
2153 // Objective-C dictionary literal
2154 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2155
Patrick Beardeb382ec2012-04-19 00:25:12 +00002156 case tok::l_paren:
2157 // Objective-C boxed expression
2158 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2159
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002160 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002161 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002162 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002163
Chris Lattner4fef81d2008-08-05 06:19:09 +00002164 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2165 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002166 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002167 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002168 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002169 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002170 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian6749ae12012-07-09 20:00:35 +00002171 default: {
2172 const char *str = 0;
2173 if (GetLookAheadToken(1).is(tok::l_brace)) {
2174 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2175 str =
2176 ch == 't' ? "try"
2177 : (ch == 'f' ? "finally"
2178 : (ch == 'a' ? "autoreleasepool" : 0));
2179 }
2180 if (str) {
2181 SourceLocation kwLoc = Tok.getLocation();
2182 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2183 FixItHint::CreateReplacement(kwLoc, str));
2184 }
2185 else
2186 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2187 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002188 }
Anders Carlsson55085182007-08-21 17:43:55 +00002189 }
Anders Carlsson55085182007-08-21 17:43:55 +00002190}
2191
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002192/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002193///
2194/// This routine parses the receiver of a message send in
2195/// Objective-C++ either as a type or as an expression. Note that this
2196/// routine must not be called to parse a send to 'super', since it
2197/// has no way to return such a result.
2198///
2199/// \param IsExpr Whether the receiver was parsed as an expression.
2200///
2201/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2202/// IsExpr is true), the parsed expression. If the receiver was parsed
2203/// as a type (\c IsExpr is false), the parsed type.
2204///
2205/// \returns True if an error occurred during parsing or semantic
2206/// analysis, in which case the arguments do not have valid
2207/// values. Otherwise, returns false for a successful parse.
2208///
2209/// objc-receiver: [C++]
2210/// 'super' [not parsed here]
2211/// expression
2212/// simple-type-specifier
2213/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002214bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002215 InMessageExpressionRAIIObject InMessage(*this, true);
2216
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002217 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2218 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2219 TryAnnotateTypeOrScopeToken();
2220
Kaelyn Uhrain7bf33402012-06-15 23:45:51 +00002221 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002222 // objc-receiver:
2223 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002224 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002225 if (Receiver.isInvalid())
2226 return true;
2227
2228 IsExpr = true;
2229 TypeOrExpr = Receiver.take();
2230 return false;
2231 }
2232
2233 // objc-receiver:
2234 // typename-specifier
2235 // simple-type-specifier
2236 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002237 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002238 ParseCXXSimpleTypeSpecifier(DS);
2239
2240 if (Tok.is(tok::l_paren)) {
2241 // If we see an opening parentheses at this point, we are
2242 // actually parsing an expression that starts with a
2243 // function-style cast, e.g.,
2244 //
2245 // postfix-expression:
2246 // simple-type-specifier ( expression-list [opt] )
2247 // typename-specifier ( expression-list [opt] )
2248 //
2249 // Parse the remainder of this case, then the (optional)
2250 // postfix-expression suffix, followed by the (optional)
2251 // right-hand side of the binary expression. We have an
2252 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002253 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002254 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002255 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002256 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002257 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002258 if (Receiver.isInvalid())
2259 return true;
2260
2261 IsExpr = true;
2262 TypeOrExpr = Receiver.take();
2263 return false;
2264 }
2265
2266 // We have a class message. Turn the simple-type-specifier or
2267 // typename-specifier we parsed into a type and parse the
2268 // remainder of the class message.
2269 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002270 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002271 if (Type.isInvalid())
2272 return true;
2273
2274 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002275 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002276 return false;
2277}
2278
Douglas Gregor1b730e82010-05-31 14:40:22 +00002279/// \brief Determine whether the parser is currently referring to a an
2280/// Objective-C message send, using a simplified heuristic to avoid overhead.
2281///
2282/// This routine will only return true for a subset of valid message-send
2283/// expressions.
2284bool Parser::isSimpleObjCMessageExpression() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002285 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002286 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002287 return GetLookAheadToken(1).is(tok::identifier) &&
2288 GetLookAheadToken(2).is(tok::identifier);
2289}
2290
Douglas Gregor9497a732010-09-16 01:51:54 +00002291bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002292 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregor9497a732010-09-16 01:51:54 +00002293 InMessageExpression)
2294 return false;
2295
2296
2297 ParsedType Type;
2298
2299 if (Tok.is(tok::annot_typename))
2300 Type = getTypeAnnotation(Tok);
2301 else if (Tok.is(tok::identifier))
2302 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2303 getCurScope());
2304 else
2305 return false;
2306
2307 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2308 const Token &AfterNext = GetLookAheadToken(2);
2309 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2310 if (Tok.is(tok::identifier))
2311 TryAnnotateTypeOrScopeToken();
2312
2313 return Tok.is(tok::annot_typename);
2314 }
2315 }
2316
2317 return false;
2318}
2319
Mike Stump1eb44332009-09-09 15:08:12 +00002320/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002321/// '[' objc-receiver objc-message-args ']'
2322///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002323/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002324/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002325/// expression
2326/// class-name
2327/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002328///
John McCall60d7b3a2010-08-24 06:29:42 +00002329ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002330 assert(Tok.is(tok::l_square) && "'[' expected");
2331 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2332
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002333 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002334 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002335 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002336 return ExprError();
2337 }
2338
Douglas Gregor0fbda682010-09-15 14:51:05 +00002339 InMessageExpressionRAIIObject InMessage(*this, true);
2340
David Blaikie4e4d0842012-03-11 07:00:24 +00002341 if (getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002342 // We completely separate the C and C++ cases because C++ requires
2343 // more complicated (read: slower) parsing.
2344
2345 // Handle send to super.
2346 // FIXME: This doesn't benefit from the same typo-correction we
2347 // get in Objective-C.
2348 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002349 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002350 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2351 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002352
2353 // Parse the receiver, which is either a type or an expression.
2354 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002355 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002356 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2357 SkipUntil(tok::r_square);
2358 return ExprError();
2359 }
2360
2361 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002362 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2363 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002364 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002365
2366 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002367 ParsedType::getFromOpaquePtr(TypeOrExpr),
2368 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002369 }
2370
2371 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002372 IdentifierInfo *Name = Tok.getIdentifierInfo();
2373 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002374 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002375 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002376 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002377 NextToken().is(tok::period),
2378 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002379 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002380 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2381 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002382
John McCallf312b1e2010-08-26 23:41:50 +00002383 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002384 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002385 SkipUntil(tok::r_square);
2386 return ExprError();
2387 }
2388
Douglas Gregor1569f952010-04-21 20:38:13 +00002389 ConsumeToken(); // the type name
2390
2391 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002392 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002393
John McCallf312b1e2010-08-26 23:41:50 +00002394 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002395 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002396 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002397 }
Chris Lattner699b6612008-01-25 18:59:06 +00002398 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002399
2400 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002401 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002402 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002403 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002404 return Res;
Chris Lattner699b6612008-01-25 18:59:06 +00002405 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002406
John McCallb3d87482010-08-24 05:47:05 +00002407 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2408 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002409}
Sebastian Redl1d922962008-12-13 15:32:12 +00002410
Douglas Gregor2725ca82010-04-21 19:57:20 +00002411/// \brief Parse the remainder of an Objective-C message following the
2412/// '[' objc-receiver.
2413///
2414/// This routine handles sends to super, class messages (sent to a
2415/// class name), and instance messages (sent to an object), and the
2416/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2417/// ReceiverExpr, respectively. Only one of these parameters may have
2418/// a valid value.
2419///
2420/// \param LBracLoc The location of the opening '['.
2421///
2422/// \param SuperLoc If this is a send to 'super', the location of the
2423/// 'super' keyword that indicates a send to the superclass.
2424///
2425/// \param ReceiverType If this is a class message, the type of the
2426/// class we are sending a message to.
2427///
2428/// \param ReceiverExpr If this is an instance message, the expression
2429/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002430///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002431/// objc-message-args:
2432/// objc-selector
2433/// objc-keywordarg-list
2434///
2435/// objc-keywordarg-list:
2436/// objc-keywordarg
2437/// objc-keywordarg-list objc-keywordarg
2438///
Mike Stump1eb44332009-09-09 15:08:12 +00002439/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002440/// selector-name[opt] ':' objc-keywordexpr
2441///
2442/// objc-keywordexpr:
2443/// nonempty-expr-list
2444///
2445/// nonempty-expr-list:
2446/// assignment-expression
2447/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002448///
John McCall60d7b3a2010-08-24 06:29:42 +00002449ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002450Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002451 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002452 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002453 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002454 InMessageExpressionRAIIObject InMessage(*this, true);
2455
Steve Naroffc4df6d22009-11-07 02:08:14 +00002456 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002457 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002458 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2459 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002460 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002461 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2462 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002463 else
John McCall9ae2f072010-08-23 23:25:46 +00002464 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002465 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002466 cutOffParsing();
2467 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002468 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002469
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002470 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002471 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002472 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002473
Chris Lattner5f9e2722011-07-23 10:55:15 +00002474 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002475 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002476 ExprVector KeyExprs;
Steve Naroff68d331a2007-09-27 14:38:14 +00002477
Chris Lattnerdf195262007-10-09 17:51:17 +00002478 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002479 while (1) {
2480 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002481 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002482 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002483
Chris Lattnerdf195262007-10-09 17:51:17 +00002484 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002485 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002486 // We must manually skip to a ']', otherwise the expression skipper will
2487 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2488 // the enclosing expression.
2489 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002490 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002491 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002492
Steve Naroff68d331a2007-09-27 14:38:14 +00002493 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002494 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002495
2496 if (Tok.is(tok::code_completion)) {
2497 if (SuperLoc.isValid())
2498 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2499 KeyIdents.data(),
2500 KeyIdents.size(),
2501 /*AtArgumentEpression=*/true);
2502 else if (ReceiverType)
2503 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2504 KeyIdents.data(),
2505 KeyIdents.size(),
2506 /*AtArgumentEpression=*/true);
2507 else
2508 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2509 KeyIdents.data(),
2510 KeyIdents.size(),
2511 /*AtArgumentEpression=*/true);
2512
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002513 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002514 return ExprError();
2515 }
2516
Fariborz Jahanian0c7102f2013-04-18 23:43:21 +00002517 ExprResult Expr;
2518 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2519 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2520 Expr = ParseBraceInitializer();
2521 } else
2522 Expr = ParseAssignmentExpression();
2523
2524 ExprResult Res(Expr);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002525 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002526 // We must manually skip to a ']', otherwise the expression skipper will
2527 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2528 // the enclosing expression.
2529 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002530 return Res;
Steve Naroff37387c92007-09-17 20:25:27 +00002531 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002532
Steve Naroff37387c92007-09-17 20:25:27 +00002533 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002534 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002535
Douglas Gregord3c68542009-11-19 01:08:35 +00002536 // Code completion after each argument.
2537 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002538 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002539 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002540 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002541 KeyIdents.size(),
2542 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002543 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002544 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002545 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002546 KeyIdents.size(),
2547 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002548 else
John McCall9ae2f072010-08-23 23:25:46 +00002549 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002550 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002551 KeyIdents.size(),
2552 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002553 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002554 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002555 }
2556
Steve Naroff37387c92007-09-17 20:25:27 +00002557 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002558 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002559 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002560 break;
2561 // We have a selector or a colon, continue parsing.
2562 }
2563 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002564 while (Tok.is(tok::comma)) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002565 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002566 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002567 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002568 if (Res.isInvalid()) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002569 if (Tok.is(tok::colon)) {
2570 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2571 FixItHint::CreateRemoval(commaLoc);
2572 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002573 // We must manually skip to a ']', otherwise the expression skipper will
2574 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2575 // the enclosing expression.
2576 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002577 return Res;
Steve Naroff49f109c2007-11-15 13:05:42 +00002578 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002579
Steve Naroff49f109c2007-11-15 13:05:42 +00002580 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002581 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002582 }
2583 } else if (!selIdent) {
2584 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002585
Chris Lattner4fef81d2008-08-05 06:19:09 +00002586 // We must manually skip to a ']', otherwise the expression skipper will
2587 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2588 // the enclosing expression.
2589 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002590 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002591 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002592
Chris Lattnerdf195262007-10-09 17:51:17 +00002593 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002594 if (Tok.is(tok::identifier))
2595 Diag(Tok, diag::err_expected_colon);
2596 else
2597 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002598 // We must manually skip to a ']', otherwise the expression skipper will
2599 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2600 // the enclosing expression.
2601 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002602 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002603 }
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002604
Chris Lattner699b6612008-01-25 18:59:06 +00002605 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002606
Steve Naroff29238a02007-10-05 18:42:47 +00002607 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002608 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002609 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002610 KeyLocs.push_back(Loc);
2611 }
Chris Lattnerff384912007-10-07 02:00:24 +00002612 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002613
Douglas Gregor2725ca82010-04-21 19:57:20 +00002614 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002615 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002616 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002617 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002618 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002619 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCall9ae2f072010-08-23 23:25:46 +00002620 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002621 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002622}
2623
John McCall60d7b3a2010-08-24 06:29:42 +00002624ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2625 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002626 if (Res.isInvalid()) return Res;
Sebastian Redl1d922962008-12-13 15:32:12 +00002627
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002628 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2629 // expressions. At this point, we know that the only valid thing that starts
2630 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002631 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002632 ExprVector AtStrings;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002633 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002634 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002635
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002636 while (Tok.is(tok::at)) {
2637 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002638
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002639 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002640 if (!isTokenStringLiteral())
2641 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002642
John McCall60d7b3a2010-08-24 06:29:42 +00002643 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002644 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002645 return Lit;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002646
Sebastian Redleffa8d12008-12-10 00:02:53 +00002647 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002648 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002649
Nico Weberd32b94d2012-12-31 00:28:03 +00002650 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2651 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00002652}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002653
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002654/// ParseObjCBooleanLiteral -
2655/// objc-scalar-literal : '@' boolean-keyword
2656/// ;
2657/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2658/// ;
2659ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2660 bool ArgValue) {
2661 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2662 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2663}
2664
2665/// ParseObjCCharacterLiteral -
2666/// objc-scalar-literal : '@' character-literal
2667/// ;
2668ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2669 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2670 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002671 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002672 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002673 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002674 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002675}
2676
2677/// ParseObjCNumericLiteral -
2678/// objc-scalar-literal : '@' scalar-literal
2679/// ;
2680/// scalar-literal : | numeric-constant /* any numeric constant. */
2681/// ;
2682ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2683 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2684 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002685 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002686 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002687 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002688 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002689}
2690
Patrick Beardeb382ec2012-04-19 00:25:12 +00002691/// ParseObjCBoxedExpr -
2692/// objc-box-expression:
2693/// @( assignment-expression )
2694ExprResult
2695Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2696 if (Tok.isNot(tok::l_paren))
2697 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2698
2699 BalancedDelimiterTracker T(*this, tok::l_paren);
2700 T.consumeOpen();
2701 ExprResult ValueExpr(ParseAssignmentExpression());
2702 if (T.consumeClose())
2703 return ExprError();
Argyrios Kyrtzidisedd27602012-05-10 20:02:36 +00002704
2705 if (ValueExpr.isInvalid())
2706 return ExprError();
2707
Patrick Beardeb382ec2012-04-19 00:25:12 +00002708 // Wrap the sub-expression in a parenthesized expression, to distinguish
2709 // a boxed expression from a literal.
2710 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2711 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Weberd32b94d2012-12-31 00:28:03 +00002712 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2713 ValueExpr.take());
Patrick Beardeb382ec2012-04-19 00:25:12 +00002714}
2715
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002716ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002717 ExprVector ElementExprs; // array elements.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002718 ConsumeBracket(); // consume the l_square.
2719
2720 while (Tok.isNot(tok::r_square)) {
2721 // Parse list of array element expressions (all must be id types).
2722 ExprResult Res(ParseAssignmentExpression());
2723 if (Res.isInvalid()) {
2724 // We must manually skip to a ']', otherwise the expression skipper will
2725 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2726 // the enclosing expression.
2727 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002728 return Res;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002729 }
2730
2731 // Parse the ellipsis that indicates a pack expansion.
2732 if (Tok.is(tok::ellipsis))
2733 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2734 if (Res.isInvalid())
2735 return true;
2736
2737 ElementExprs.push_back(Res.release());
2738
2739 if (Tok.is(tok::comma))
2740 ConsumeToken(); // Eat the ','.
2741 else if (Tok.isNot(tok::r_square))
2742 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2743 }
2744 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002745 MultiExprArg Args(ElementExprs);
Nico Weberd32b94d2012-12-31 00:28:03 +00002746 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002747}
2748
2749ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2750 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2751 ConsumeBrace(); // consume the l_square.
2752 while (Tok.isNot(tok::r_brace)) {
2753 // Parse the comma separated key : value expressions.
2754 ExprResult KeyExpr;
2755 {
2756 ColonProtectionRAIIObject X(*this);
2757 KeyExpr = ParseAssignmentExpression();
2758 if (KeyExpr.isInvalid()) {
2759 // We must manually skip to a '}', otherwise the expression skipper will
2760 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2761 // the enclosing expression.
2762 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002763 return KeyExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002764 }
2765 }
2766
2767 if (Tok.is(tok::colon)) {
2768 ConsumeToken();
2769 } else {
Fariborz Jahanian0ac0ee92013-04-18 19:37:43 +00002770 Diag(Tok, diag::err_expected_colon);
2771 SkipUntil(tok::r_brace);
2772 return ExprError();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002773 }
2774
2775 ExprResult ValueExpr(ParseAssignmentExpression());
2776 if (ValueExpr.isInvalid()) {
2777 // We must manually skip to a '}', otherwise the expression skipper will
2778 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2779 // the enclosing expression.
2780 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002781 return ValueExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002782 }
2783
2784 // Parse the ellipsis that designates this as a pack expansion.
2785 SourceLocation EllipsisLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00002786 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002787 EllipsisLoc = ConsumeToken();
2788
2789 // We have a valid expression. Collect it in a vector so we can
2790 // build the argument list.
2791 ObjCDictionaryElement Element = {
David Blaikie66874fb2013-02-21 01:47:18 +00002792 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002793 };
2794 Elements.push_back(Element);
2795
2796 if (Tok.is(tok::comma))
2797 ConsumeToken(); // Eat the ','.
2798 else if (Tok.isNot(tok::r_brace))
2799 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2800 }
2801 SourceLocation EndLoc = ConsumeBrace();
2802
2803 // Create the ObjCDictionaryLiteral.
Nico Weberd32b94d2012-12-31 00:28:03 +00002804 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2805 Elements.data(), Elements.size());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002806}
2807
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002808/// objc-encode-expression:
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002809/// \@encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002810ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002811Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002812 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002813
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002814 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002815
Chris Lattner4fef81d2008-08-05 06:19:09 +00002816 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002817 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2818
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002819 BalancedDelimiterTracker T(*this, tok::l_paren);
2820 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002821
Douglas Gregor809070a2009-02-18 17:45:20 +00002822 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002823
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002824 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002825
Douglas Gregor809070a2009-02-18 17:45:20 +00002826 if (Ty.isInvalid())
2827 return ExprError();
2828
Nico Weberd32b94d2012-12-31 00:28:03 +00002829 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2830 Ty.get(), T.getCloseLocation());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002831}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002832
2833/// objc-protocol-expression
James Dennett17d26a62012-06-11 06:19:40 +00002834/// \@protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002835ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002836Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002837 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002838
Chris Lattner4fef81d2008-08-05 06:19:09 +00002839 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002840 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2841
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002842 BalancedDelimiterTracker T(*this, tok::l_paren);
2843 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002844
Chris Lattner4fef81d2008-08-05 06:19:09 +00002845 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002846 return ExprError(Diag(Tok, diag::err_expected_ident));
2847
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002848 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002849 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002850
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002851 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002852
Nico Weberd32b94d2012-12-31 00:28:03 +00002853 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2854 T.getOpenLocation(), ProtoIdLoc,
2855 T.getCloseLocation());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002856}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002857
2858/// objc-selector-expression
2859/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002860ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002861 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002862
Chris Lattner4fef81d2008-08-05 06:19:09 +00002863 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002864 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2865
Chris Lattner5f9e2722011-07-23 10:55:15 +00002866 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002867 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002868
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002869 BalancedDelimiterTracker T(*this, tok::l_paren);
2870 T.consumeOpen();
2871
Douglas Gregor458433d2010-08-26 15:07:07 +00002872 if (Tok.is(tok::code_completion)) {
2873 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2874 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002875 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002876 return ExprError();
2877 }
2878
Chris Lattner2fc5c242009-04-11 18:13:45 +00002879 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002880 if (!SelIdent && // missing selector name.
2881 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002882 return ExprError(Diag(Tok, diag::err_expected_ident));
2883
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002884 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002885 unsigned nColons = 0;
2886 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002887 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002888 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2889 ++nColons;
2890 KeyIdents.push_back(0);
2891 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002892 return ExprError(Diag(Tok, diag::err_expected_colon));
2893
Chris Lattner5add7542010-08-27 22:32:41 +00002894 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002895 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002896 if (Tok.is(tok::r_paren))
2897 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002898
2899 if (Tok.is(tok::code_completion)) {
2900 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2901 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002902 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002903 return ExprError();
2904 }
2905
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002906 // Check for another keyword selector.
2907 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002908 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002909 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002910 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002911 break;
2912 }
Steve Naroff887407e2007-12-05 22:21:29 +00002913 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002914 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002915 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Weberd32b94d2012-12-31 00:28:03 +00002916 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2917 T.getOpenLocation(),
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002918 T.getCloseLocation());
Gabor Greif58065b22007-10-19 15:38:32 +00002919 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002920
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002921void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2922 // MCDecl might be null due to error in method or c-function prototype, etc.
2923 Decl *MCDecl = LM.D;
2924 bool skip = MCDecl &&
2925 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2926 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2927 if (skip)
2928 return;
2929
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002930 // Save the current token position.
2931 SourceLocation OrigLoc = Tok.getLocation();
2932
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002933 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2934 // Append the current token at the end of the new token stream so that it
2935 // doesn't get lost.
2936 LM.Toks.push_back(Tok);
2937 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2938
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002939 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00002940 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002941
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002942 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2943 Tok.is(tok::colon)) &&
2944 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002945 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002946 ParseScope BodyScope(this,
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002947 parseMethod
2948 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2949 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002950
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002951 // Tell the actions module that we have entered a method or c-function definition
2952 // with the specified Declarator for the method/function.
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +00002953 if (parseMethod)
2954 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2955 else
2956 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002957 if (Tok.is(tok::kw_try))
2958 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002959 else {
2960 if (Tok.is(tok::colon))
2961 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002962 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002963 }
Fariborz Jahanian69409722012-08-09 21:12:39 +00002964
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002965 if (Tok.getLocation() != OrigLoc) {
2966 // Due to parsing error, we either went over the cached tokens or
2967 // there are still cached tokens left. If it's the latter case skip the
2968 // leftover tokens.
2969 // Since this is an uncommon situation that should be avoided, use the
2970 // expensive isBeforeInTranslationUnit call.
2971 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2972 OrigLoc))
2973 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2974 ConsumeAnyToken();
2975 }
2976
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002977 return;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002978}