blob: fb0237ac05ea38ed2fb87eb3439c696d6e1f9567 [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
25
Chris Lattner891dca62008-12-08 21:53:24 +000026/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000027/// external-declaration: [C99 6.9]
28/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000029/// [OBJC] objc-class-declaration
30/// [OBJC] objc-alias-declaration
31/// [OBJC] objc-protocol-definition
32/// [OBJC] objc-method-definition
33/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000034Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000035 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000036
Douglas Gregorc464ae82009-12-07 09:27:33 +000037 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000038 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000039 cutOffParsing();
40 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000041 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000042
43 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000044 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000045 case tok::objc_class:
46 return ParseObjCAtClassDeclaration(AtLoc);
John McCall7f040a92010-12-24 02:08:15 +000047 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000048 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000049 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
50 break;
John McCall7f040a92010-12-24 02:08:15 +000051 }
52 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000053 ParsedAttributes attrs(AttrFactory);
Douglas Gregorbd9482d2012-01-01 21:23:57 +000054 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall7f040a92010-12-24 02:08:15 +000055 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000056 case tok::objc_implementation:
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +000057 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000058 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000059 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000060 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
62 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000063 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000064 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
65 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000066 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000067 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
68 break;
Douglas Gregor1b257af2012-12-11 22:11:52 +000069 case tok::objc_import:
David Blaikie4e4d0842012-03-11 07:00:24 +000070 if (getLangOpts().Modules)
Douglas Gregor94ad28b2012-01-03 18:24:14 +000071 return ParseModuleImport(AtLoc);
72
73 // Fall through
74
Chris Lattner5ffb14b2008-08-23 02:02:23 +000075 default:
76 Diag(AtLoc, diag::err_unexpected_at);
77 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000078 SingleDecl = 0;
79 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000080 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000081 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000082}
83
84///
Mike Stump1eb44332009-09-09 15:08:12 +000085/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000086/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000087///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000088Parser::DeclGroupPtrTy
89Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000091 SmallVector<IdentifierInfo *, 8> ClassNames;
92 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000093
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000096 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 Diag(Tok, diag::err_expected_ident);
98 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000099 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +0000102 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattnerdf195262007-10-09 17:51:17 +0000105 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 ConsumeToken();
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // Consume the ';'.
112 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000113 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenekc09cba62009-11-17 23:12:20 +0000115 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
116 ClassLocs.data(),
117 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000118}
119
Erik Verbruggend64251f2011-12-06 09:25:23 +0000120void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
121{
122 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
123 if (ock == Sema::OCK_None)
124 return;
125
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000126 Decl *Decl = Actions.getObjCDeclContext();
127 if (CurParsedObjCImpl) {
128 CurParsedObjCImpl->finish(AtLoc);
129 } else {
130 Actions.ActOnAtEnd(getCurScope(), AtLoc);
131 }
Erik Verbruggend64251f2011-12-06 09:25:23 +0000132 Diag(AtLoc, diag::err_objc_missing_end)
133 << FixItHint::CreateInsertion(AtLoc, "@end\n");
134 if (Decl)
135 Diag(Decl->getLocStart(), diag::note_objc_container_start)
136 << (int) ock;
Erik Verbruggend64251f2011-12-06 09:25:23 +0000137}
138
Steve Naroffdac269b2007-08-20 21:31:48 +0000139///
140/// objc-interface:
141/// objc-class-interface-attributes[opt] objc-class-interface
142/// objc-category-interface
143///
144/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000145/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000146/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000147/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000148/// objc-interface-decl-list
149/// @end
150///
151/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000152/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000153/// objc-protocol-refs[opt]
154/// objc-interface-decl-list
155/// @end
156///
157/// objc-superclass:
158/// ':' identifier
159///
160/// objc-class-interface-attributes:
161/// __attribute__((visibility("default")))
162/// __attribute__((visibility("hidden")))
163/// __attribute__((deprecated))
164/// __attribute__((unavailable))
165/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardb2f68202012-04-06 18:12:22 +0000166/// __attribute__((objc_root_class))
Steve Naroffdac269b2007-08-20 21:31:48 +0000167///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000168Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000169 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000170 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000172 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000173 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000175 // Code completion after '@interface'.
176 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000177 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000178 cutOffParsing();
179 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000180 }
181
Chris Lattnerdf195262007-10-09 17:51:17 +0000182 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000183 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000184 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000185 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000186
Steve Naroffdac269b2007-08-20 21:31:48 +0000187 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000188 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000189 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000190 if (Tok.is(tok::l_paren) &&
191 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000192
193 BalancedDelimiterTracker T(*this, tok::l_paren);
194 T.consumeOpen();
195
196 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000197 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000198 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000199 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000200 cutOffParsing();
201 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000202 }
203
Steve Naroff527fe232007-08-23 19:56:30 +0000204 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000205 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000206 categoryId = Tok.getIdentifierInfo();
207 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000208 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000209 else if (!getLangOpts().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000210 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000211 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000212 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000213
214 T.consumeClose();
215 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000216 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000217
218 if (!attrs.empty()) { // categories don't support attributes.
219 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
220 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000221 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000222
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000223 // Next, we need to check for any protocol references.
224 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 SmallVector<Decl *, 8> ProtocolRefs;
226 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000227 if (Tok.is(tok::less) &&
228 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000229 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000230 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
John McCalld226f652010-08-21 09:40:31 +0000232 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000233 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000234 nameId, nameLoc,
235 categoryId, categoryLoc,
236 ProtocolRefs.data(),
237 ProtocolRefs.size(),
238 ProtocolLocs.data(),
239 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000240
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000241 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000242 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000243
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000244 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000245 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000246 }
247 // Parse a class interface.
248 IdentifierInfo *superClassId = 0;
249 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000250
Chris Lattnerdf195262007-10-09 17:51:17 +0000251 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000252 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000253
254 // Code completion of superclass names.
255 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000256 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000257 cutOffParsing();
258 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000259 }
260
Chris Lattnerdf195262007-10-09 17:51:17 +0000261 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000262 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000263 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000264 }
265 superClassId = Tok.getIdentifierInfo();
266 superClassLoc = ConsumeToken();
267 }
268 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269 SmallVector<Decl *, 8> ProtocolRefs;
270 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000271 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000272 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000273 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
274 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000275 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
John McCalld226f652010-08-21 09:40:31 +0000277 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000278 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000279 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000280 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000281 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000282 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattnerdf195262007-10-09 17:51:17 +0000284 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000285 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000286
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000287 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000288 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000289}
290
John McCalld0014542009-12-03 22:31:13 +0000291/// The Objective-C property callback. This should be defined where
292/// it's used, but instead it's been lifted to here to support VS2005.
293struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000294private:
295 virtual void anchor();
296public:
John McCalld0014542009-12-03 22:31:13 +0000297 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000299 ObjCDeclSpec &OCDS;
300 SourceLocation AtLoc;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000301 SourceLocation LParenLoc;
John McCalld0014542009-12-03 22:31:13 +0000302 tok::ObjCKeywordKind MethodImplKind;
303
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000304 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000305 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000306 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000307 SourceLocation LParenLoc,
John McCalld0014542009-12-03 22:31:13 +0000308 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000309 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCalld0014542009-12-03 22:31:13 +0000310 MethodImplKind(MethodImplKind) {
311 }
312
Eli Friedmandcdff462012-08-08 23:53:27 +0000313 void invoke(ParsingFieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000314 if (FD.D.getIdentifier() == 0) {
315 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
316 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000317 return;
John McCalld0014542009-12-03 22:31:13 +0000318 }
319 if (FD.BitfieldSize) {
320 P.Diag(AtLoc, diag::err_objc_property_bitfield)
321 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000322 return;
John McCalld0014542009-12-03 22:31:13 +0000323 }
324
325 // Install the property declarator into interfaceDecl.
326 IdentifierInfo *SelName =
327 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
328
329 Selector GetterSel =
330 P.PP.getSelectorTable().getNullarySelector(SelName);
331 IdentifierInfo *SetterName = OCDS.getSetterName();
332 Selector SetterSel;
333 if (SetterName)
334 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
335 else
336 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
337 P.PP.getSelectorTable(),
338 FD.D.getIdentifier());
339 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000340 Decl *Property =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000341 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
342 FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000343 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000344 &isOverridingProperty,
345 MethodImplKind);
346 if (!isOverridingProperty)
347 Props.push_back(Property);
348
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000349 FD.complete(Property);
John McCalld0014542009-12-03 22:31:13 +0000350 }
351};
352
David Blaikie99ba9e32011-12-20 02:48:34 +0000353void Parser::ObjCPropertyCallback::anchor() {
354}
355
Steve Naroffdac269b2007-08-20 21:31:48 +0000356/// objc-interface-decl-list:
357/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000358/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000359/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000360/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000361/// objc-interface-decl-list declaration
362/// objc-interface-decl-list ';'
363///
Steve Naroff294494e2007-08-22 16:35:03 +0000364/// objc-method-requirement: [OBJC2]
365/// @required
366/// @optional
367///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000368void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
369 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000370 SmallVector<Decl *, 32> allMethods;
371 SmallVector<Decl *, 16> allProperties;
372 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000373 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek782f2f52010-01-07 01:20:12 +0000375 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000376
Steve Naroff294494e2007-08-22 16:35:03 +0000377 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000378 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000379 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniand30ec702012-07-26 17:32:28 +0000380 if (Decl *methodPrototype =
381 ParseObjCMethodPrototype(MethodImplKind, false))
382 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000383 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
384 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000385 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
386 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
387 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
388 if (Tok.is(tok::semi))
389 ConsumeToken();
390 }
Steve Naroff294494e2007-08-22 16:35:03 +0000391 continue;
392 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000393 if (Tok.is(tok::l_paren)) {
394 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000395 ParseObjCMethodDecl(Tok.getLocation(),
396 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000397 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000398 continue;
399 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000400 // Ignore excess semicolons.
401 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000402 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000403 continue;
404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattnerbc662af2008-10-20 06:10:06 +0000406 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000407 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000408 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000410 // Code completion within an Objective-C interface.
411 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000412 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000413 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallf312b1e2010-08-26 23:41:50 +0000414 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000415 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000416 }
417
Chris Lattnere82a10f2008-10-20 05:46:22 +0000418 // If we don't have an @ directive, parse it as a function definition.
419 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000420 // The code below does not consume '}'s because it is afraid of eating the
421 // end of a namespace. Because of the way this code is structured, an
422 // erroneous r_brace would cause an infinite loop if not handled here.
423 if (Tok.is(tok::r_brace))
424 break;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000425 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000426 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000427 continue;
428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnere82a10f2008-10-20 05:46:22 +0000430 // Otherwise, we have an @ directive, eat the @.
431 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000432 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000433 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000434 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000435 }
436
Chris Lattnera2449b22008-10-20 05:57:40 +0000437 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnera2449b22008-10-20 05:57:40 +0000439 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000440 AtEnd.setBegin(AtLoc);
441 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000442 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000443 } else if (DirectiveKind == tok::objc_not_keyword) {
444 Diag(Tok, diag::err_objc_unknown_at);
445 SkipUntil(tok::semi);
446 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattnerbc662af2008-10-20 06:10:06 +0000449 // Eat the identifier.
450 ConsumeToken();
451
Chris Lattnera2449b22008-10-20 05:57:40 +0000452 switch (DirectiveKind) {
453 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000454 // FIXME: If someone forgets an @end on a protocol, this loop will
455 // continue to eat up tons of stuff and spew lots of nonsense errors. It
456 // would probably be better to bail out if we saw an @class or @interface
457 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000458 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000459 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000460 SkipUntil(tok::r_brace, tok::at);
461 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000462
463 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000464 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000465 Diag(AtLoc, diag::err_objc_missing_end)
466 << FixItHint::CreateInsertion(AtLoc, "@end\n");
467 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
468 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000469 ConsumeToken();
470 break;
471
Chris Lattnera2449b22008-10-20 05:57:40 +0000472 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000473 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000474 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000475 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000476 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000477 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000478 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000479 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000480 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattnera2449b22008-10-20 05:57:40 +0000482 case tok::objc_property:
David Blaikie4e4d0842012-03-11 07:00:24 +0000483 if (!getLangOpts().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000484 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000485
Chris Lattnere82a10f2008-10-20 05:46:22 +0000486 ObjCDeclSpec OCDS;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000487 SourceLocation LParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000488 // Parse property attribute list, if any.
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000489 if (Tok.is(tok::l_paren)) {
490 LParenLoc = Tok.getLocation();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000491 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000494 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000495 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000496
Chris Lattnere82a10f2008-10-20 05:46:22 +0000497 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000498 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +0000499 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
John McCall7da19ea2011-03-26 01:53:26 +0000501 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000502 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000503 }
Steve Naroff294494e2007-08-22 16:35:03 +0000504 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000505
506 // We break out of the big loop in two cases: when we see @end or when we see
507 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000508 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000509 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000510 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000511 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000512 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000513 } else {
514 Diag(Tok, diag::err_objc_missing_end)
515 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
516 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
517 << (int) Actions.getObjCContainerKind();
518 AtEnd.setBegin(Tok.getLocation());
519 AtEnd.setEnd(Tok.getLocation());
520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattnera2449b22008-10-20 05:57:40 +0000522 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000523 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000524 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000525 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000526 allProperties.data(), allProperties.size(),
527 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000528}
529
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000530/// Parse property attribute declarations.
531///
532/// property-attr-decl: '(' property-attrlist ')'
533/// property-attrlist:
534/// property-attribute
535/// property-attrlist ',' property-attribute
536/// property-attribute:
537/// getter '=' identifier
538/// setter '=' identifier ':'
539/// readonly
540/// readwrite
541/// assign
542/// retain
543/// copy
544/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000545/// atomic
546/// strong
547/// weak
548/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000549///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000550void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000551 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000552 BalancedDelimiterTracker T(*this, tok::l_paren);
553 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000555 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000556 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000557 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000558 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000559 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000560 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000562 // If this is not an identifier at all, bail out early.
563 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000564 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000565 return;
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner156b0612008-10-20 07:37:22 +0000568 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner92e62b02008-11-20 04:42:34 +0000570 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000572 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000574 else if (II->isStr("unsafe_unretained"))
575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000576 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000578 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000580 else if (II->isStr("strong"))
581 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000582 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000583 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000584 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000585 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000586 else if (II->isStr("atomic"))
587 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000588 else if (II->isStr("weak"))
589 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000590 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000591 bool IsSetter = II->getNameStart()[0] == 's';
592
Chris Lattnere00da7c2008-10-20 07:39:53 +0000593 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000594 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
595 diag::err_objc_expected_equal_for_getter;
596
597 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000598 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Douglas Gregor4ad96852009-11-19 07:41:15 +0000600 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000601 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000602 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000603 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000604 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000605 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000606 }
607
Anders Carlsson42499be2010-10-02 17:45:21 +0000608
609 SourceLocation SelLoc;
610 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
611
612 if (!SelIdent) {
613 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
614 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000615 SkipUntil(tok::r_paren);
616 return;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Anders Carlsson42499be2010-10-02 17:45:21 +0000619 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000620 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000621 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000623 if (ExpectAndConsume(tok::colon,
624 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000625 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000626 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000627 } else {
628 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000629 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000630 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000631 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000632 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000633 SkipUntil(tok::r_paren);
634 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Chris Lattner156b0612008-10-20 07:37:22 +0000637 if (Tok.isNot(tok::comma))
638 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Chris Lattner156b0612008-10-20 07:37:22 +0000640 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000643 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000644}
645
Steve Naroff3536b442007-09-06 21:24:23 +0000646/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000647/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000648/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000649///
650/// objc-instance-method: '-'
651/// objc-class-method: '+'
652///
Steve Naroff4985ace2007-08-22 18:35:33 +0000653/// objc-method-attributes: [OBJC2]
654/// __attribute__((deprecated))
655///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000656Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000657 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000658 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000659
Mike Stump1eb44332009-09-09 15:08:12 +0000660 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000661 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000662 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000663 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000664 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000665 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000666 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000667}
668
669/// objc-selector:
670/// identifier
671/// one of
672/// enum struct union if else while do for switch case default
673/// break continue return goto asm sizeof typeof __alignof
674/// unsigned long const short volatile signed restrict _Complex
675/// in out inout bycopy byref oneway int char float double void _Bool
676///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000677IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000678
Chris Lattnerff384912007-10-07 02:00:24 +0000679 switch (Tok.getKind()) {
680 default:
681 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000682 case tok::ampamp:
683 case tok::ampequal:
684 case tok::amp:
685 case tok::pipe:
686 case tok::tilde:
687 case tok::exclaim:
688 case tok::exclaimequal:
689 case tok::pipepipe:
690 case tok::pipeequal:
691 case tok::caret:
692 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000693 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000694 if (isLetter(ThisTok[0])) {
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000695 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
696 Tok.setKind(tok::identifier);
697 SelectorLoc = ConsumeToken();
698 return II;
699 }
700 return 0;
701 }
702
Chris Lattnerff384912007-10-07 02:00:24 +0000703 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000704 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000705 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000706 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000707 case tok::kw_break:
708 case tok::kw_case:
709 case tok::kw_catch:
710 case tok::kw_char:
711 case tok::kw_class:
712 case tok::kw_const:
713 case tok::kw_const_cast:
714 case tok::kw_continue:
715 case tok::kw_default:
716 case tok::kw_delete:
717 case tok::kw_do:
718 case tok::kw_double:
719 case tok::kw_dynamic_cast:
720 case tok::kw_else:
721 case tok::kw_enum:
722 case tok::kw_explicit:
723 case tok::kw_export:
724 case tok::kw_extern:
725 case tok::kw_false:
726 case tok::kw_float:
727 case tok::kw_for:
728 case tok::kw_friend:
729 case tok::kw_goto:
730 case tok::kw_if:
731 case tok::kw_inline:
732 case tok::kw_int:
733 case tok::kw_long:
734 case tok::kw_mutable:
735 case tok::kw_namespace:
736 case tok::kw_new:
737 case tok::kw_operator:
738 case tok::kw_private:
739 case tok::kw_protected:
740 case tok::kw_public:
741 case tok::kw_register:
742 case tok::kw_reinterpret_cast:
743 case tok::kw_restrict:
744 case tok::kw_return:
745 case tok::kw_short:
746 case tok::kw_signed:
747 case tok::kw_sizeof:
748 case tok::kw_static:
749 case tok::kw_static_cast:
750 case tok::kw_struct:
751 case tok::kw_switch:
752 case tok::kw_template:
753 case tok::kw_this:
754 case tok::kw_throw:
755 case tok::kw_true:
756 case tok::kw_try:
757 case tok::kw_typedef:
758 case tok::kw_typeid:
759 case tok::kw_typename:
760 case tok::kw_typeof:
761 case tok::kw_union:
762 case tok::kw_unsigned:
763 case tok::kw_using:
764 case tok::kw_virtual:
765 case tok::kw_void:
766 case tok::kw_volatile:
767 case tok::kw_wchar_t:
768 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000769 case tok::kw__Bool:
770 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000771 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000772 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000773 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000774 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000775 }
Steve Naroff294494e2007-08-22 16:35:03 +0000776}
777
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000778/// objc-for-collection-in: 'in'
779///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000780bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000781 // FIXME: May have to do additional look-ahead to only allow for
782 // valid tokens following an 'in'; such as an identifier, unary operators,
783 // '[' etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000784 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000785 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000786}
787
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000788/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000789/// qualifier list and builds their bitmask representation in the input
790/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000791///
792/// objc-type-qualifiers:
793/// objc-type-qualifier
794/// objc-type-qualifiers objc-type-qualifier
795///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000796void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000797 Declarator::TheContext Context) {
798 assert(Context == Declarator::ObjCParameterContext ||
799 Context == Declarator::ObjCResultContext);
800
Chris Lattnere8b724d2007-12-12 06:56:32 +0000801 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000802 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000803 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000804 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000805 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000806 }
807
Chris Lattnercb53b362007-12-27 19:57:00 +0000808 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000809 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Chris Lattnere8b724d2007-12-12 06:56:32 +0000811 const IdentifierInfo *II = Tok.getIdentifierInfo();
812 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000814 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000817 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000818 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000819 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
820 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
821 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
822 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
823 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
824 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000825 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000826 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000827 ConsumeToken();
828 II = 0;
829 break;
830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattnere8b724d2007-12-12 06:56:32 +0000832 // If this wasn't a recognized qualifier, bail out.
833 if (II) return;
834 }
835}
836
John McCallcdda47f2011-10-01 09:56:14 +0000837/// Take all the decl attributes out of the given list and add
838/// them to the given attribute set.
839static void takeDeclAttributes(ParsedAttributes &attrs,
840 AttributeList *list) {
841 while (list) {
842 AttributeList *cur = list;
843 list = cur->getNext();
844
845 if (!cur->isUsedAsTypeAttr()) {
846 // Clear out the next pointer. We're really completely
847 // destroying the internal invariants of the declarator here,
848 // but it doesn't matter because we're done with it.
849 cur->setNext(0);
850 attrs.add(cur);
851 }
852 }
853}
854
855/// takeDeclAttributes - Take all the decl attributes from the given
856/// declarator and add them to the given list.
857static void takeDeclAttributes(ParsedAttributes &attrs,
858 Declarator &D) {
859 // First, take ownership of all attributes.
860 attrs.getPool().takeAllFrom(D.getAttributePool());
861 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
862
863 // Now actually move the attributes over.
864 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
865 takeDeclAttributes(attrs, D.getAttributes());
866 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
867 takeDeclAttributes(attrs,
868 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
869}
870
Chris Lattnere8b724d2007-12-12 06:56:32 +0000871/// objc-type-name:
872/// '(' objc-type-qualifiers[opt] type-name ')'
873/// '(' objc-type-qualifiers[opt] ')'
874///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000875ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000876 Declarator::TheContext context,
877 ParsedAttributes *paramAttrs) {
878 assert(context == Declarator::ObjCParameterContext ||
879 context == Declarator::ObjCResultContext);
880 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
881
Chris Lattnerdf195262007-10-09 17:51:17 +0000882 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000884 BalancedDelimiterTracker T(*this, tok::l_paren);
885 T.consumeOpen();
886
Chris Lattnere8904e92008-08-23 01:48:03 +0000887 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000888 ObjCDeclContextSwitch ObjCDC(*this);
889
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000890 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000891 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000892
John McCallb3d87482010-08-24 05:47:05 +0000893 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000894 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000895 // Parse an abstract declarator.
896 DeclSpec declSpec(AttrFactory);
897 declSpec.setObjCQualifiers(&DS);
898 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanian6d1de1b2012-05-29 21:52:45 +0000899 declSpec.SetRangeEnd(Tok.getLocation());
John McCallcdda47f2011-10-01 09:56:14 +0000900 Declarator declarator(declSpec, context);
901 ParseDeclarator(declarator);
902
903 // If that's not invalid, extract a type.
904 if (!declarator.isInvalidType()) {
905 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
906 if (!type.isInvalid())
907 Ty = type.get();
908
909 // If we're parsing a parameter, steal all the decl attributes
910 // and add them to the decl spec.
911 if (context == Declarator::ObjCParameterContext)
912 takeDeclAttributes(*paramAttrs, declarator);
913 }
914 } else if (context == Declarator::ObjCResultContext &&
915 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000916 if (!Ident_instancetype)
917 Ident_instancetype = PP.getIdentifierInfo("instancetype");
918
919 if (Tok.getIdentifierInfo() == Ident_instancetype) {
920 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
921 ConsumeToken();
922 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000923 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000924
Steve Naroffd7333c22008-10-21 14:15:04 +0000925 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000926 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000927 else if (Tok.getLocation() == TypeStartLoc) {
928 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000929 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000930 SkipUntil(tok::r_paren);
931 } else {
932 // Otherwise, we found *something*, but didn't get a ')' in the right
933 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000934 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000935 }
Steve Narofff28b2642007-09-05 23:30:30 +0000936 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000937}
938
939/// objc-method-decl:
940/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000941/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000942/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000943/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000944///
945/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000946/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000947/// objc-keyword-selector objc-keyword-decl
948///
949/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000950/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
951/// objc-selector ':' objc-keyword-attributes[opt] identifier
952/// ':' objc-type-name objc-keyword-attributes[opt] identifier
953/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000954///
Steve Naroff4985ace2007-08-22 18:35:33 +0000955/// objc-parmlist:
956/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000957///
Steve Naroff4985ace2007-08-22 18:35:33 +0000958/// objc-parms:
959/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000960///
Steve Naroff4985ace2007-08-22 18:35:33 +0000961/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000962/// , ...
963///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000964/// objc-keyword-attributes: [OBJC2]
965/// __attribute__((unused))
966///
John McCalld226f652010-08-21 09:40:31 +0000967Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000968 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000969 tok::ObjCKeywordKind MethodImplKind,
970 bool MethodDefinition) {
John McCall92576642012-05-07 06:16:41 +0000971 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall54abf7d2009-11-04 02:18:39 +0000972
Douglas Gregore8f5a172010-04-07 00:21:17 +0000973 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000974 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000975 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000976 cutOffParsing();
977 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000978 }
979
Chris Lattnere8904e92008-08-23 01:48:03 +0000980 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000981 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000983 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000984 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek9e049352010-02-18 23:05:16 +0000986 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000987 ParsedAttributes methodAttrs(AttrFactory);
David Blaikie4e4d0842012-03-11 07:00:24 +0000988 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000989 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000990
Douglas Gregore8f5a172010-04-07 00:21:17 +0000991 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000992 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000993 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000994 cutOffParsing();
995 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000996 }
997
Ted Kremenek9e049352010-02-18 23:05:16 +0000998 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000999 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001000 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +00001001
Steve Naroff84c43102009-02-11 20:43:13 +00001002 // An unnamed colon is valid.
1003 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001004 Diag(Tok, diag::err_expected_selector_for_method)
1005 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniand30ec702012-07-26 17:32:28 +00001006 // Skip until we get a ; or @.
1007 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCalld226f652010-08-21 09:40:31 +00001008 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +00001009 }
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Chris Lattner5f9e2722011-07-23 10:55:15 +00001011 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001012 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001013 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001014 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001015 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattnerff384912007-10-07 02:00:24 +00001017 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001018 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001019 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001020 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001021 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001022 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001023 methodAttrs.getList(), MethodImplKind,
1024 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001025 PD.complete(Result);
1026 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001027 }
Steve Narofff28b2642007-09-05 23:30:30 +00001028
Chris Lattner5f9e2722011-07-23 10:55:15 +00001029 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001030 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001031 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smith3a2b7a12013-01-28 22:42:45 +00001032 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1033 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001034
1035 AttributePool allParamAttrs(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001036 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001037 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001038 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattnerff384912007-10-07 02:00:24 +00001040 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001041 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001042 Diag(Tok, diag::err_expected_colon);
1043 break;
1044 }
1045 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001046
John McCallb3d87482010-08-24 05:47:05 +00001047 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001048 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001049 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1050 Declarator::ObjCParameterContext,
1051 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001052
Chris Lattnerff384912007-10-07 02:00:24 +00001053 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001054 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001055 ArgInfo.ArgAttrs = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001056 if (getLangOpts().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001057 MaybeParseGNUAttributes(paramAttrs);
1058 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001059 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001060
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001061 // Code completion for the next piece of the selector.
1062 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001063 KeyIdents.push_back(SelIdent);
1064 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1065 mType == tok::minus,
1066 /*AtParameterName=*/true,
1067 ReturnType,
1068 KeyIdents.data(),
1069 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001070 cutOffParsing();
1071 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001072 }
1073
Chris Lattnerdf195262007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001075 Diag(Tok, diag::err_expected_ident); // missing argument name.
1076 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattnere294d3f2009-04-11 18:57:04 +00001079 ArgInfo.Name = Tok.getIdentifierInfo();
1080 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001081 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattnere294d3f2009-04-11 18:57:04 +00001083 ArgInfos.push_back(ArgInfo);
1084 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001085 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001086
John McCall0b7e6782011-03-24 11:26:52 +00001087 // Make sure the attributes persist.
1088 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1089
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001090 // Code completion for the next piece of the selector.
1091 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001092 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1093 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001094 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001095 ReturnType,
1096 KeyIdents.data(),
1097 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001098 cutOffParsing();
1099 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001100 }
1101
Chris Lattnerff384912007-10-07 02:00:24 +00001102 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001103 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenekb648cf02012-09-12 16:50:35 +00001104 if (!SelIdent && Tok.isNot(tok::colon))
1105 break;
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001106 if (!SelIdent) {
Fariborz Jahanian08602352012-09-17 19:15:26 +00001107 SourceLocation ColonLoc = Tok.getLocation();
1108 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001109 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1110 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1111 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanian08602352012-09-17 19:15:26 +00001112 }
1113 }
Chris Lattnerff384912007-10-07 02:00:24 +00001114 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Steve Naroff335eafa2007-11-15 12:35:21 +00001117 bool isVariadic = false;
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001118 bool cStyleParamWarned = false;
Chris Lattnerff384912007-10-07 02:00:24 +00001119 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001120 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001121 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001122 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001123 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001124 ConsumeToken();
1125 break;
1126 }
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001127 if (!cStyleParamWarned) {
1128 Diag(Tok, diag::warn_cstyle_param);
1129 cStyleParamWarned = true;
1130 }
John McCall0b7e6782011-03-24 11:26:52 +00001131 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001132 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001133 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001134 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1135 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001136 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001137 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001138 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1139 ParmDecl.getIdentifierLoc(),
1140 Param,
1141 0));
Chris Lattnerff384912007-10-07 02:00:24 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001144 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001145 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001146 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001147 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001148
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001149 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001150 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001151
Chris Lattnerff384912007-10-07 02:00:24 +00001152 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1153 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001154 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001155 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001156 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001157 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001158 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001159 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001160 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001161
John McCall54abf7d2009-11-04 02:18:39 +00001162 PD.complete(Result);
1163 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001164}
1165
Steve Naroffdac269b2007-08-20 21:31:48 +00001166/// objc-protocol-refs:
1167/// '<' identifier-list '>'
1168///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001169bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001170ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1171 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001172 bool WarnOnDeclarations,
1173 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001174 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001176 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Chris Lattner5f9e2722011-07-23 10:55:15 +00001178 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattnere13b9592008-07-26 04:03:38 +00001180 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001181 if (Tok.is(tok::code_completion)) {
1182 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1183 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001184 cutOffParsing();
1185 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001186 }
1187
Chris Lattnere13b9592008-07-26 04:03:38 +00001188 if (Tok.isNot(tok::identifier)) {
1189 Diag(Tok, diag::err_expected_ident);
1190 SkipUntil(tok::greater);
1191 return true;
1192 }
1193 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1194 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001195 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001196 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Chris Lattnere13b9592008-07-26 04:03:38 +00001198 if (Tok.isNot(tok::comma))
1199 break;
1200 ConsumeToken();
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattnere13b9592008-07-26 04:03:38 +00001203 // Consume the '>'.
Nico Weberb707a472012-12-14 18:22:38 +00001204 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattnere13b9592008-07-26 04:03:38 +00001205 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Chris Lattnere13b9592008-07-26 04:03:38 +00001207 // Convert the list of protocols identifiers into a list of protocol decls.
1208 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1209 &ProtocolIdents[0], ProtocolIdents.size(),
1210 Protocols);
1211 return false;
1212}
1213
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001214/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1215/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001216bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001217 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikie4e4d0842012-03-11 07:00:24 +00001218 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001219 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001220 SmallVector<Decl *, 8> ProtocolDecl;
1221 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001222 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1223 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001224 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1225 ProtocolLocs.data(), LAngleLoc);
1226 if (EndProtoLoc.isValid())
1227 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001228 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001229}
1230
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001231void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1232 BalancedDelimiterTracker &T,
1233 SmallVectorImpl<Decl *> &AllIvarDecls,
1234 bool RBraceMissing) {
1235 if (!RBraceMissing)
1236 T.consumeClose();
1237
1238 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1239 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1240 Actions.ActOnObjCContainerFinishDefinition();
1241 // Call ActOnFields() even if we don't have any decls. This is useful
1242 // for code rewriting tools that need to be aware of the empty list.
1243 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1244 AllIvarDecls,
1245 T.getOpenLocation(), T.getCloseLocation(), 0);
1246}
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001247
Steve Naroffdac269b2007-08-20 21:31:48 +00001248/// objc-class-instance-variables:
1249/// '{' objc-instance-variable-decl-list[opt] '}'
1250///
1251/// objc-instance-variable-decl-list:
1252/// objc-visibility-spec
1253/// objc-instance-variable-decl ';'
1254/// ';'
1255/// objc-instance-variable-decl-list objc-visibility-spec
1256/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1257/// objc-instance-variable-decl-list ';'
1258///
1259/// objc-visibility-spec:
1260/// @private
1261/// @protected
1262/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001263/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001264///
1265/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001266/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001267///
John McCalld226f652010-08-21 09:40:31 +00001268void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001269 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001270 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001271 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001272 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001273
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001274 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001275 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001276
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001277 BalancedDelimiterTracker T(*this, tok::l_brace);
1278 T.consumeOpen();
Steve Naroffddbff782007-08-21 21:17:12 +00001279 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001280 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001281 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Steve Naroffddbff782007-08-21 21:17:12 +00001283 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001284 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001285 ConsumeExtraSemi(InstanceVariableList);
Steve Naroffddbff782007-08-21 21:17:12 +00001286 continue;
1287 }
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Steve Naroffddbff782007-08-21 21:17:12 +00001289 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001290 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001291 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001292
1293 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001294 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001295 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001296 }
1297
Steve Naroff861cf3e2007-08-23 18:16:40 +00001298 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001299 case tok::objc_private:
1300 case tok::objc_public:
1301 case tok::objc_protected:
1302 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001303 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001304 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001305 continue;
Fariborz Jahanian695031c2013-03-20 18:45:49 +00001306
1307 case tok::objc_end:
1308 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001309 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1310 Tok.setKind(tok::at);
1311 Tok.setLength(1);
1312 PP.EnterToken(Tok);
1313 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1314 T, AllIvarDecls, true);
1315 return;
Fariborz Jahanian695031c2013-03-20 18:45:49 +00001316
1317 default:
1318 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1319 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001320 }
1321 }
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001323 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001324 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001325 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001326 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001327 }
1328
John McCallbdd563e2009-11-03 02:38:08 +00001329 struct ObjCIvarCallback : FieldCallback {
1330 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001331 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001332 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001333 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001334
John McCalld226f652010-08-21 09:40:31 +00001335 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001336 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001337 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1338 }
1339
Eli Friedmandcdff462012-08-08 23:53:27 +00001340 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001341 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001342 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001343 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001344 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001345 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001346 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001347 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001348 if (Field)
1349 AllIvarDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001350 FD.complete(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001351 }
1352 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001353
Chris Lattnere1359422008-04-10 06:46:29 +00001354 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001355 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001356 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Chris Lattnerdf195262007-10-09 17:51:17 +00001358 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001359 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001360 } else {
1361 Diag(Tok, diag::err_expected_semi_decl_list);
1362 // Skip to end of block or statement
1363 SkipUntil(tok::r_brace, true, true);
1364 }
1365 }
Fariborz Jahanian4cc0cf12013-03-20 18:09:33 +00001366 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1367 T, AllIvarDecls, false);
Steve Naroffddbff782007-08-21 21:17:12 +00001368 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001369}
Steve Naroffdac269b2007-08-20 21:31:48 +00001370
1371/// objc-protocol-declaration:
1372/// objc-protocol-definition
1373/// objc-protocol-forward-reference
1374///
1375/// objc-protocol-definition:
James Dennett17d26a62012-06-11 06:19:40 +00001376/// \@protocol identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001377/// objc-protocol-refs[opt]
1378/// objc-interface-decl-list
James Dennett17d26a62012-06-11 06:19:40 +00001379/// \@end
Steve Naroffdac269b2007-08-20 21:31:48 +00001380///
1381/// objc-protocol-forward-reference:
James Dennett17d26a62012-06-11 06:19:40 +00001382/// \@protocol identifier-list ';'
Steve Naroffdac269b2007-08-20 21:31:48 +00001383///
James Dennett17d26a62012-06-11 06:19:40 +00001384/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001385/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001386/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001387Parser::DeclGroupPtrTy
1388Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1389 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001390 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001391 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1392 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregor083128f2009-11-18 04:49:41 +00001394 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001395 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001396 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001397 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001398 }
1399
Chris Lattnerdf195262007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001401 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001402 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001403 }
1404 // Save the protocol name, then consume it.
1405 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1406 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Chris Lattnerdf195262007-10-09 17:51:17 +00001408 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001409 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001410 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001411 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001412 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001415 CheckNestedObjCContexts(AtLoc);
1416
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001418 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001419 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1420
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001421 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001422 while (1) {
1423 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001424 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001425 Diag(Tok, diag::err_expected_ident);
1426 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001427 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001428 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001429 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1430 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001431 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Chris Lattnerdf195262007-10-09 17:51:17 +00001433 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001434 break;
1435 }
1436 // Consume the ';'.
1437 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001438 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Steve Naroffe440eb82007-10-10 17:32:04 +00001440 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001441 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001442 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001443 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001446 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001447 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001448
Chris Lattner5f9e2722011-07-23 10:55:15 +00001449 SmallVector<Decl *, 8> ProtocolRefs;
1450 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001451 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001452 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1453 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001454 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001455
John McCalld226f652010-08-21 09:40:31 +00001456 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001457 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001458 ProtocolRefs.data(),
1459 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001460 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001461 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001462
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001463 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001464 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001465}
Steve Naroffdac269b2007-08-20 21:31:48 +00001466
1467/// objc-implementation:
1468/// objc-class-implementation-prologue
1469/// objc-category-implementation-prologue
1470///
1471/// objc-class-implementation-prologue:
1472/// @implementation identifier objc-superclass[opt]
1473/// objc-class-instance-variables[opt]
1474///
1475/// objc-category-implementation-prologue:
1476/// @implementation identifier ( identifier )
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001477Parser::DeclGroupPtrTy
1478Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001479 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1480 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001481 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001482 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001484 // Code completion after '@implementation'.
1485 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001486 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001487 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001488 return DeclGroupPtrTy();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001489 }
1490
Chris Lattnerdf195262007-10-09 17:51:17 +00001491 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001492 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001493 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001494 }
1495 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001496 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001497 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001498 Decl *ObjCImpDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
1500 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001501 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001502 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001503 SourceLocation categoryLoc, rparenLoc;
1504 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001506 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001507 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001508 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001509 return DeclGroupPtrTy();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001510 }
1511
Chris Lattnerdf195262007-10-09 17:51:17 +00001512 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001513 categoryId = Tok.getIdentifierInfo();
1514 categoryLoc = ConsumeToken();
1515 } else {
1516 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001517 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001518 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001519 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001520 Diag(Tok, diag::err_expected_rparen);
1521 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001522 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001523 }
1524 rparenLoc = ConsumeParen();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001525 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001526 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001527 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001528
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001529 } else {
1530 // We have a class implementation
1531 SourceLocation superClassLoc;
1532 IdentifierInfo *superClassId = 0;
1533 if (Tok.is(tok::colon)) {
1534 // We have a super class
1535 ConsumeToken();
1536 if (Tok.isNot(tok::identifier)) {
1537 Diag(Tok, diag::err_expected_ident); // missing super class name.
1538 return DeclGroupPtrTy();
1539 }
1540 superClassId = Tok.getIdentifierInfo();
1541 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001542 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001543 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1544 AtLoc, nameId, nameLoc,
1545 superClassId, superClassLoc);
1546
1547 if (Tok.is(tok::l_brace)) // we have ivars
1548 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001549 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001550 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001552 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001553
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001554 {
1555 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1556 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1557 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001558 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001559 MaybeParseMicrosoftAttributes(attrs);
1560 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1561 DeclGroupRef DG = DGP.get();
1562 DeclsInGroup.append(DG.begin(), DG.end());
1563 }
1564 }
1565 }
1566
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001567 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001568}
Steve Naroff60fccee2007-10-29 21:38:07 +00001569
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001570Parser::DeclGroupPtrTy
1571Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001572 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1573 "ParseObjCAtEndDeclaration(): Expected @end");
1574 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001575 if (CurParsedObjCImpl)
1576 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001577 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001578 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001579 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001580 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001581}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001582
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001583Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1584 if (!Finished) {
1585 finish(P.Tok.getLocation());
1586 if (P.Tok.is(tok::eof)) {
1587 P.Diag(P.Tok, diag::err_objc_missing_end)
1588 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1589 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1590 << Sema::OCK_Implementation;
1591 }
1592 }
1593 P.CurParsedObjCImpl = 0;
1594 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001595}
1596
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001597void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1598 assert(!Finished);
1599 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1600 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001601 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1602 true/*Methods*/);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001603
1604 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1605
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001606 if (HasCFunction)
1607 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1608 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1609 false/*c-functions*/);
1610
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001611 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001612 for (LateParsedObjCMethodContainer::iterator
1613 I = LateParsedObjCMethods.begin(),
1614 E = LateParsedObjCMethods.end(); I != E; ++I)
1615 delete *I;
1616 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001617
1618 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001619}
1620
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001621/// compatibility-alias-decl:
1622/// @compatibility_alias alias-name class-name ';'
1623///
John McCalld226f652010-08-21 09:40:31 +00001624Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001625 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1626 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1627 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001628 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001629 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001630 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001631 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001632 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1633 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001634 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001635 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001636 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001637 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001638 IdentifierInfo *classId = Tok.getIdentifierInfo();
1639 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001640 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1641 "@compatibility_alias");
Richard Smithde01b7a2012-08-08 23:32:13 +00001642 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1643 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001644}
1645
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001646/// property-synthesis:
1647/// @synthesize property-ivar-list ';'
1648///
1649/// property-ivar-list:
1650/// property-ivar
1651/// property-ivar-list ',' property-ivar
1652///
1653/// property-ivar:
1654/// identifier
1655/// identifier '=' identifier
1656///
John McCalld226f652010-08-21 09:40:31 +00001657Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001658 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1659 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001660 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Douglas Gregorb328c422009-11-18 19:45:45 +00001662 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001663 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001664 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001665 cutOffParsing();
1666 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001667 }
1668
Douglas Gregorb328c422009-11-18 19:45:45 +00001669 if (Tok.isNot(tok::identifier)) {
1670 Diag(Tok, diag::err_synthesized_property_name);
1671 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001672 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001673 }
1674
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001675 IdentifierInfo *propertyIvar = 0;
1676 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1677 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001678 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001679 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001680 // property '=' ivar-name
1681 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001682
1683 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001684 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001685 cutOffParsing();
1686 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001687 }
1688
Chris Lattnerdf195262007-10-09 17:51:17 +00001689 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001690 Diag(Tok, diag::err_expected_ident);
1691 break;
1692 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001693 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001694 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001695 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001696 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001697 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001698 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001699 break;
1700 ConsumeToken(); // consume ','
1701 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001702 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001703 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001704}
1705
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001706/// property-dynamic:
1707/// @dynamic property-list
1708///
1709/// property-list:
1710/// identifier
1711/// property-list ',' identifier
1712///
John McCalld226f652010-08-21 09:40:31 +00001713Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001714 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1715 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001716 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001717 while (true) {
1718 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001719 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001720 cutOffParsing();
1721 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001722 }
1723
1724 if (Tok.isNot(tok::identifier)) {
1725 Diag(Tok, diag::err_expected_ident);
1726 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001727 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001728 }
1729
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001730 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1731 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001732 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001733 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001734
Chris Lattnerdf195262007-10-09 17:51:17 +00001735 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001736 break;
1737 ConsumeToken(); // consume ','
1738 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001739 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001740 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001741}
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001743/// objc-throw-statement:
1744/// throw expression[opt];
1745///
John McCall60d7b3a2010-08-24 06:29:42 +00001746StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1747 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001748 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001749 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001750 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001751 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001752 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001753 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001754 }
1755 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001756 // consume ';'
1757 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001758 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001759}
1760
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001761/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001762/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001763///
John McCall60d7b3a2010-08-24 06:29:42 +00001764StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001765Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001766 ConsumeToken(); // consume synchronized
1767 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001768 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001769 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001770 }
John McCall07524032011-07-27 21:50:02 +00001771
1772 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001773 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001774 ExprResult operand(ParseExpression());
1775
1776 if (Tok.is(tok::r_paren)) {
1777 ConsumeParen(); // ')'
1778 } else {
1779 if (!operand.isInvalid())
1780 Diag(Tok, diag::err_expected_rparen);
1781
1782 // Skip forward until we see a left brace, but don't consume it.
1783 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001784 }
John McCall07524032011-07-27 21:50:02 +00001785
1786 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001787 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001788 if (!operand.isInvalid())
1789 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001790 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001791 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001792
John McCall07524032011-07-27 21:50:02 +00001793 // Check the @synchronized operand now.
1794 if (!operand.isInvalid())
1795 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001796
John McCall07524032011-07-27 21:50:02 +00001797 // Parse the compound statement within a new scope.
1798 ParseScope bodyScope(this, Scope::DeclScope);
1799 StmtResult body(ParseCompoundStatementBody());
1800 bodyScope.Exit();
1801
1802 // If there was a semantic or parse error earlier with the
1803 // operand, fail now.
1804 if (operand.isInvalid())
1805 return StmtError();
1806
1807 if (body.isInvalid())
1808 body = Actions.ActOnNullStmt(Tok.getLocation());
1809
1810 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001811}
1812
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001813/// objc-try-catch-statement:
1814/// @try compound-statement objc-catch-list[opt]
1815/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1816///
1817/// objc-catch-list:
1818/// @catch ( parameter-declaration ) compound-statement
1819/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1820/// catch-parameter-declaration:
1821/// parameter-declaration
1822/// '...' [OBJC2]
1823///
John McCall60d7b3a2010-08-24 06:29:42 +00001824StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001825 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001826
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001827 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001828 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001829 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001830 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001831 }
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001832 StmtVector CatchStmts;
John McCall60d7b3a2010-08-24 06:29:42 +00001833 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001834 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001835 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001836 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001837 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001838 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001839
Chris Lattnerdf195262007-10-09 17:51:17 +00001840 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001841 // At this point, we need to lookahead to determine if this @ is the start
1842 // of an @catch or @finally. We don't want to consume the @ token if this
1843 // is an @try or @encode or something else.
1844 Token AfterAt = GetLookAheadToken(1);
1845 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1846 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1847 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001848
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001849 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001850 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001851 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001852 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001853 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001854 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001855 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001856 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001857 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001858 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001859 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001860 ParseDeclarator(ParmDecl);
1861
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001862 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001863 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001864 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001865 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001866 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Steve Naroff93a25952009-04-07 22:56:58 +00001868 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Steve Naroff93a25952009-04-07 22:56:58 +00001870 if (Tok.is(tok::r_paren))
1871 RParenLoc = ConsumeParen();
1872 else // Skip over garbage, until we get to ')'. Eat the ')'.
1873 SkipUntil(tok::r_paren, true, false);
1874
John McCall60d7b3a2010-08-24 06:29:42 +00001875 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001876 if (Tok.is(tok::l_brace))
1877 CatchBody = ParseCompoundStatementBody();
1878 else
1879 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001880 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001881 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001882
John McCall60d7b3a2010-08-24 06:29:42 +00001883 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001884 RParenLoc,
1885 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001886 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001887 if (!Catch.isInvalid())
1888 CatchStmts.push_back(Catch.release());
1889
Steve Naroff64515f32008-02-05 21:27:35 +00001890 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001891 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1892 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001893 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001894 }
1895 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001896 } else {
1897 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001898 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001899 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001900
John McCall60d7b3a2010-08-24 06:29:42 +00001901 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001902 if (Tok.is(tok::l_brace))
1903 FinallyBody = ParseCompoundStatementBody();
1904 else
1905 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001906 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001907 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001908 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001909 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001910 catch_or_finally_seen = true;
1911 break;
1912 }
1913 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001914 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001915 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001916 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001917 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001918
John McCall9ae2f072010-08-23 23:25:46 +00001919 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001920 CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001921 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001922}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001923
John McCallf85e1932011-06-15 23:02:42 +00001924/// objc-autoreleasepool-statement:
1925/// @autoreleasepool compound-statement
1926///
1927StmtResult
1928Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1929 ConsumeToken(); // consume autoreleasepool
1930 if (Tok.isNot(tok::l_brace)) {
1931 Diag(Tok, diag::err_expected_lbrace);
1932 return StmtError();
1933 }
1934 // Enter a scope to hold everything within the compound stmt. Compound
1935 // statements can always hold declarations.
1936 ParseScope BodyScope(this, Scope::DeclScope);
1937
1938 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1939
1940 BodyScope.Exit();
1941 if (AutoreleasePoolBody.isInvalid())
1942 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1943 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1944 AutoreleasePoolBody.take());
1945}
1946
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001947/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1948/// for later parsing.
1949void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1950 LexedMethod* LM = new LexedMethod(this, MDecl);
1951 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1952 CachedTokens &Toks = LM->Toks;
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001953 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001954 Toks.push_back(Tok);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001955 if (Tok.is(tok::kw_try)) {
1956 ConsumeToken();
Fariborz Jahaniandbd69452012-08-10 20:34:17 +00001957 if (Tok.is(tok::colon)) {
1958 Toks.push_back(Tok);
1959 ConsumeToken();
1960 while (Tok.isNot(tok::l_brace)) {
1961 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1962 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1963 }
1964 }
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001965 Toks.push_back(Tok); // also store '{'
1966 }
1967 else if (Tok.is(tok::colon)) {
1968 ConsumeToken();
1969 while (Tok.isNot(tok::l_brace)) {
1970 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1971 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1972 }
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001973 Toks.push_back(Tok); // also store '{'
1974 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001975 ConsumeBrace();
1976 // Consume everything up to (and including) the matching right brace.
1977 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001978 while (Tok.is(tok::kw_catch)) {
1979 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1980 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1981 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001982}
1983
Steve Naroff3536b442007-09-06 21:24:23 +00001984/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001985///
John McCalld226f652010-08-21 09:40:31 +00001986Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001987 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001988
John McCallf312b1e2010-08-26 23:41:50 +00001989 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1990 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001992 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001993 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001994 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001995 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001996 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001997 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001998 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001999 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002000
Steve Naroff409be832007-11-11 19:54:21 +00002001 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00002002 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002003 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Steve Naroff409be832007-11-11 19:54:21 +00002005 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2006 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Steve Naroff409be832007-11-11 19:54:21 +00002008 // If we didn't find the '{', bail out.
2009 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00002010 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002011 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002012
2013 if (!MDecl) {
2014 ConsumeBrace();
2015 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2016 return 0;
2017 }
2018
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002019 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002020 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahanianc9b97092012-08-09 17:15:00 +00002021 assert (CurParsedObjCImpl
2022 && "ParseObjCMethodDefinition - Method out of @implementation");
2023 // Consume the tokens and store them for later parsing.
2024 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff71c0a952007-11-13 23:01:27 +00002025 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00002026}
Anders Carlsson55085182007-08-21 17:43:55 +00002027
John McCall60d7b3a2010-08-24 06:29:42 +00002028StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002029 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002030 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002031 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002032 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00002033 }
2034
2035 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00002036 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002037
2038 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00002039 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002040
2041 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00002042 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00002043
2044 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2045 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002046
John McCall60d7b3a2010-08-24 06:29:42 +00002047 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002048 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00002049 // If the expression is invalid, skip ahead to the next semicolon. Not
2050 // doing this opens us up to the possibility of infinite loops if
2051 // ParseExpression does not consume any tokens.
2052 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00002053 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00002054 }
Chris Lattner5d803162009-12-07 16:33:19 +00002055
Steve Naroff64515f32008-02-05 21:27:35 +00002056 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00002057 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +00002058 return Actions.ActOnExprStmt(Res);
Steve Naroff64515f32008-02-05 21:27:35 +00002059}
2060
John McCall60d7b3a2010-08-24 06:29:42 +00002061ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002062 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002063 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002064 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002065 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002066 return ExprError();
2067
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002068 case tok::minus:
2069 case tok::plus: {
2070 tok::TokenKind Kind = Tok.getKind();
2071 SourceLocation OpLoc = ConsumeToken();
2072
2073 if (!Tok.is(tok::numeric_constant)) {
2074 const char *Symbol = 0;
2075 switch (Kind) {
2076 case tok::minus: Symbol = "-"; break;
2077 case tok::plus: Symbol = "+"; break;
2078 default: llvm_unreachable("missing unary operator case");
2079 }
2080 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2081 << Symbol;
2082 return ExprError();
2083 }
2084
2085 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2086 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002087 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002088 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002089 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002090
2091 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2092 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002093 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002094
2095 return ParsePostfixExpressionSuffix(
2096 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2097 }
2098
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002099 case tok::string_literal: // primary-expression: string-literal
2100 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002101 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002102
2103 case tok::char_constant:
2104 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2105
2106 case tok::numeric_constant:
2107 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2108
2109 case tok::kw_true: // Objective-C++, etc.
2110 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2111 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2112 case tok::kw_false: // Objective-C++, etc.
2113 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2114 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2115
2116 case tok::l_square:
2117 // Objective-C array literal
2118 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2119
2120 case tok::l_brace:
2121 // Objective-C dictionary literal
2122 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2123
Patrick Beardeb382ec2012-04-19 00:25:12 +00002124 case tok::l_paren:
2125 // Objective-C boxed expression
2126 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2127
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002128 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002129 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002130 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002131
Chris Lattner4fef81d2008-08-05 06:19:09 +00002132 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2133 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002134 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002135 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002136 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002137 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002138 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian6749ae12012-07-09 20:00:35 +00002139 default: {
2140 const char *str = 0;
2141 if (GetLookAheadToken(1).is(tok::l_brace)) {
2142 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2143 str =
2144 ch == 't' ? "try"
2145 : (ch == 'f' ? "finally"
2146 : (ch == 'a' ? "autoreleasepool" : 0));
2147 }
2148 if (str) {
2149 SourceLocation kwLoc = Tok.getLocation();
2150 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2151 FixItHint::CreateReplacement(kwLoc, str));
2152 }
2153 else
2154 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2155 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002156 }
Anders Carlsson55085182007-08-21 17:43:55 +00002157 }
Anders Carlsson55085182007-08-21 17:43:55 +00002158}
2159
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002160/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002161///
2162/// This routine parses the receiver of a message send in
2163/// Objective-C++ either as a type or as an expression. Note that this
2164/// routine must not be called to parse a send to 'super', since it
2165/// has no way to return such a result.
2166///
2167/// \param IsExpr Whether the receiver was parsed as an expression.
2168///
2169/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2170/// IsExpr is true), the parsed expression. If the receiver was parsed
2171/// as a type (\c IsExpr is false), the parsed type.
2172///
2173/// \returns True if an error occurred during parsing or semantic
2174/// analysis, in which case the arguments do not have valid
2175/// values. Otherwise, returns false for a successful parse.
2176///
2177/// objc-receiver: [C++]
2178/// 'super' [not parsed here]
2179/// expression
2180/// simple-type-specifier
2181/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002182bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002183 InMessageExpressionRAIIObject InMessage(*this, true);
2184
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002185 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2186 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2187 TryAnnotateTypeOrScopeToken();
2188
Kaelyn Uhrain7bf33402012-06-15 23:45:51 +00002189 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002190 // objc-receiver:
2191 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002192 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002193 if (Receiver.isInvalid())
2194 return true;
2195
2196 IsExpr = true;
2197 TypeOrExpr = Receiver.take();
2198 return false;
2199 }
2200
2201 // objc-receiver:
2202 // typename-specifier
2203 // simple-type-specifier
2204 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002205 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002206 ParseCXXSimpleTypeSpecifier(DS);
2207
2208 if (Tok.is(tok::l_paren)) {
2209 // If we see an opening parentheses at this point, we are
2210 // actually parsing an expression that starts with a
2211 // function-style cast, e.g.,
2212 //
2213 // postfix-expression:
2214 // simple-type-specifier ( expression-list [opt] )
2215 // typename-specifier ( expression-list [opt] )
2216 //
2217 // Parse the remainder of this case, then the (optional)
2218 // postfix-expression suffix, followed by the (optional)
2219 // right-hand side of the binary expression. We have an
2220 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002221 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002222 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002223 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002224 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002225 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002226 if (Receiver.isInvalid())
2227 return true;
2228
2229 IsExpr = true;
2230 TypeOrExpr = Receiver.take();
2231 return false;
2232 }
2233
2234 // We have a class message. Turn the simple-type-specifier or
2235 // typename-specifier we parsed into a type and parse the
2236 // remainder of the class message.
2237 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002238 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002239 if (Type.isInvalid())
2240 return true;
2241
2242 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002243 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002244 return false;
2245}
2246
Douglas Gregor1b730e82010-05-31 14:40:22 +00002247/// \brief Determine whether the parser is currently referring to a an
2248/// Objective-C message send, using a simplified heuristic to avoid overhead.
2249///
2250/// This routine will only return true for a subset of valid message-send
2251/// expressions.
2252bool Parser::isSimpleObjCMessageExpression() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002253 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002254 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002255 return GetLookAheadToken(1).is(tok::identifier) &&
2256 GetLookAheadToken(2).is(tok::identifier);
2257}
2258
Douglas Gregor9497a732010-09-16 01:51:54 +00002259bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002260 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregor9497a732010-09-16 01:51:54 +00002261 InMessageExpression)
2262 return false;
2263
2264
2265 ParsedType Type;
2266
2267 if (Tok.is(tok::annot_typename))
2268 Type = getTypeAnnotation(Tok);
2269 else if (Tok.is(tok::identifier))
2270 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2271 getCurScope());
2272 else
2273 return false;
2274
2275 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2276 const Token &AfterNext = GetLookAheadToken(2);
2277 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2278 if (Tok.is(tok::identifier))
2279 TryAnnotateTypeOrScopeToken();
2280
2281 return Tok.is(tok::annot_typename);
2282 }
2283 }
2284
2285 return false;
2286}
2287
Mike Stump1eb44332009-09-09 15:08:12 +00002288/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002289/// '[' objc-receiver objc-message-args ']'
2290///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002291/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002292/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002293/// expression
2294/// class-name
2295/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002296///
John McCall60d7b3a2010-08-24 06:29:42 +00002297ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002298 assert(Tok.is(tok::l_square) && "'[' expected");
2299 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2300
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002301 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002302 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002303 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002304 return ExprError();
2305 }
2306
Douglas Gregor0fbda682010-09-15 14:51:05 +00002307 InMessageExpressionRAIIObject InMessage(*this, true);
2308
David Blaikie4e4d0842012-03-11 07:00:24 +00002309 if (getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002310 // We completely separate the C and C++ cases because C++ requires
2311 // more complicated (read: slower) parsing.
2312
2313 // Handle send to super.
2314 // FIXME: This doesn't benefit from the same typo-correction we
2315 // get in Objective-C.
2316 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002317 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002318 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2319 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002320
2321 // Parse the receiver, which is either a type or an expression.
2322 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002323 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002324 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2325 SkipUntil(tok::r_square);
2326 return ExprError();
2327 }
2328
2329 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002330 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2331 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002332 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002333
2334 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002335 ParsedType::getFromOpaquePtr(TypeOrExpr),
2336 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002337 }
2338
2339 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002340 IdentifierInfo *Name = Tok.getIdentifierInfo();
2341 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002342 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002343 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002344 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002345 NextToken().is(tok::period),
2346 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002347 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002348 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2349 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002350
John McCallf312b1e2010-08-26 23:41:50 +00002351 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002352 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002353 SkipUntil(tok::r_square);
2354 return ExprError();
2355 }
2356
Douglas Gregor1569f952010-04-21 20:38:13 +00002357 ConsumeToken(); // the type name
2358
2359 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002360 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002361
John McCallf312b1e2010-08-26 23:41:50 +00002362 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002363 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002364 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002365 }
Chris Lattner699b6612008-01-25 18:59:06 +00002366 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002367
2368 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002369 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002370 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002371 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002372 return Res;
Chris Lattner699b6612008-01-25 18:59:06 +00002373 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002374
John McCallb3d87482010-08-24 05:47:05 +00002375 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2376 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002377}
Sebastian Redl1d922962008-12-13 15:32:12 +00002378
Douglas Gregor2725ca82010-04-21 19:57:20 +00002379/// \brief Parse the remainder of an Objective-C message following the
2380/// '[' objc-receiver.
2381///
2382/// This routine handles sends to super, class messages (sent to a
2383/// class name), and instance messages (sent to an object), and the
2384/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2385/// ReceiverExpr, respectively. Only one of these parameters may have
2386/// a valid value.
2387///
2388/// \param LBracLoc The location of the opening '['.
2389///
2390/// \param SuperLoc If this is a send to 'super', the location of the
2391/// 'super' keyword that indicates a send to the superclass.
2392///
2393/// \param ReceiverType If this is a class message, the type of the
2394/// class we are sending a message to.
2395///
2396/// \param ReceiverExpr If this is an instance message, the expression
2397/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002398///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002399/// objc-message-args:
2400/// objc-selector
2401/// objc-keywordarg-list
2402///
2403/// objc-keywordarg-list:
2404/// objc-keywordarg
2405/// objc-keywordarg-list objc-keywordarg
2406///
Mike Stump1eb44332009-09-09 15:08:12 +00002407/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002408/// selector-name[opt] ':' objc-keywordexpr
2409///
2410/// objc-keywordexpr:
2411/// nonempty-expr-list
2412///
2413/// nonempty-expr-list:
2414/// assignment-expression
2415/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002416///
John McCall60d7b3a2010-08-24 06:29:42 +00002417ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002418Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002419 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002420 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002421 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002422 InMessageExpressionRAIIObject InMessage(*this, true);
2423
Steve Naroffc4df6d22009-11-07 02:08:14 +00002424 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002425 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002426 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2427 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002428 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002429 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2430 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002431 else
John McCall9ae2f072010-08-23 23:25:46 +00002432 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002433 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002434 cutOffParsing();
2435 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002436 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002437
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002438 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002439 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002440 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002441
Chris Lattner5f9e2722011-07-23 10:55:15 +00002442 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002443 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002444 ExprVector KeyExprs;
Steve Naroff68d331a2007-09-27 14:38:14 +00002445
Chris Lattnerdf195262007-10-09 17:51:17 +00002446 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002447 while (1) {
2448 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002449 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002450 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002451
Chris Lattnerdf195262007-10-09 17:51:17 +00002452 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002453 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002454 // We must manually skip to a ']', otherwise the expression skipper will
2455 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2456 // the enclosing expression.
2457 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002458 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002459 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002460
Steve Naroff68d331a2007-09-27 14:38:14 +00002461 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002462 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002463
2464 if (Tok.is(tok::code_completion)) {
2465 if (SuperLoc.isValid())
2466 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2467 KeyIdents.data(),
2468 KeyIdents.size(),
2469 /*AtArgumentEpression=*/true);
2470 else if (ReceiverType)
2471 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2472 KeyIdents.data(),
2473 KeyIdents.size(),
2474 /*AtArgumentEpression=*/true);
2475 else
2476 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2477 KeyIdents.data(),
2478 KeyIdents.size(),
2479 /*AtArgumentEpression=*/true);
2480
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002481 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002482 return ExprError();
2483 }
2484
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002485 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002486 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002487 // We must manually skip to a ']', otherwise the expression skipper will
2488 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2489 // the enclosing expression.
2490 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002491 return Res;
Steve Naroff37387c92007-09-17 20:25:27 +00002492 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002493
Steve Naroff37387c92007-09-17 20:25:27 +00002494 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002495 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002496
Douglas Gregord3c68542009-11-19 01:08:35 +00002497 // Code completion after each argument.
2498 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002499 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002500 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002501 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002502 KeyIdents.size(),
2503 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002504 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002505 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002506 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002507 KeyIdents.size(),
2508 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002509 else
John McCall9ae2f072010-08-23 23:25:46 +00002510 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002511 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002512 KeyIdents.size(),
2513 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002514 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002515 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002516 }
2517
Steve Naroff37387c92007-09-17 20:25:27 +00002518 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002519 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002520 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002521 break;
2522 // We have a selector or a colon, continue parsing.
2523 }
2524 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002525 while (Tok.is(tok::comma)) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002526 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002527 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002528 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002529 if (Res.isInvalid()) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002530 if (Tok.is(tok::colon)) {
2531 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2532 FixItHint::CreateRemoval(commaLoc);
2533 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002534 // We must manually skip to a ']', otherwise the expression skipper will
2535 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2536 // the enclosing expression.
2537 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002538 return Res;
Steve Naroff49f109c2007-11-15 13:05:42 +00002539 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002540
Steve Naroff49f109c2007-11-15 13:05:42 +00002541 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002542 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002543 }
2544 } else if (!selIdent) {
2545 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002546
Chris Lattner4fef81d2008-08-05 06:19:09 +00002547 // We must manually skip to a ']', otherwise the expression skipper will
2548 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2549 // the enclosing expression.
2550 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002551 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002552 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002553
Chris Lattnerdf195262007-10-09 17:51:17 +00002554 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002555 if (Tok.is(tok::identifier))
2556 Diag(Tok, diag::err_expected_colon);
2557 else
2558 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002559 // We must manually skip to a ']', otherwise the expression skipper will
2560 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2561 // the enclosing expression.
2562 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002563 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002564 }
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002565
Chris Lattner699b6612008-01-25 18:59:06 +00002566 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002567
Steve Naroff29238a02007-10-05 18:42:47 +00002568 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002569 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002570 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002571 KeyLocs.push_back(Loc);
2572 }
Chris Lattnerff384912007-10-07 02:00:24 +00002573 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002574
Douglas Gregor2725ca82010-04-21 19:57:20 +00002575 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002576 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002577 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002578 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002579 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002580 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCall9ae2f072010-08-23 23:25:46 +00002581 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002582 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002583}
2584
John McCall60d7b3a2010-08-24 06:29:42 +00002585ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2586 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002587 if (Res.isInvalid()) return Res;
Sebastian Redl1d922962008-12-13 15:32:12 +00002588
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002589 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2590 // expressions. At this point, we know that the only valid thing that starts
2591 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002592 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002593 ExprVector AtStrings;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002594 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002595 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002596
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002597 while (Tok.is(tok::at)) {
2598 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002599
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002600 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002601 if (!isTokenStringLiteral())
2602 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002603
John McCall60d7b3a2010-08-24 06:29:42 +00002604 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002605 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002606 return Lit;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002607
Sebastian Redleffa8d12008-12-10 00:02:53 +00002608 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002609 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002610
Nico Weberd32b94d2012-12-31 00:28:03 +00002611 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2612 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00002613}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002614
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002615/// ParseObjCBooleanLiteral -
2616/// objc-scalar-literal : '@' boolean-keyword
2617/// ;
2618/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2619/// ;
2620ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2621 bool ArgValue) {
2622 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2623 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2624}
2625
2626/// ParseObjCCharacterLiteral -
2627/// objc-scalar-literal : '@' character-literal
2628/// ;
2629ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2630 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2631 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002632 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002633 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002634 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002635 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002636}
2637
2638/// ParseObjCNumericLiteral -
2639/// objc-scalar-literal : '@' scalar-literal
2640/// ;
2641/// scalar-literal : | numeric-constant /* any numeric constant. */
2642/// ;
2643ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2644 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2645 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002646 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002647 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002648 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002649 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002650}
2651
Patrick Beardeb382ec2012-04-19 00:25:12 +00002652/// ParseObjCBoxedExpr -
2653/// objc-box-expression:
2654/// @( assignment-expression )
2655ExprResult
2656Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2657 if (Tok.isNot(tok::l_paren))
2658 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2659
2660 BalancedDelimiterTracker T(*this, tok::l_paren);
2661 T.consumeOpen();
2662 ExprResult ValueExpr(ParseAssignmentExpression());
2663 if (T.consumeClose())
2664 return ExprError();
Argyrios Kyrtzidisedd27602012-05-10 20:02:36 +00002665
2666 if (ValueExpr.isInvalid())
2667 return ExprError();
2668
Patrick Beardeb382ec2012-04-19 00:25:12 +00002669 // Wrap the sub-expression in a parenthesized expression, to distinguish
2670 // a boxed expression from a literal.
2671 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2672 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Weberd32b94d2012-12-31 00:28:03 +00002673 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2674 ValueExpr.take());
Patrick Beardeb382ec2012-04-19 00:25:12 +00002675}
2676
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002677ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002678 ExprVector ElementExprs; // array elements.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002679 ConsumeBracket(); // consume the l_square.
2680
2681 while (Tok.isNot(tok::r_square)) {
2682 // Parse list of array element expressions (all must be id types).
2683 ExprResult Res(ParseAssignmentExpression());
2684 if (Res.isInvalid()) {
2685 // We must manually skip to a ']', otherwise the expression skipper will
2686 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2687 // the enclosing expression.
2688 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002689 return Res;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002690 }
2691
2692 // Parse the ellipsis that indicates a pack expansion.
2693 if (Tok.is(tok::ellipsis))
2694 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2695 if (Res.isInvalid())
2696 return true;
2697
2698 ElementExprs.push_back(Res.release());
2699
2700 if (Tok.is(tok::comma))
2701 ConsumeToken(); // Eat the ','.
2702 else if (Tok.isNot(tok::r_square))
2703 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2704 }
2705 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002706 MultiExprArg Args(ElementExprs);
Nico Weberd32b94d2012-12-31 00:28:03 +00002707 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002708}
2709
2710ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2711 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2712 ConsumeBrace(); // consume the l_square.
2713 while (Tok.isNot(tok::r_brace)) {
2714 // Parse the comma separated key : value expressions.
2715 ExprResult KeyExpr;
2716 {
2717 ColonProtectionRAIIObject X(*this);
2718 KeyExpr = ParseAssignmentExpression();
2719 if (KeyExpr.isInvalid()) {
2720 // We must manually skip to a '}', otherwise the expression skipper will
2721 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2722 // the enclosing expression.
2723 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002724 return KeyExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002725 }
2726 }
2727
2728 if (Tok.is(tok::colon)) {
2729 ConsumeToken();
2730 } else {
2731 return ExprError(Diag(Tok, diag::err_expected_colon));
2732 }
2733
2734 ExprResult ValueExpr(ParseAssignmentExpression());
2735 if (ValueExpr.isInvalid()) {
2736 // We must manually skip to a '}', otherwise the expression skipper will
2737 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2738 // the enclosing expression.
2739 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002740 return ValueExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002741 }
2742
2743 // Parse the ellipsis that designates this as a pack expansion.
2744 SourceLocation EllipsisLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00002745 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002746 EllipsisLoc = ConsumeToken();
2747
2748 // We have a valid expression. Collect it in a vector so we can
2749 // build the argument list.
2750 ObjCDictionaryElement Element = {
David Blaikie66874fb2013-02-21 01:47:18 +00002751 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002752 };
2753 Elements.push_back(Element);
2754
2755 if (Tok.is(tok::comma))
2756 ConsumeToken(); // Eat the ','.
2757 else if (Tok.isNot(tok::r_brace))
2758 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2759 }
2760 SourceLocation EndLoc = ConsumeBrace();
2761
2762 // Create the ObjCDictionaryLiteral.
Nico Weberd32b94d2012-12-31 00:28:03 +00002763 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2764 Elements.data(), Elements.size());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002765}
2766
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002767/// objc-encode-expression:
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002768/// \@encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002769ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002770Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002771 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002772
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002773 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002774
Chris Lattner4fef81d2008-08-05 06:19:09 +00002775 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002776 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2777
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002778 BalancedDelimiterTracker T(*this, tok::l_paren);
2779 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002780
Douglas Gregor809070a2009-02-18 17:45:20 +00002781 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002782
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002783 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002784
Douglas Gregor809070a2009-02-18 17:45:20 +00002785 if (Ty.isInvalid())
2786 return ExprError();
2787
Nico Weberd32b94d2012-12-31 00:28:03 +00002788 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2789 Ty.get(), T.getCloseLocation());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002790}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002791
2792/// objc-protocol-expression
James Dennett17d26a62012-06-11 06:19:40 +00002793/// \@protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002794ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002795Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002796 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002797
Chris Lattner4fef81d2008-08-05 06:19:09 +00002798 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002799 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2800
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002801 BalancedDelimiterTracker T(*this, tok::l_paren);
2802 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002803
Chris Lattner4fef81d2008-08-05 06:19:09 +00002804 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002805 return ExprError(Diag(Tok, diag::err_expected_ident));
2806
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002807 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002808 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002809
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002810 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002811
Nico Weberd32b94d2012-12-31 00:28:03 +00002812 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2813 T.getOpenLocation(), ProtoIdLoc,
2814 T.getCloseLocation());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002815}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002816
2817/// objc-selector-expression
2818/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002819ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002820 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002821
Chris Lattner4fef81d2008-08-05 06:19:09 +00002822 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002823 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2824
Chris Lattner5f9e2722011-07-23 10:55:15 +00002825 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002826 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002827
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002828 BalancedDelimiterTracker T(*this, tok::l_paren);
2829 T.consumeOpen();
2830
Douglas Gregor458433d2010-08-26 15:07:07 +00002831 if (Tok.is(tok::code_completion)) {
2832 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2833 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002834 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002835 return ExprError();
2836 }
2837
Chris Lattner2fc5c242009-04-11 18:13:45 +00002838 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002839 if (!SelIdent && // missing selector name.
2840 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002841 return ExprError(Diag(Tok, diag::err_expected_ident));
2842
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002843 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002844 unsigned nColons = 0;
2845 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002846 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002847 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2848 ++nColons;
2849 KeyIdents.push_back(0);
2850 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002851 return ExprError(Diag(Tok, diag::err_expected_colon));
2852
Chris Lattner5add7542010-08-27 22:32:41 +00002853 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002854 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002855 if (Tok.is(tok::r_paren))
2856 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002857
2858 if (Tok.is(tok::code_completion)) {
2859 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2860 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002861 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002862 return ExprError();
2863 }
2864
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002865 // Check for another keyword selector.
2866 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002867 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002868 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002869 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002870 break;
2871 }
Steve Naroff887407e2007-12-05 22:21:29 +00002872 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002873 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002874 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Weberd32b94d2012-12-31 00:28:03 +00002875 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2876 T.getOpenLocation(),
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002877 T.getCloseLocation());
Gabor Greif58065b22007-10-19 15:38:32 +00002878 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002879
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002880void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2881 // MCDecl might be null due to error in method or c-function prototype, etc.
2882 Decl *MCDecl = LM.D;
2883 bool skip = MCDecl &&
2884 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2885 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2886 if (skip)
2887 return;
2888
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002889 // Save the current token position.
2890 SourceLocation OrigLoc = Tok.getLocation();
2891
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002892 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2893 // Append the current token at the end of the new token stream so that it
2894 // doesn't get lost.
2895 LM.Toks.push_back(Tok);
2896 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2897
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002898 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00002899 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002900
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002901 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2902 Tok.is(tok::colon)) &&
2903 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002904 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002905 ParseScope BodyScope(this,
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002906 parseMethod
2907 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2908 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002909
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002910 // Tell the actions module that we have entered a method or c-function definition
2911 // with the specified Declarator for the method/function.
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +00002912 if (parseMethod)
2913 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2914 else
2915 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002916 if (Tok.is(tok::kw_try))
2917 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002918 else {
2919 if (Tok.is(tok::colon))
2920 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002921 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002922 }
Fariborz Jahanian69409722012-08-09 21:12:39 +00002923
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002924 if (Tok.getLocation() != OrigLoc) {
2925 // Due to parsing error, we either went over the cached tokens or
2926 // there are still cached tokens left. If it's the latter case skip the
2927 // leftover tokens.
2928 // Since this is an uncommon situation that should be avoided, use the
2929 // expensive isBeforeInTranslationUnit call.
2930 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2931 OrigLoc))
2932 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2933 ConsumeAnyToken();
2934 }
2935
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002936 return;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002937}