blob: e4793dbd41b28326b5bfb3b5a4138edb312ce9b1 [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
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000351 SetterSel =
352 SelectorTable::constructSetterSelector(P.PP.getIdentifierTable(),
353 P.PP.getSelectorTable(),
354 FD.D.getIdentifier());
John McCalld0014542009-12-03 22:31:13 +0000355 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000356 Decl *Property =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000357 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
358 FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000359 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000360 &isOverridingProperty,
361 MethodImplKind);
362 if (!isOverridingProperty)
363 Props.push_back(Property);
364
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000365 FD.complete(Property);
John McCalld0014542009-12-03 22:31:13 +0000366 }
367};
368
David Blaikie99ba9e32011-12-20 02:48:34 +0000369void Parser::ObjCPropertyCallback::anchor() {
370}
371
Steve Naroffdac269b2007-08-20 21:31:48 +0000372/// objc-interface-decl-list:
373/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000374/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000375/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000376/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000377/// objc-interface-decl-list declaration
378/// objc-interface-decl-list ';'
379///
Steve Naroff294494e2007-08-22 16:35:03 +0000380/// objc-method-requirement: [OBJC2]
381/// @required
382/// @optional
383///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000384void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
385 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000386 SmallVector<Decl *, 32> allMethods;
387 SmallVector<Decl *, 16> allProperties;
388 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000389 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek782f2f52010-01-07 01:20:12 +0000391 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000392
Steve Naroff294494e2007-08-22 16:35:03 +0000393 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000394 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000395 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniand30ec702012-07-26 17:32:28 +0000396 if (Decl *methodPrototype =
397 ParseObjCMethodPrototype(MethodImplKind, false))
398 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000399 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
400 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000401 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
402 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
403 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
404 if (Tok.is(tok::semi))
405 ConsumeToken();
406 }
Steve Naroff294494e2007-08-22 16:35:03 +0000407 continue;
408 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000409 if (Tok.is(tok::l_paren)) {
410 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000411 ParseObjCMethodDecl(Tok.getLocation(),
412 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000413 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000414 continue;
415 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000416 // Ignore excess semicolons.
417 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000418 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000419 continue;
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattnerbc662af2008-10-20 06:10:06 +0000422 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000423 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000424 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000426 // Code completion within an Objective-C interface.
427 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000428 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000429 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallf312b1e2010-08-26 23:41:50 +0000430 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000431 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000432 }
433
Chris Lattnere82a10f2008-10-20 05:46:22 +0000434 // If we don't have an @ directive, parse it as a function definition.
435 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000436 // The code below does not consume '}'s because it is afraid of eating the
437 // end of a namespace. Because of the way this code is structured, an
438 // erroneous r_brace would cause an infinite loop if not handled here.
439 if (Tok.is(tok::r_brace))
440 break;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000441 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000442 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000443 continue;
444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattnere82a10f2008-10-20 05:46:22 +0000446 // Otherwise, we have an @ directive, eat the @.
447 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000448 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000449 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000450 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000451 }
452
Chris Lattnera2449b22008-10-20 05:57:40 +0000453 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattnera2449b22008-10-20 05:57:40 +0000455 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000456 AtEnd.setBegin(AtLoc);
457 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000458 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000459 } else if (DirectiveKind == tok::objc_not_keyword) {
460 Diag(Tok, diag::err_objc_unknown_at);
461 SkipUntil(tok::semi);
462 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattnerbc662af2008-10-20 06:10:06 +0000465 // Eat the identifier.
466 ConsumeToken();
467
Chris Lattnera2449b22008-10-20 05:57:40 +0000468 switch (DirectiveKind) {
469 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000470 // FIXME: If someone forgets an @end on a protocol, this loop will
471 // continue to eat up tons of stuff and spew lots of nonsense errors. It
472 // would probably be better to bail out if we saw an @class or @interface
473 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000474 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000475 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000476 SkipUntil(tok::r_brace, tok::at);
477 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000478
479 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000480 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000481 Diag(AtLoc, diag::err_objc_missing_end)
482 << FixItHint::CreateInsertion(AtLoc, "@end\n");
483 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
484 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000485 ConsumeToken();
486 break;
487
Chris Lattnera2449b22008-10-20 05:57:40 +0000488 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000489 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000490 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000491 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000492 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000493 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000494 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000495 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000496 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattnera2449b22008-10-20 05:57:40 +0000498 case tok::objc_property:
David Blaikie4e4d0842012-03-11 07:00:24 +0000499 if (!getLangOpts().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000500 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000501
Chris Lattnere82a10f2008-10-20 05:46:22 +0000502 ObjCDeclSpec OCDS;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000503 SourceLocation LParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000504 // Parse property attribute list, if any.
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000505 if (Tok.is(tok::l_paren)) {
506 LParenLoc = Tok.getLocation();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000507 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000510 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000511 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000512
Chris Lattnere82a10f2008-10-20 05:46:22 +0000513 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000514 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +0000515 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
John McCall7da19ea2011-03-26 01:53:26 +0000517 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000518 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000519 }
Steve Naroff294494e2007-08-22 16:35:03 +0000520 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000521
522 // We break out of the big loop in two cases: when we see @end or when we see
523 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000524 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000525 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000526 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000527 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000528 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000529 } else {
530 Diag(Tok, diag::err_objc_missing_end)
531 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
532 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
533 << (int) Actions.getObjCContainerKind();
534 AtEnd.setBegin(Tok.getLocation());
535 AtEnd.setEnd(Tok.getLocation());
536 }
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattnera2449b22008-10-20 05:57:40 +0000538 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000539 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000540 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000541 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000542 allProperties.data(), allProperties.size(),
543 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000544}
545
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000546/// Parse property attribute declarations.
547///
548/// property-attr-decl: '(' property-attrlist ')'
549/// property-attrlist:
550/// property-attribute
551/// property-attrlist ',' property-attribute
552/// property-attribute:
553/// getter '=' identifier
554/// setter '=' identifier ':'
555/// readonly
556/// readwrite
557/// assign
558/// retain
559/// copy
560/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000561/// atomic
562/// strong
563/// weak
564/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000565///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000566void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000567 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000568 BalancedDelimiterTracker T(*this, tok::l_paren);
569 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000571 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000572 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000573 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000574 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000575 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000576 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000578 // If this is not an identifier at all, bail out early.
579 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000580 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000581 return;
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Chris Lattner156b0612008-10-20 07:37:22 +0000584 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattner92e62b02008-11-20 04:42:34 +0000586 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000587 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000588 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000589 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000590 else if (II->isStr("unsafe_unretained"))
591 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000592 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000593 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000594 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000595 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000596 else if (II->isStr("strong"))
597 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000598 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000599 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000600 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000601 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000602 else if (II->isStr("atomic"))
603 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000604 else if (II->isStr("weak"))
605 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000606 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000607 bool IsSetter = II->getNameStart()[0] == 's';
608
Chris Lattnere00da7c2008-10-20 07:39:53 +0000609 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000610 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
611 diag::err_objc_expected_equal_for_getter;
612
613 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000614 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor4ad96852009-11-19 07:41:15 +0000616 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000617 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000618 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000619 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000620 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000621 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000622 }
623
Anders Carlsson42499be2010-10-02 17:45:21 +0000624
625 SourceLocation SelLoc;
626 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
627
628 if (!SelIdent) {
629 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
630 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000631 SkipUntil(tok::r_paren);
632 return;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlsson42499be2010-10-02 17:45:21 +0000635 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000636 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000637 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000639 if (ExpectAndConsume(tok::colon,
640 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000641 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000642 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000643 } else {
644 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000645 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000646 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000647 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000648 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000649 SkipUntil(tok::r_paren);
650 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Chris Lattner156b0612008-10-20 07:37:22 +0000653 if (Tok.isNot(tok::comma))
654 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattner156b0612008-10-20 07:37:22 +0000656 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000659 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000660}
661
Steve Naroff3536b442007-09-06 21:24:23 +0000662/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000663/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000664/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000665///
666/// objc-instance-method: '-'
667/// objc-class-method: '+'
668///
Steve Naroff4985ace2007-08-22 18:35:33 +0000669/// objc-method-attributes: [OBJC2]
670/// __attribute__((deprecated))
671///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000672Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000673 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000674 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000675
Mike Stump1eb44332009-09-09 15:08:12 +0000676 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000677 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000678 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000679 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000680 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000681 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000682 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000683}
684
685/// objc-selector:
686/// identifier
687/// one of
688/// enum struct union if else while do for switch case default
689/// break continue return goto asm sizeof typeof __alignof
690/// unsigned long const short volatile signed restrict _Complex
691/// in out inout bycopy byref oneway int char float double void _Bool
692///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000693IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000694
Chris Lattnerff384912007-10-07 02:00:24 +0000695 switch (Tok.getKind()) {
696 default:
697 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000698 case tok::ampamp:
699 case tok::ampequal:
700 case tok::amp:
701 case tok::pipe:
702 case tok::tilde:
703 case tok::exclaim:
704 case tok::exclaimequal:
705 case tok::pipepipe:
706 case tok::pipeequal:
707 case tok::caret:
708 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000709 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000710 if (isLetter(ThisTok[0])) {
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000711 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
712 Tok.setKind(tok::identifier);
713 SelectorLoc = ConsumeToken();
714 return II;
715 }
716 return 0;
717 }
718
Chris Lattnerff384912007-10-07 02:00:24 +0000719 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000720 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000721 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000722 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000723 case tok::kw_break:
724 case tok::kw_case:
725 case tok::kw_catch:
726 case tok::kw_char:
727 case tok::kw_class:
728 case tok::kw_const:
729 case tok::kw_const_cast:
730 case tok::kw_continue:
731 case tok::kw_default:
732 case tok::kw_delete:
733 case tok::kw_do:
734 case tok::kw_double:
735 case tok::kw_dynamic_cast:
736 case tok::kw_else:
737 case tok::kw_enum:
738 case tok::kw_explicit:
739 case tok::kw_export:
740 case tok::kw_extern:
741 case tok::kw_false:
742 case tok::kw_float:
743 case tok::kw_for:
744 case tok::kw_friend:
745 case tok::kw_goto:
746 case tok::kw_if:
747 case tok::kw_inline:
748 case tok::kw_int:
749 case tok::kw_long:
750 case tok::kw_mutable:
751 case tok::kw_namespace:
752 case tok::kw_new:
753 case tok::kw_operator:
754 case tok::kw_private:
755 case tok::kw_protected:
756 case tok::kw_public:
757 case tok::kw_register:
758 case tok::kw_reinterpret_cast:
759 case tok::kw_restrict:
760 case tok::kw_return:
761 case tok::kw_short:
762 case tok::kw_signed:
763 case tok::kw_sizeof:
764 case tok::kw_static:
765 case tok::kw_static_cast:
766 case tok::kw_struct:
767 case tok::kw_switch:
768 case tok::kw_template:
769 case tok::kw_this:
770 case tok::kw_throw:
771 case tok::kw_true:
772 case tok::kw_try:
773 case tok::kw_typedef:
774 case tok::kw_typeid:
775 case tok::kw_typename:
776 case tok::kw_typeof:
777 case tok::kw_union:
778 case tok::kw_unsigned:
779 case tok::kw_using:
780 case tok::kw_virtual:
781 case tok::kw_void:
782 case tok::kw_volatile:
783 case tok::kw_wchar_t:
784 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000785 case tok::kw__Bool:
786 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000787 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000788 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000789 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000790 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000791 }
Steve Naroff294494e2007-08-22 16:35:03 +0000792}
793
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000794/// objc-for-collection-in: 'in'
795///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000796bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000797 // FIXME: May have to do additional look-ahead to only allow for
798 // valid tokens following an 'in'; such as an identifier, unary operators,
799 // '[' etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000800 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000801 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000802}
803
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000805/// qualifier list and builds their bitmask representation in the input
806/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000807///
808/// objc-type-qualifiers:
809/// objc-type-qualifier
810/// objc-type-qualifiers objc-type-qualifier
811///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000812void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000813 Declarator::TheContext Context) {
814 assert(Context == Declarator::ObjCParameterContext ||
815 Context == Declarator::ObjCResultContext);
816
Chris Lattnere8b724d2007-12-12 06:56:32 +0000817 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000818 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000819 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000820 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000821 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000822 }
823
Chris Lattnercb53b362007-12-27 19:57:00 +0000824 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000825 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Chris Lattnere8b724d2007-12-12 06:56:32 +0000827 const IdentifierInfo *II = Tok.getIdentifierInfo();
828 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000829 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000830 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000832 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000833 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000834 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
836 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
837 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
838 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
839 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
840 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000841 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000843 ConsumeToken();
844 II = 0;
845 break;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnere8b724d2007-12-12 06:56:32 +0000848 // If this wasn't a recognized qualifier, bail out.
849 if (II) return;
850 }
851}
852
John McCallcdda47f2011-10-01 09:56:14 +0000853/// Take all the decl attributes out of the given list and add
854/// them to the given attribute set.
855static void takeDeclAttributes(ParsedAttributes &attrs,
856 AttributeList *list) {
857 while (list) {
858 AttributeList *cur = list;
859 list = cur->getNext();
860
861 if (!cur->isUsedAsTypeAttr()) {
862 // Clear out the next pointer. We're really completely
863 // destroying the internal invariants of the declarator here,
864 // but it doesn't matter because we're done with it.
865 cur->setNext(0);
866 attrs.add(cur);
867 }
868 }
869}
870
871/// takeDeclAttributes - Take all the decl attributes from the given
872/// declarator and add them to the given list.
873static void takeDeclAttributes(ParsedAttributes &attrs,
874 Declarator &D) {
875 // First, take ownership of all attributes.
876 attrs.getPool().takeAllFrom(D.getAttributePool());
877 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
878
879 // Now actually move the attributes over.
880 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
881 takeDeclAttributes(attrs, D.getAttributes());
882 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
883 takeDeclAttributes(attrs,
884 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
885}
886
Chris Lattnere8b724d2007-12-12 06:56:32 +0000887/// objc-type-name:
888/// '(' objc-type-qualifiers[opt] type-name ')'
889/// '(' objc-type-qualifiers[opt] ')'
890///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000891ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000892 Declarator::TheContext context,
893 ParsedAttributes *paramAttrs) {
894 assert(context == Declarator::ObjCParameterContext ||
895 context == Declarator::ObjCResultContext);
896 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
897
Chris Lattnerdf195262007-10-09 17:51:17 +0000898 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000900 BalancedDelimiterTracker T(*this, tok::l_paren);
901 T.consumeOpen();
902
Chris Lattnere8904e92008-08-23 01:48:03 +0000903 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000904 ObjCDeclContextSwitch ObjCDC(*this);
905
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000906 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000907 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000908
John McCallb3d87482010-08-24 05:47:05 +0000909 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000910 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000911 // Parse an abstract declarator.
912 DeclSpec declSpec(AttrFactory);
913 declSpec.setObjCQualifiers(&DS);
914 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanian6d1de1b2012-05-29 21:52:45 +0000915 declSpec.SetRangeEnd(Tok.getLocation());
John McCallcdda47f2011-10-01 09:56:14 +0000916 Declarator declarator(declSpec, context);
917 ParseDeclarator(declarator);
918
919 // If that's not invalid, extract a type.
920 if (!declarator.isInvalidType()) {
921 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
922 if (!type.isInvalid())
923 Ty = type.get();
924
925 // If we're parsing a parameter, steal all the decl attributes
926 // and add them to the decl spec.
927 if (context == Declarator::ObjCParameterContext)
928 takeDeclAttributes(*paramAttrs, declarator);
929 }
930 } else if (context == Declarator::ObjCResultContext &&
931 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000932 if (!Ident_instancetype)
933 Ident_instancetype = PP.getIdentifierInfo("instancetype");
934
935 if (Tok.getIdentifierInfo() == Ident_instancetype) {
936 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
937 ConsumeToken();
938 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000939 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000940
Steve Naroffd7333c22008-10-21 14:15:04 +0000941 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000942 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000943 else if (Tok.getLocation() == TypeStartLoc) {
944 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000945 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000946 SkipUntil(tok::r_paren);
947 } else {
948 // Otherwise, we found *something*, but didn't get a ')' in the right
949 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000950 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000951 }
Steve Narofff28b2642007-09-05 23:30:30 +0000952 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000953}
954
955/// objc-method-decl:
956/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000957/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000958/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000959/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000960///
961/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000962/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000963/// objc-keyword-selector objc-keyword-decl
964///
965/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000966/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
967/// objc-selector ':' objc-keyword-attributes[opt] identifier
968/// ':' objc-type-name objc-keyword-attributes[opt] identifier
969/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000970///
Steve Naroff4985ace2007-08-22 18:35:33 +0000971/// objc-parmlist:
972/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000973///
Steve Naroff4985ace2007-08-22 18:35:33 +0000974/// objc-parms:
975/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000976///
Steve Naroff4985ace2007-08-22 18:35:33 +0000977/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000978/// , ...
979///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000980/// objc-keyword-attributes: [OBJC2]
981/// __attribute__((unused))
982///
John McCalld226f652010-08-21 09:40:31 +0000983Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000984 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000985 tok::ObjCKeywordKind MethodImplKind,
986 bool MethodDefinition) {
John McCall92576642012-05-07 06:16:41 +0000987 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall54abf7d2009-11-04 02:18:39 +0000988
Douglas Gregore8f5a172010-04-07 00:21:17 +0000989 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000990 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000991 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000992 cutOffParsing();
993 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000994 }
995
Chris Lattnere8904e92008-08-23 01:48:03 +0000996 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000997 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000999 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +00001000 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek9e049352010-02-18 23:05:16 +00001002 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00001003 ParsedAttributes methodAttrs(AttrFactory);
David Blaikie4e4d0842012-03-11 07:00:24 +00001004 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001005 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +00001006
Douglas Gregore8f5a172010-04-07 00:21:17 +00001007 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001008 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001009 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001010 cutOffParsing();
1011 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +00001012 }
1013
Ted Kremenek9e049352010-02-18 23:05:16 +00001014 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +00001015 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001016 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +00001017
Steve Naroff84c43102009-02-11 20:43:13 +00001018 // An unnamed colon is valid.
1019 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001020 Diag(Tok, diag::err_expected_selector_for_method)
1021 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniand30ec702012-07-26 17:32:28 +00001022 // Skip until we get a ; or @.
1023 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCalld226f652010-08-21 09:40:31 +00001024 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +00001025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattner5f9e2722011-07-23 10:55:15 +00001027 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001028 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001029 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001030 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001031 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattnerff384912007-10-07 02:00:24 +00001033 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001034 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001035 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001036 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001037 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001038 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001039 methodAttrs.getList(), MethodImplKind,
1040 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001041 PD.complete(Result);
1042 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001043 }
Steve Narofff28b2642007-09-05 23:30:30 +00001044
Chris Lattner5f9e2722011-07-23 10:55:15 +00001045 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001046 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001047 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smith3a2b7a12013-01-28 22:42:45 +00001048 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1049 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001050
1051 AttributePool allParamAttrs(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001052 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001053 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001054 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattnerff384912007-10-07 02:00:24 +00001056 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001058 Diag(Tok, diag::err_expected_colon);
1059 break;
1060 }
1061 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001062
John McCallb3d87482010-08-24 05:47:05 +00001063 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001064 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001065 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1066 Declarator::ObjCParameterContext,
1067 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001068
Chris Lattnerff384912007-10-07 02:00:24 +00001069 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001070 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001071 ArgInfo.ArgAttrs = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001072 if (getLangOpts().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001073 MaybeParseGNUAttributes(paramAttrs);
1074 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001075 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001076
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001077 // Code completion for the next piece of the selector.
1078 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001079 KeyIdents.push_back(SelIdent);
1080 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1081 mType == tok::minus,
1082 /*AtParameterName=*/true,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00001083 ReturnType, KeyIdents);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001084 cutOffParsing();
1085 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001086 }
1087
Chris Lattnerdf195262007-10-09 17:51:17 +00001088 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001089 Diag(Tok, diag::err_expected_ident); // missing argument name.
1090 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattnere294d3f2009-04-11 18:57:04 +00001093 ArgInfo.Name = Tok.getIdentifierInfo();
1094 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001095 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattnere294d3f2009-04-11 18:57:04 +00001097 ArgInfos.push_back(ArgInfo);
1098 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001099 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001100
John McCall0b7e6782011-03-24 11:26:52 +00001101 // Make sure the attributes persist.
1102 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1103
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001104 // Code completion for the next piece of the selector.
1105 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001106 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1107 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001108 /*AtParameterName=*/false,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00001109 ReturnType, KeyIdents);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001110 cutOffParsing();
1111 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001112 }
1113
Chris Lattnerff384912007-10-07 02:00:24 +00001114 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001115 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenekb648cf02012-09-12 16:50:35 +00001116 if (!SelIdent && Tok.isNot(tok::colon))
1117 break;
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001118 if (!SelIdent) {
Fariborz Jahanian08602352012-09-17 19:15:26 +00001119 SourceLocation ColonLoc = Tok.getLocation();
1120 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001121 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1122 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1123 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanian08602352012-09-17 19:15:26 +00001124 }
1125 }
Chris Lattnerff384912007-10-07 02:00:24 +00001126 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Steve Naroff335eafa2007-11-15 12:35:21 +00001129 bool isVariadic = false;
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001130 bool cStyleParamWarned = false;
Chris Lattnerff384912007-10-07 02:00:24 +00001131 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001132 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001133 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001134 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001135 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001136 ConsumeToken();
1137 break;
1138 }
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001139 if (!cStyleParamWarned) {
1140 Diag(Tok, diag::warn_cstyle_param);
1141 cStyleParamWarned = true;
1142 }
John McCall0b7e6782011-03-24 11:26:52 +00001143 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001144 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001145 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001146 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1147 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001148 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001149 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001150 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1151 ParmDecl.getIdentifierLoc(),
1152 Param,
1153 0));
Chris Lattnerff384912007-10-07 02:00:24 +00001154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001156 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001157 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001158 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001159 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001160
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001161 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001162 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001163
Chris Lattnerff384912007-10-07 02:00:24 +00001164 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1165 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001166 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001167 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001168 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001169 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001170 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001171 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001172 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001173
John McCall54abf7d2009-11-04 02:18:39 +00001174 PD.complete(Result);
1175 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001176}
1177
Steve Naroffdac269b2007-08-20 21:31:48 +00001178/// objc-protocol-refs:
1179/// '<' identifier-list '>'
1180///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001181bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001182ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1183 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001184 bool WarnOnDeclarations,
1185 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001186 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001188 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Chris Lattner5f9e2722011-07-23 10:55:15 +00001190 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Chris Lattnere13b9592008-07-26 04:03:38 +00001192 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001193 if (Tok.is(tok::code_completion)) {
1194 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1195 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001196 cutOffParsing();
1197 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001198 }
1199
Chris Lattnere13b9592008-07-26 04:03:38 +00001200 if (Tok.isNot(tok::identifier)) {
1201 Diag(Tok, diag::err_expected_ident);
1202 SkipUntil(tok::greater);
1203 return true;
1204 }
1205 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1206 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001207 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001208 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattnere13b9592008-07-26 04:03:38 +00001210 if (Tok.isNot(tok::comma))
1211 break;
1212 ConsumeToken();
1213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Chris Lattnere13b9592008-07-26 04:03:38 +00001215 // Consume the '>'.
Nico Weberb707a472012-12-14 18:22:38 +00001216 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattnere13b9592008-07-26 04:03:38 +00001217 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Chris Lattnere13b9592008-07-26 04:03:38 +00001219 // Convert the list of protocols identifiers into a list of protocol decls.
1220 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1221 &ProtocolIdents[0], ProtocolIdents.size(),
1222 Protocols);
1223 return false;
1224}
1225
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001226/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1227/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001228bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001229 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikie4e4d0842012-03-11 07:00:24 +00001230 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001231 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001232 SmallVector<Decl *, 8> ProtocolDecl;
1233 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001234 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1235 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001236 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1237 ProtocolLocs.data(), LAngleLoc);
1238 if (EndProtoLoc.isValid())
1239 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001240 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001241}
1242
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001243void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1244 BalancedDelimiterTracker &T,
1245 SmallVectorImpl<Decl *> &AllIvarDecls,
1246 bool RBraceMissing) {
1247 if (!RBraceMissing)
1248 T.consumeClose();
1249
1250 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1251 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1252 Actions.ActOnObjCContainerFinishDefinition();
1253 // Call ActOnFields() even if we don't have any decls. This is useful
1254 // for code rewriting tools that need to be aware of the empty list.
1255 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1256 AllIvarDecls,
1257 T.getOpenLocation(), T.getCloseLocation(), 0);
1258}
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001259
Steve Naroffdac269b2007-08-20 21:31:48 +00001260/// objc-class-instance-variables:
1261/// '{' objc-instance-variable-decl-list[opt] '}'
1262///
1263/// objc-instance-variable-decl-list:
1264/// objc-visibility-spec
1265/// objc-instance-variable-decl ';'
1266/// ';'
1267/// objc-instance-variable-decl-list objc-visibility-spec
1268/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1269/// objc-instance-variable-decl-list ';'
1270///
1271/// objc-visibility-spec:
1272/// @private
1273/// @protected
1274/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001275/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001276///
1277/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001278/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001279///
John McCalld226f652010-08-21 09:40:31 +00001280void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001281 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001282 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001283 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001284 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001285
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001286 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001287 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001288
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001289 BalancedDelimiterTracker T(*this, tok::l_brace);
1290 T.consumeOpen();
Steve Naroffddbff782007-08-21 21:17:12 +00001291 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001292 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001293 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Steve Naroffddbff782007-08-21 21:17:12 +00001295 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001296 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001297 ConsumeExtraSemi(InstanceVariableList);
Steve Naroffddbff782007-08-21 21:17:12 +00001298 continue;
1299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Steve Naroffddbff782007-08-21 21:17:12 +00001301 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001302 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001303 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001304
1305 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001306 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001307 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001308 }
1309
Steve Naroff861cf3e2007-08-23 18:16:40 +00001310 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001311 case tok::objc_private:
1312 case tok::objc_public:
1313 case tok::objc_protected:
1314 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001315 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001316 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001317 continue;
Fariborz Jahanian695031c2013-03-20 18:45:49 +00001318
1319 case tok::objc_end:
1320 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001321 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1322 Tok.setKind(tok::at);
1323 Tok.setLength(1);
1324 PP.EnterToken(Tok);
1325 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1326 T, AllIvarDecls, true);
1327 return;
Fariborz Jahanian695031c2013-03-20 18:45:49 +00001328
1329 default:
1330 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1331 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001332 }
1333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001335 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001336 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001337 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001338 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001339 }
1340
John McCallbdd563e2009-11-03 02:38:08 +00001341 struct ObjCIvarCallback : FieldCallback {
1342 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001343 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001344 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001345 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001346
John McCalld226f652010-08-21 09:40:31 +00001347 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001348 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001349 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1350 }
1351
Eli Friedmandcdff462012-08-08 23:53:27 +00001352 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001353 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001354 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001355 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001356 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001357 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001358 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001359 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001360 if (Field)
1361 AllIvarDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001362 FD.complete(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001363 }
1364 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001365
Chris Lattnere1359422008-04-10 06:46:29 +00001366 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001367 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001368 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Chris Lattnerdf195262007-10-09 17:51:17 +00001370 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001371 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001372 } else {
1373 Diag(Tok, diag::err_expected_semi_decl_list);
1374 // Skip to end of block or statement
1375 SkipUntil(tok::r_brace, true, true);
1376 }
1377 }
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001378 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1379 T, AllIvarDecls, false);
Steve Naroffddbff782007-08-21 21:17:12 +00001380 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001381}
Steve Naroffdac269b2007-08-20 21:31:48 +00001382
1383/// objc-protocol-declaration:
1384/// objc-protocol-definition
1385/// objc-protocol-forward-reference
1386///
1387/// objc-protocol-definition:
James Dennett17d26a62012-06-11 06:19:40 +00001388/// \@protocol identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001389/// objc-protocol-refs[opt]
1390/// objc-interface-decl-list
James Dennett17d26a62012-06-11 06:19:40 +00001391/// \@end
Steve Naroffdac269b2007-08-20 21:31:48 +00001392///
1393/// objc-protocol-forward-reference:
James Dennett17d26a62012-06-11 06:19:40 +00001394/// \@protocol identifier-list ';'
Steve Naroffdac269b2007-08-20 21:31:48 +00001395///
James Dennett17d26a62012-06-11 06:19:40 +00001396/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001397/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001398/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001399Parser::DeclGroupPtrTy
1400Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1401 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001402 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001403 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1404 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Douglas Gregor083128f2009-11-18 04:49:41 +00001406 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001407 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001408 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001409 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001410 }
1411
Nico Webere145bfd2013-04-04 00:15:10 +00001412 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber9f4f5f12013-04-03 17:36:11 +00001413
Chris Lattnerdf195262007-10-09 17:51:17 +00001414 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001415 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001416 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001417 }
1418 // Save the protocol name, then consume it.
1419 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1420 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Chris Lattnerdf195262007-10-09 17:51:17 +00001422 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001423 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001424 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001425 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001426 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001429 CheckNestedObjCContexts(AtLoc);
1430
Chris Lattnerdf195262007-10-09 17:51:17 +00001431 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001432 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001433 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1434
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001435 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001436 while (1) {
1437 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001438 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001439 Diag(Tok, diag::err_expected_ident);
1440 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001441 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001442 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001443 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1444 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001445 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Chris Lattnerdf195262007-10-09 17:51:17 +00001447 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001448 break;
1449 }
1450 // Consume the ';'.
1451 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001452 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Steve Naroffe440eb82007-10-10 17:32:04 +00001454 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001455 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001456 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001457 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001458 }
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001460 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001461 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001462
Chris Lattner5f9e2722011-07-23 10:55:15 +00001463 SmallVector<Decl *, 8> ProtocolRefs;
1464 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001465 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001466 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1467 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001468 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001469
John McCalld226f652010-08-21 09:40:31 +00001470 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001471 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001472 ProtocolRefs.data(),
1473 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001474 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001475 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001476
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001477 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001478 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001479}
Steve Naroffdac269b2007-08-20 21:31:48 +00001480
1481/// objc-implementation:
1482/// objc-class-implementation-prologue
1483/// objc-category-implementation-prologue
1484///
1485/// objc-class-implementation-prologue:
1486/// @implementation identifier objc-superclass[opt]
1487/// objc-class-instance-variables[opt]
1488///
1489/// objc-category-implementation-prologue:
1490/// @implementation identifier ( identifier )
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001491Parser::DeclGroupPtrTy
1492Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001493 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1494 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001495 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001496 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001498 // Code completion after '@implementation'.
1499 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001500 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001501 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001502 return DeclGroupPtrTy();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001503 }
1504
Nico Webere145bfd2013-04-04 00:15:10 +00001505 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber9f4f5f12013-04-03 17:36:11 +00001506
Chris Lattnerdf195262007-10-09 17:51:17 +00001507 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001508 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001509 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001510 }
1511 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001512 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001513 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001514 Decl *ObjCImpDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
1516 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001517 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001518 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001519 SourceLocation categoryLoc, rparenLoc;
1520 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001522 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001523 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001524 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001525 return DeclGroupPtrTy();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001526 }
1527
Chris Lattnerdf195262007-10-09 17:51:17 +00001528 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001529 categoryId = Tok.getIdentifierInfo();
1530 categoryLoc = ConsumeToken();
1531 } else {
1532 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001533 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001534 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001535 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001536 Diag(Tok, diag::err_expected_rparen);
1537 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001538 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001539 }
1540 rparenLoc = ConsumeParen();
Fariborz Jahaniane4bb7492013-05-17 17:58:11 +00001541 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1542 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1543 AttributeFactory attr;
1544 DeclSpec DS(attr);
1545 (void)ParseObjCProtocolQualifiers(DS);
1546 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001547 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001548 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001549 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001550
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001551 } else {
1552 // We have a class implementation
1553 SourceLocation superClassLoc;
1554 IdentifierInfo *superClassId = 0;
1555 if (Tok.is(tok::colon)) {
1556 // We have a super class
1557 ConsumeToken();
1558 if (Tok.isNot(tok::identifier)) {
1559 Diag(Tok, diag::err_expected_ident); // missing super class name.
1560 return DeclGroupPtrTy();
1561 }
1562 superClassId = Tok.getIdentifierInfo();
1563 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001564 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001565 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1566 AtLoc, nameId, nameLoc,
1567 superClassId, superClassLoc);
1568
1569 if (Tok.is(tok::l_brace)) // we have ivars
1570 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian51c30af2013-04-24 23:23:47 +00001571 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1572 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1573 // try to recover.
1574 AttributeFactory attr;
1575 DeclSpec DS(attr);
1576 (void)ParseObjCProtocolQualifiers(DS);
1577 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001578 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001579 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001581 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001582
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001583 {
1584 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1585 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1586 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001587 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001588 MaybeParseMicrosoftAttributes(attrs);
1589 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1590 DeclGroupRef DG = DGP.get();
1591 DeclsInGroup.append(DG.begin(), DG.end());
1592 }
1593 }
1594 }
1595
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001596 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001597}
Steve Naroff60fccee2007-10-29 21:38:07 +00001598
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001599Parser::DeclGroupPtrTy
1600Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001601 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1602 "ParseObjCAtEndDeclaration(): Expected @end");
1603 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001604 if (CurParsedObjCImpl)
1605 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001606 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001607 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001608 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001609 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001610}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001611
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001612Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1613 if (!Finished) {
1614 finish(P.Tok.getLocation());
1615 if (P.Tok.is(tok::eof)) {
1616 P.Diag(P.Tok, diag::err_objc_missing_end)
1617 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1618 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1619 << Sema::OCK_Implementation;
1620 }
1621 }
1622 P.CurParsedObjCImpl = 0;
1623 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001624}
1625
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001626void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1627 assert(!Finished);
1628 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1629 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001630 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1631 true/*Methods*/);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001632
1633 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1634
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001635 if (HasCFunction)
1636 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1637 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1638 false/*c-functions*/);
1639
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001640 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001641 for (LateParsedObjCMethodContainer::iterator
1642 I = LateParsedObjCMethods.begin(),
1643 E = LateParsedObjCMethods.end(); I != E; ++I)
1644 delete *I;
1645 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001646
1647 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001648}
1649
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001650/// compatibility-alias-decl:
1651/// @compatibility_alias alias-name class-name ';'
1652///
John McCalld226f652010-08-21 09:40:31 +00001653Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001654 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1655 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1656 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001657 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001658 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001659 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001660 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001661 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1662 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001663 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001664 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001665 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001666 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001667 IdentifierInfo *classId = Tok.getIdentifierInfo();
1668 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001669 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1670 "@compatibility_alias");
Richard Smithde01b7a2012-08-08 23:32:13 +00001671 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1672 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001673}
1674
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001675/// property-synthesis:
1676/// @synthesize property-ivar-list ';'
1677///
1678/// property-ivar-list:
1679/// property-ivar
1680/// property-ivar-list ',' property-ivar
1681///
1682/// property-ivar:
1683/// identifier
1684/// identifier '=' identifier
1685///
John McCalld226f652010-08-21 09:40:31 +00001686Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001687 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniandf1fdfd2013-04-29 15:35:35 +00001688 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001689 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Douglas Gregorb328c422009-11-18 19:45:45 +00001691 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001692 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001693 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001694 cutOffParsing();
1695 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001696 }
1697
Douglas Gregorb328c422009-11-18 19:45:45 +00001698 if (Tok.isNot(tok::identifier)) {
1699 Diag(Tok, diag::err_synthesized_property_name);
1700 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001701 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001702 }
1703
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001704 IdentifierInfo *propertyIvar = 0;
1705 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1706 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001707 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001708 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001709 // property '=' ivar-name
1710 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001711
1712 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001713 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001714 cutOffParsing();
1715 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001716 }
1717
Chris Lattnerdf195262007-10-09 17:51:17 +00001718 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001719 Diag(Tok, diag::err_expected_ident);
1720 break;
1721 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001722 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001723 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001724 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001725 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001726 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001727 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001728 break;
1729 ConsumeToken(); // consume ','
1730 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001731 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001732 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001733}
1734
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001735/// property-dynamic:
1736/// @dynamic property-list
1737///
1738/// property-list:
1739/// identifier
1740/// property-list ',' identifier
1741///
John McCalld226f652010-08-21 09:40:31 +00001742Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001743 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1744 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001745 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001746 while (true) {
1747 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001748 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001749 cutOffParsing();
1750 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001751 }
1752
1753 if (Tok.isNot(tok::identifier)) {
1754 Diag(Tok, diag::err_expected_ident);
1755 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001756 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001757 }
1758
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001759 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1760 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001761 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001762 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001763
Chris Lattnerdf195262007-10-09 17:51:17 +00001764 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001765 break;
1766 ConsumeToken(); // consume ','
1767 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001768 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001769 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001770}
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001772/// objc-throw-statement:
1773/// throw expression[opt];
1774///
John McCall60d7b3a2010-08-24 06:29:42 +00001775StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1776 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001777 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001778 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001779 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001780 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001781 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001782 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001783 }
1784 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001785 // consume ';'
1786 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001787 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001788}
1789
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001790/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001791/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001792///
John McCall60d7b3a2010-08-24 06:29:42 +00001793StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001794Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001795 ConsumeToken(); // consume synchronized
1796 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001797 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001798 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001799 }
John McCall07524032011-07-27 21:50:02 +00001800
1801 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001802 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001803 ExprResult operand(ParseExpression());
1804
1805 if (Tok.is(tok::r_paren)) {
1806 ConsumeParen(); // ')'
1807 } else {
1808 if (!operand.isInvalid())
1809 Diag(Tok, diag::err_expected_rparen);
1810
1811 // Skip forward until we see a left brace, but don't consume it.
1812 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001813 }
John McCall07524032011-07-27 21:50:02 +00001814
1815 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001816 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001817 if (!operand.isInvalid())
1818 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001819 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001820 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001821
John McCall07524032011-07-27 21:50:02 +00001822 // Check the @synchronized operand now.
1823 if (!operand.isInvalid())
1824 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001825
John McCall07524032011-07-27 21:50:02 +00001826 // Parse the compound statement within a new scope.
1827 ParseScope bodyScope(this, Scope::DeclScope);
1828 StmtResult body(ParseCompoundStatementBody());
1829 bodyScope.Exit();
1830
1831 // If there was a semantic or parse error earlier with the
1832 // operand, fail now.
1833 if (operand.isInvalid())
1834 return StmtError();
1835
1836 if (body.isInvalid())
1837 body = Actions.ActOnNullStmt(Tok.getLocation());
1838
1839 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001840}
1841
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001842/// objc-try-catch-statement:
1843/// @try compound-statement objc-catch-list[opt]
1844/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1845///
1846/// objc-catch-list:
1847/// @catch ( parameter-declaration ) compound-statement
1848/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1849/// catch-parameter-declaration:
1850/// parameter-declaration
1851/// '...' [OBJC2]
1852///
John McCall60d7b3a2010-08-24 06:29:42 +00001853StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001854 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001855
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001856 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001857 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001858 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001859 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001860 }
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001861 StmtVector CatchStmts;
John McCall60d7b3a2010-08-24 06:29:42 +00001862 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001863 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001864 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001865 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001866 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001867 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001868
Chris Lattnerdf195262007-10-09 17:51:17 +00001869 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001870 // At this point, we need to lookahead to determine if this @ is the start
1871 // of an @catch or @finally. We don't want to consume the @ token if this
1872 // is an @try or @encode or something else.
1873 Token AfterAt = GetLookAheadToken(1);
1874 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1875 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1876 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001877
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001878 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001879 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001880 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001881 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001882 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001883 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001884 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001885 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001886 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001887 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001888 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001889 ParseDeclarator(ParmDecl);
1890
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001891 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001892 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001893 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001894 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001895 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Steve Naroff93a25952009-04-07 22:56:58 +00001897 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Steve Naroff93a25952009-04-07 22:56:58 +00001899 if (Tok.is(tok::r_paren))
1900 RParenLoc = ConsumeParen();
1901 else // Skip over garbage, until we get to ')'. Eat the ')'.
1902 SkipUntil(tok::r_paren, true, false);
1903
John McCall60d7b3a2010-08-24 06:29:42 +00001904 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001905 if (Tok.is(tok::l_brace))
1906 CatchBody = ParseCompoundStatementBody();
1907 else
1908 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001909 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001910 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001911
John McCall60d7b3a2010-08-24 06:29:42 +00001912 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001913 RParenLoc,
1914 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001915 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001916 if (!Catch.isInvalid())
1917 CatchStmts.push_back(Catch.release());
1918
Steve Naroff64515f32008-02-05 21:27:35 +00001919 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001920 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1921 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001922 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001923 }
1924 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001925 } else {
1926 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001927 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001928 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001929
John McCall60d7b3a2010-08-24 06:29:42 +00001930 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001931 if (Tok.is(tok::l_brace))
1932 FinallyBody = ParseCompoundStatementBody();
1933 else
1934 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001935 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001936 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001937 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001938 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001939 catch_or_finally_seen = true;
1940 break;
1941 }
1942 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001943 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001944 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001945 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001946 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001947
John McCall9ae2f072010-08-23 23:25:46 +00001948 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001949 CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001950 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001951}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001952
John McCallf85e1932011-06-15 23:02:42 +00001953/// objc-autoreleasepool-statement:
1954/// @autoreleasepool compound-statement
1955///
1956StmtResult
1957Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1958 ConsumeToken(); // consume autoreleasepool
1959 if (Tok.isNot(tok::l_brace)) {
1960 Diag(Tok, diag::err_expected_lbrace);
1961 return StmtError();
1962 }
1963 // Enter a scope to hold everything within the compound stmt. Compound
1964 // statements can always hold declarations.
1965 ParseScope BodyScope(this, Scope::DeclScope);
1966
1967 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1968
1969 BodyScope.Exit();
1970 if (AutoreleasePoolBody.isInvalid())
1971 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1972 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1973 AutoreleasePoolBody.take());
1974}
1975
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001976/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1977/// for later parsing.
1978void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1979 LexedMethod* LM = new LexedMethod(this, MDecl);
1980 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1981 CachedTokens &Toks = LM->Toks;
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001982 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001983 Toks.push_back(Tok);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001984 if (Tok.is(tok::kw_try)) {
1985 ConsumeToken();
Fariborz Jahaniandbd69452012-08-10 20:34:17 +00001986 if (Tok.is(tok::colon)) {
1987 Toks.push_back(Tok);
1988 ConsumeToken();
1989 while (Tok.isNot(tok::l_brace)) {
1990 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1991 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1992 }
1993 }
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001994 Toks.push_back(Tok); // also store '{'
1995 }
1996 else if (Tok.is(tok::colon)) {
1997 ConsumeToken();
1998 while (Tok.isNot(tok::l_brace)) {
1999 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2000 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2001 }
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002002 Toks.push_back(Tok); // also store '{'
2003 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002004 ConsumeBrace();
2005 // Consume everything up to (and including) the matching right brace.
2006 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002007 while (Tok.is(tok::kw_catch)) {
2008 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2009 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2010 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002011}
2012
Steve Naroff3536b442007-09-06 21:24:23 +00002013/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002014///
John McCalld226f652010-08-21 09:40:31 +00002015Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002016 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00002017
John McCallf312b1e2010-08-26 23:41:50 +00002018 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2019 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002021 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00002022 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002023 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00002024 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00002025 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00002026 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002027 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00002028 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002029
Steve Naroff409be832007-11-11 19:54:21 +00002030 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00002031 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002032 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Steve Naroff409be832007-11-11 19:54:21 +00002034 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2035 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Steve Naroff409be832007-11-11 19:54:21 +00002037 // If we didn't find the '{', bail out.
2038 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00002039 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002040 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002041
2042 if (!MDecl) {
2043 ConsumeBrace();
2044 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2045 return 0;
2046 }
2047
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002048 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002049 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahanianc9b97092012-08-09 17:15:00 +00002050 assert (CurParsedObjCImpl
2051 && "ParseObjCMethodDefinition - Method out of @implementation");
2052 // Consume the tokens and store them for later parsing.
2053 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff71c0a952007-11-13 23:01:27 +00002054 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00002055}
Anders Carlsson55085182007-08-21 17:43:55 +00002056
John McCall60d7b3a2010-08-24 06:29:42 +00002057StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002058 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002059 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002060 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002061 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00002062 }
2063
2064 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00002065 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002066
2067 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00002068 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002069
2070 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00002071 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00002072
2073 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2074 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002075
John McCall60d7b3a2010-08-24 06:29:42 +00002076 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002077 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00002078 // If the expression is invalid, skip ahead to the next semicolon. Not
2079 // doing this opens us up to the possibility of infinite loops if
2080 // ParseExpression does not consume any tokens.
2081 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00002082 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00002083 }
Chris Lattner5d803162009-12-07 16:33:19 +00002084
Steve Naroff64515f32008-02-05 21:27:35 +00002085 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00002086 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +00002087 return Actions.ActOnExprStmt(Res);
Steve Naroff64515f32008-02-05 21:27:35 +00002088}
2089
John McCall60d7b3a2010-08-24 06:29:42 +00002090ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002091 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002092 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002093 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002094 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002095 return ExprError();
2096
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002097 case tok::minus:
2098 case tok::plus: {
2099 tok::TokenKind Kind = Tok.getKind();
2100 SourceLocation OpLoc = ConsumeToken();
2101
2102 if (!Tok.is(tok::numeric_constant)) {
2103 const char *Symbol = 0;
2104 switch (Kind) {
2105 case tok::minus: Symbol = "-"; break;
2106 case tok::plus: Symbol = "+"; break;
2107 default: llvm_unreachable("missing unary operator case");
2108 }
2109 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2110 << Symbol;
2111 return ExprError();
2112 }
2113
2114 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2115 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002116 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002117 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002118 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002119
2120 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2121 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002122 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002123
2124 return ParsePostfixExpressionSuffix(
2125 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2126 }
2127
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002128 case tok::string_literal: // primary-expression: string-literal
2129 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002130 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002131
2132 case tok::char_constant:
2133 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2134
2135 case tok::numeric_constant:
2136 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2137
2138 case tok::kw_true: // Objective-C++, etc.
2139 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2140 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2141 case tok::kw_false: // Objective-C++, etc.
2142 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2143 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2144
2145 case tok::l_square:
2146 // Objective-C array literal
2147 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2148
2149 case tok::l_brace:
2150 // Objective-C dictionary literal
2151 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2152
Patrick Beardeb382ec2012-04-19 00:25:12 +00002153 case tok::l_paren:
2154 // Objective-C boxed expression
2155 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2156
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002157 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002158 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002159 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002160
Chris Lattner4fef81d2008-08-05 06:19:09 +00002161 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2162 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002163 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002164 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002165 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002166 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002167 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian6749ae12012-07-09 20:00:35 +00002168 default: {
2169 const char *str = 0;
2170 if (GetLookAheadToken(1).is(tok::l_brace)) {
2171 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2172 str =
2173 ch == 't' ? "try"
2174 : (ch == 'f' ? "finally"
2175 : (ch == 'a' ? "autoreleasepool" : 0));
2176 }
2177 if (str) {
2178 SourceLocation kwLoc = Tok.getLocation();
2179 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2180 FixItHint::CreateReplacement(kwLoc, str));
2181 }
2182 else
2183 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2184 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002185 }
Anders Carlsson55085182007-08-21 17:43:55 +00002186 }
Anders Carlsson55085182007-08-21 17:43:55 +00002187}
2188
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002189/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002190///
2191/// This routine parses the receiver of a message send in
2192/// Objective-C++ either as a type or as an expression. Note that this
2193/// routine must not be called to parse a send to 'super', since it
2194/// has no way to return such a result.
2195///
2196/// \param IsExpr Whether the receiver was parsed as an expression.
2197///
2198/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2199/// IsExpr is true), the parsed expression. If the receiver was parsed
2200/// as a type (\c IsExpr is false), the parsed type.
2201///
2202/// \returns True if an error occurred during parsing or semantic
2203/// analysis, in which case the arguments do not have valid
2204/// values. Otherwise, returns false for a successful parse.
2205///
2206/// objc-receiver: [C++]
2207/// 'super' [not parsed here]
2208/// expression
2209/// simple-type-specifier
2210/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002211bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002212 InMessageExpressionRAIIObject InMessage(*this, true);
2213
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002214 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2215 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2216 TryAnnotateTypeOrScopeToken();
2217
Kaelyn Uhrain7bf33402012-06-15 23:45:51 +00002218 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002219 // objc-receiver:
2220 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002221 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002222 if (Receiver.isInvalid())
2223 return true;
2224
2225 IsExpr = true;
2226 TypeOrExpr = Receiver.take();
2227 return false;
2228 }
2229
2230 // objc-receiver:
2231 // typename-specifier
2232 // simple-type-specifier
2233 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002234 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002235 ParseCXXSimpleTypeSpecifier(DS);
2236
2237 if (Tok.is(tok::l_paren)) {
2238 // If we see an opening parentheses at this point, we are
2239 // actually parsing an expression that starts with a
2240 // function-style cast, e.g.,
2241 //
2242 // postfix-expression:
2243 // simple-type-specifier ( expression-list [opt] )
2244 // typename-specifier ( expression-list [opt] )
2245 //
2246 // Parse the remainder of this case, then the (optional)
2247 // postfix-expression suffix, followed by the (optional)
2248 // right-hand side of the binary expression. We have an
2249 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002250 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002251 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002252 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002253 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002254 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002255 if (Receiver.isInvalid())
2256 return true;
2257
2258 IsExpr = true;
2259 TypeOrExpr = Receiver.take();
2260 return false;
2261 }
2262
2263 // We have a class message. Turn the simple-type-specifier or
2264 // typename-specifier we parsed into a type and parse the
2265 // remainder of the class message.
2266 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002267 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002268 if (Type.isInvalid())
2269 return true;
2270
2271 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002272 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002273 return false;
2274}
2275
Douglas Gregor1b730e82010-05-31 14:40:22 +00002276/// \brief Determine whether the parser is currently referring to a an
2277/// Objective-C message send, using a simplified heuristic to avoid overhead.
2278///
2279/// This routine will only return true for a subset of valid message-send
2280/// expressions.
2281bool Parser::isSimpleObjCMessageExpression() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002282 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002283 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002284 return GetLookAheadToken(1).is(tok::identifier) &&
2285 GetLookAheadToken(2).is(tok::identifier);
2286}
2287
Douglas Gregor9497a732010-09-16 01:51:54 +00002288bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002289 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregor9497a732010-09-16 01:51:54 +00002290 InMessageExpression)
2291 return false;
2292
2293
2294 ParsedType Type;
2295
2296 if (Tok.is(tok::annot_typename))
2297 Type = getTypeAnnotation(Tok);
2298 else if (Tok.is(tok::identifier))
2299 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2300 getCurScope());
2301 else
2302 return false;
2303
2304 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2305 const Token &AfterNext = GetLookAheadToken(2);
2306 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2307 if (Tok.is(tok::identifier))
2308 TryAnnotateTypeOrScopeToken();
2309
2310 return Tok.is(tok::annot_typename);
2311 }
2312 }
2313
2314 return false;
2315}
2316
Mike Stump1eb44332009-09-09 15:08:12 +00002317/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002318/// '[' objc-receiver objc-message-args ']'
2319///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002320/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002321/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002322/// expression
2323/// class-name
2324/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002325///
John McCall60d7b3a2010-08-24 06:29:42 +00002326ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002327 assert(Tok.is(tok::l_square) && "'[' expected");
2328 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2329
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002330 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002331 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002332 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002333 return ExprError();
2334 }
2335
Douglas Gregor0fbda682010-09-15 14:51:05 +00002336 InMessageExpressionRAIIObject InMessage(*this, true);
2337
David Blaikie4e4d0842012-03-11 07:00:24 +00002338 if (getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002339 // We completely separate the C and C++ cases because C++ requires
2340 // more complicated (read: slower) parsing.
2341
2342 // Handle send to super.
2343 // FIXME: This doesn't benefit from the same typo-correction we
2344 // get in Objective-C.
2345 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002346 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002347 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2348 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002349
2350 // Parse the receiver, which is either a type or an expression.
2351 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002352 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002353 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2354 SkipUntil(tok::r_square);
2355 return ExprError();
2356 }
2357
2358 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002359 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2360 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002361 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002362
2363 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002364 ParsedType::getFromOpaquePtr(TypeOrExpr),
2365 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002366 }
2367
2368 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002369 IdentifierInfo *Name = Tok.getIdentifierInfo();
2370 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002371 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002372 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002373 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002374 NextToken().is(tok::period),
2375 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002376 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002377 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2378 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002379
John McCallf312b1e2010-08-26 23:41:50 +00002380 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002381 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002382 SkipUntil(tok::r_square);
2383 return ExprError();
2384 }
2385
Douglas Gregor1569f952010-04-21 20:38:13 +00002386 ConsumeToken(); // the type name
2387
2388 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002389 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002390
John McCallf312b1e2010-08-26 23:41:50 +00002391 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002392 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002393 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002394 }
Chris Lattner699b6612008-01-25 18:59:06 +00002395 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002396
2397 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002398 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002399 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002400 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002401 return Res;
Chris Lattner699b6612008-01-25 18:59:06 +00002402 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002403
John McCallb3d87482010-08-24 05:47:05 +00002404 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2405 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002406}
Sebastian Redl1d922962008-12-13 15:32:12 +00002407
Douglas Gregor2725ca82010-04-21 19:57:20 +00002408/// \brief Parse the remainder of an Objective-C message following the
2409/// '[' objc-receiver.
2410///
2411/// This routine handles sends to super, class messages (sent to a
2412/// class name), and instance messages (sent to an object), and the
2413/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2414/// ReceiverExpr, respectively. Only one of these parameters may have
2415/// a valid value.
2416///
2417/// \param LBracLoc The location of the opening '['.
2418///
2419/// \param SuperLoc If this is a send to 'super', the location of the
2420/// 'super' keyword that indicates a send to the superclass.
2421///
2422/// \param ReceiverType If this is a class message, the type of the
2423/// class we are sending a message to.
2424///
2425/// \param ReceiverExpr If this is an instance message, the expression
2426/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002427///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002428/// objc-message-args:
2429/// objc-selector
2430/// objc-keywordarg-list
2431///
2432/// objc-keywordarg-list:
2433/// objc-keywordarg
2434/// objc-keywordarg-list objc-keywordarg
2435///
Mike Stump1eb44332009-09-09 15:08:12 +00002436/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002437/// selector-name[opt] ':' objc-keywordexpr
2438///
2439/// objc-keywordexpr:
2440/// nonempty-expr-list
2441///
2442/// nonempty-expr-list:
2443/// assignment-expression
2444/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002445///
John McCall60d7b3a2010-08-24 06:29:42 +00002446ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002447Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002448 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002449 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002450 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002451 InMessageExpressionRAIIObject InMessage(*this, true);
2452
Steve Naroffc4df6d22009-11-07 02:08:14 +00002453 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002454 if (SuperLoc.isValid())
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002455 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002456 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002457 else if (ReceiverType)
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002458 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002459 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002460 else
John McCall9ae2f072010-08-23 23:25:46 +00002461 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002462 None, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002463 cutOffParsing();
2464 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002465 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002466
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002467 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002468 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002469 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002470
Chris Lattner5f9e2722011-07-23 10:55:15 +00002471 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002472 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002473 ExprVector KeyExprs;
Steve Naroff68d331a2007-09-27 14:38:14 +00002474
Chris Lattnerdf195262007-10-09 17:51:17 +00002475 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002476 while (1) {
2477 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002478 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002479 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002480
Chris Lattnerdf195262007-10-09 17:51:17 +00002481 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002482 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002483 // We must manually skip to a ']', otherwise the expression skipper will
2484 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2485 // the enclosing expression.
2486 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002487 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002488 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002489
Steve Naroff68d331a2007-09-27 14:38:14 +00002490 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002491 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002492
2493 if (Tok.is(tok::code_completion)) {
2494 if (SuperLoc.isValid())
2495 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002496 KeyIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002497 /*AtArgumentEpression=*/true);
2498 else if (ReceiverType)
2499 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002500 KeyIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002501 /*AtArgumentEpression=*/true);
2502 else
2503 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002504 KeyIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002505 /*AtArgumentEpression=*/true);
2506
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002507 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002508 return ExprError();
2509 }
2510
Fariborz Jahanian0c7102f2013-04-18 23:43:21 +00002511 ExprResult Expr;
2512 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2513 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2514 Expr = ParseBraceInitializer();
2515 } else
2516 Expr = ParseAssignmentExpression();
2517
2518 ExprResult Res(Expr);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002519 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002520 // We must manually skip to a ']', otherwise the expression skipper will
2521 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2522 // the enclosing expression.
2523 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002524 return Res;
Steve Naroff37387c92007-09-17 20:25:27 +00002525 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002526
Steve Naroff37387c92007-09-17 20:25:27 +00002527 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002528 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002529
Douglas Gregord3c68542009-11-19 01:08:35 +00002530 // Code completion after each argument.
2531 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002532 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002533 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002534 KeyIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002535 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002536 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002537 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002538 KeyIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002539 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002540 else
John McCall9ae2f072010-08-23 23:25:46 +00002541 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002542 KeyIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002543 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002544 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002545 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002546 }
2547
Steve Naroff37387c92007-09-17 20:25:27 +00002548 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002549 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002550 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002551 break;
2552 // We have a selector or a colon, continue parsing.
2553 }
2554 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002555 while (Tok.is(tok::comma)) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002556 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002557 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002558 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002559 if (Res.isInvalid()) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002560 if (Tok.is(tok::colon)) {
2561 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2562 FixItHint::CreateRemoval(commaLoc);
2563 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002564 // We must manually skip to a ']', otherwise the expression skipper will
2565 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2566 // the enclosing expression.
2567 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002568 return Res;
Steve Naroff49f109c2007-11-15 13:05:42 +00002569 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002570
Steve Naroff49f109c2007-11-15 13:05:42 +00002571 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002572 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002573 }
2574 } else if (!selIdent) {
2575 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002576
Chris Lattner4fef81d2008-08-05 06:19:09 +00002577 // We must manually skip to a ']', otherwise the expression skipper will
2578 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2579 // the enclosing expression.
2580 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002581 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002582 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002583
Chris Lattnerdf195262007-10-09 17:51:17 +00002584 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002585 if (Tok.is(tok::identifier))
2586 Diag(Tok, diag::err_expected_colon);
2587 else
2588 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002589 // We must manually skip to a ']', otherwise the expression skipper will
2590 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2591 // the enclosing expression.
2592 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002593 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002594 }
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002595
Chris Lattner699b6612008-01-25 18:59:06 +00002596 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002597
Steve Naroff29238a02007-10-05 18:42:47 +00002598 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002599 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002600 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002601 KeyLocs.push_back(Loc);
2602 }
Chris Lattnerff384912007-10-07 02:00:24 +00002603 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002604
Douglas Gregor2725ca82010-04-21 19:57:20 +00002605 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002606 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002607 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002608 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002609 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002610 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCall9ae2f072010-08-23 23:25:46 +00002611 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002612 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002613}
2614
John McCall60d7b3a2010-08-24 06:29:42 +00002615ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2616 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002617 if (Res.isInvalid()) return Res;
Sebastian Redl1d922962008-12-13 15:32:12 +00002618
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002619 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2620 // expressions. At this point, we know that the only valid thing that starts
2621 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002622 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002623 ExprVector AtStrings;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002624 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002625 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002626
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002627 while (Tok.is(tok::at)) {
2628 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002629
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002630 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002631 if (!isTokenStringLiteral())
2632 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002633
John McCall60d7b3a2010-08-24 06:29:42 +00002634 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002635 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002636 return Lit;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002637
Sebastian Redleffa8d12008-12-10 00:02:53 +00002638 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002639 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002640
Nico Weberd32b94d2012-12-31 00:28:03 +00002641 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2642 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00002643}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002644
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002645/// ParseObjCBooleanLiteral -
2646/// objc-scalar-literal : '@' boolean-keyword
2647/// ;
2648/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2649/// ;
2650ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2651 bool ArgValue) {
2652 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2653 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2654}
2655
2656/// ParseObjCCharacterLiteral -
2657/// objc-scalar-literal : '@' character-literal
2658/// ;
2659ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2660 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2661 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002662 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002663 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002664 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002665 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002666}
2667
2668/// ParseObjCNumericLiteral -
2669/// objc-scalar-literal : '@' scalar-literal
2670/// ;
2671/// scalar-literal : | numeric-constant /* any numeric constant. */
2672/// ;
2673ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2674 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2675 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002676 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002677 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002678 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002679 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002680}
2681
Patrick Beardeb382ec2012-04-19 00:25:12 +00002682/// ParseObjCBoxedExpr -
2683/// objc-box-expression:
2684/// @( assignment-expression )
2685ExprResult
2686Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2687 if (Tok.isNot(tok::l_paren))
2688 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2689
2690 BalancedDelimiterTracker T(*this, tok::l_paren);
2691 T.consumeOpen();
2692 ExprResult ValueExpr(ParseAssignmentExpression());
2693 if (T.consumeClose())
2694 return ExprError();
Argyrios Kyrtzidisedd27602012-05-10 20:02:36 +00002695
2696 if (ValueExpr.isInvalid())
2697 return ExprError();
2698
Patrick Beardeb382ec2012-04-19 00:25:12 +00002699 // Wrap the sub-expression in a parenthesized expression, to distinguish
2700 // a boxed expression from a literal.
2701 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2702 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Weberd32b94d2012-12-31 00:28:03 +00002703 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2704 ValueExpr.take());
Patrick Beardeb382ec2012-04-19 00:25:12 +00002705}
2706
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002707ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002708 ExprVector ElementExprs; // array elements.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002709 ConsumeBracket(); // consume the l_square.
2710
2711 while (Tok.isNot(tok::r_square)) {
2712 // Parse list of array element expressions (all must be id types).
2713 ExprResult Res(ParseAssignmentExpression());
2714 if (Res.isInvalid()) {
2715 // We must manually skip to a ']', otherwise the expression skipper will
2716 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2717 // the enclosing expression.
2718 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002719 return Res;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002720 }
2721
2722 // Parse the ellipsis that indicates a pack expansion.
2723 if (Tok.is(tok::ellipsis))
2724 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2725 if (Res.isInvalid())
2726 return true;
2727
2728 ElementExprs.push_back(Res.release());
2729
2730 if (Tok.is(tok::comma))
2731 ConsumeToken(); // Eat the ','.
2732 else if (Tok.isNot(tok::r_square))
2733 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2734 }
2735 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002736 MultiExprArg Args(ElementExprs);
Nico Weberd32b94d2012-12-31 00:28:03 +00002737 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002738}
2739
2740ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2741 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2742 ConsumeBrace(); // consume the l_square.
2743 while (Tok.isNot(tok::r_brace)) {
2744 // Parse the comma separated key : value expressions.
2745 ExprResult KeyExpr;
2746 {
2747 ColonProtectionRAIIObject X(*this);
2748 KeyExpr = ParseAssignmentExpression();
2749 if (KeyExpr.isInvalid()) {
2750 // We must manually skip to a '}', otherwise the expression skipper will
2751 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2752 // the enclosing expression.
2753 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002754 return KeyExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002755 }
2756 }
2757
2758 if (Tok.is(tok::colon)) {
2759 ConsumeToken();
2760 } else {
Fariborz Jahanian0ac0ee92013-04-18 19:37:43 +00002761 Diag(Tok, diag::err_expected_colon);
2762 SkipUntil(tok::r_brace);
2763 return ExprError();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002764 }
2765
2766 ExprResult ValueExpr(ParseAssignmentExpression());
2767 if (ValueExpr.isInvalid()) {
2768 // We must manually skip to a '}', otherwise the expression skipper will
2769 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2770 // the enclosing expression.
2771 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002772 return ValueExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002773 }
2774
2775 // Parse the ellipsis that designates this as a pack expansion.
2776 SourceLocation EllipsisLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00002777 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002778 EllipsisLoc = ConsumeToken();
2779
2780 // We have a valid expression. Collect it in a vector so we can
2781 // build the argument list.
2782 ObjCDictionaryElement Element = {
David Blaikie66874fb2013-02-21 01:47:18 +00002783 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002784 };
2785 Elements.push_back(Element);
2786
2787 if (Tok.is(tok::comma))
2788 ConsumeToken(); // Eat the ','.
2789 else if (Tok.isNot(tok::r_brace))
2790 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2791 }
2792 SourceLocation EndLoc = ConsumeBrace();
2793
2794 // Create the ObjCDictionaryLiteral.
Nico Weberd32b94d2012-12-31 00:28:03 +00002795 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2796 Elements.data(), Elements.size());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002797}
2798
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002799/// objc-encode-expression:
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002800/// \@encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002801ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002802Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002803 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002804
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002805 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002806
Chris Lattner4fef81d2008-08-05 06:19:09 +00002807 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002808 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2809
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002810 BalancedDelimiterTracker T(*this, tok::l_paren);
2811 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002812
Douglas Gregor809070a2009-02-18 17:45:20 +00002813 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002814
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002815 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002816
Douglas Gregor809070a2009-02-18 17:45:20 +00002817 if (Ty.isInvalid())
2818 return ExprError();
2819
Nico Weberd32b94d2012-12-31 00:28:03 +00002820 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2821 Ty.get(), T.getCloseLocation());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002822}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002823
2824/// objc-protocol-expression
James Dennett17d26a62012-06-11 06:19:40 +00002825/// \@protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002826ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002827Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002828 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002829
Chris Lattner4fef81d2008-08-05 06:19:09 +00002830 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002831 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2832
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002833 BalancedDelimiterTracker T(*this, tok::l_paren);
2834 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002835
Chris Lattner4fef81d2008-08-05 06:19:09 +00002836 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002837 return ExprError(Diag(Tok, diag::err_expected_ident));
2838
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002839 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002840 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002841
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002842 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002843
Nico Weberd32b94d2012-12-31 00:28:03 +00002844 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2845 T.getOpenLocation(), ProtoIdLoc,
2846 T.getCloseLocation());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002847}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002848
2849/// objc-selector-expression
2850/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002851ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002852 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002853
Chris Lattner4fef81d2008-08-05 06:19:09 +00002854 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002855 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2856
Chris Lattner5f9e2722011-07-23 10:55:15 +00002857 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002858 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002859
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002860 BalancedDelimiterTracker T(*this, tok::l_paren);
2861 T.consumeOpen();
2862
Douglas Gregor458433d2010-08-26 15:07:07 +00002863 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002864 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002865 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002866 return ExprError();
2867 }
2868
Chris Lattner2fc5c242009-04-11 18:13:45 +00002869 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002870 if (!SelIdent && // missing selector name.
2871 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002872 return ExprError(Diag(Tok, diag::err_expected_ident));
2873
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002874 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002875 unsigned nColons = 0;
2876 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002877 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002878 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2879 ++nColons;
2880 KeyIdents.push_back(0);
2881 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002882 return ExprError(Diag(Tok, diag::err_expected_colon));
2883
Chris Lattner5add7542010-08-27 22:32:41 +00002884 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002885 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002886 if (Tok.is(tok::r_paren))
2887 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002888
2889 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko050315b2013-06-16 03:47:57 +00002890 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002891 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002892 return ExprError();
2893 }
2894
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002895 // Check for another keyword selector.
2896 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002897 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002898 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002899 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002900 break;
2901 }
Steve Naroff887407e2007-12-05 22:21:29 +00002902 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002903 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002904 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Weberd32b94d2012-12-31 00:28:03 +00002905 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2906 T.getOpenLocation(),
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002907 T.getCloseLocation());
Gabor Greif58065b22007-10-19 15:38:32 +00002908 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002909
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002910void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2911 // MCDecl might be null due to error in method or c-function prototype, etc.
2912 Decl *MCDecl = LM.D;
2913 bool skip = MCDecl &&
2914 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2915 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2916 if (skip)
2917 return;
2918
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002919 // Save the current token position.
2920 SourceLocation OrigLoc = Tok.getLocation();
2921
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002922 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2923 // Append the current token at the end of the new token stream so that it
2924 // doesn't get lost.
2925 LM.Toks.push_back(Tok);
2926 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2927
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002928 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00002929 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002930
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002931 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2932 Tok.is(tok::colon)) &&
2933 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002934 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002935 ParseScope BodyScope(this,
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002936 parseMethod
2937 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2938 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002939
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002940 // Tell the actions module that we have entered a method or c-function definition
2941 // with the specified Declarator for the method/function.
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +00002942 if (parseMethod)
2943 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2944 else
2945 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002946 if (Tok.is(tok::kw_try))
2947 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002948 else {
2949 if (Tok.is(tok::colon))
2950 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002951 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002952 }
Fariborz Jahanian69409722012-08-09 21:12:39 +00002953
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002954 if (Tok.getLocation() != OrigLoc) {
2955 // Due to parsing error, we either went over the cached tokens or
2956 // there are still cached tokens left. If it's the latter case skip the
2957 // leftover tokens.
2958 // Since this is an uncommon situation that should be avoided, use the
2959 // expensive isBeforeInTranslationUnit call.
2960 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2961 OrigLoc))
2962 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2963 ConsumeAnyToken();
2964 }
2965
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002966 return;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002967}