blob: 65bfd1c830ea2516fe590edc8c7eaf1cf55752c5 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
Fariborz Jahanian08602352012-09-17 19:15:26 +000021#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24
Chris Lattner891dca62008-12-08 21:53:24 +000025/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000026/// external-declaration: [C99 6.9]
27/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000028/// [OBJC] objc-class-declaration
29/// [OBJC] objc-alias-declaration
30/// [OBJC] objc-protocol-definition
31/// [OBJC] objc-method-definition
32/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000033Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000035
Douglas Gregorc464ae82009-12-07 09:27:33 +000036 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000037 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000038 cutOffParsing();
39 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000040 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000041
42 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000043 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000044 case tok::objc_class:
45 return ParseObjCAtClassDeclaration(AtLoc);
John McCall7f040a92010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall7f040a92010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Douglas Gregorbd9482d2012-01-01 21:23:57 +000053 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall7f040a92010-12-24 02:08:15 +000054 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000055 case tok::objc_implementation:
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +000056 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000057 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000058 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000059 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000060 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
61 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000062 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000063 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
64 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000065 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
67 break;
Douglas Gregor1b257af2012-12-11 22:11:52 +000068 case tok::objc_import:
David Blaikie4e4d0842012-03-11 07:00:24 +000069 if (getLangOpts().Modules)
Douglas Gregor94ad28b2012-01-03 18:24:14 +000070 return ParseModuleImport(AtLoc);
71
72 // Fall through
73
Chris Lattner5ffb14b2008-08-23 02:02:23 +000074 default:
75 Diag(AtLoc, diag::err_unexpected_at);
76 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000077 SingleDecl = 0;
78 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000080 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83///
Mike Stump1eb44332009-09-09 15:08:12 +000084/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000085/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000086///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000087Parser::DeclGroupPtrTy
88Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000090 SmallVector<IdentifierInfo *, 8> ClassNames;
91 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000092
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000095 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000096 Diag(Tok, diag::err_expected_ident);
97 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000098 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000099 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +0000101 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattnerdf195262007-10-09 17:51:17 +0000104 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 ConsumeToken();
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // Consume the ';'.
111 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000112 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekc09cba62009-11-17 23:12:20 +0000114 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
115 ClassLocs.data(),
116 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000117}
118
Erik Verbruggend64251f2011-12-06 09:25:23 +0000119void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
120{
121 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
122 if (ock == Sema::OCK_None)
123 return;
124
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000125 Decl *Decl = Actions.getObjCDeclContext();
126 if (CurParsedObjCImpl) {
127 CurParsedObjCImpl->finish(AtLoc);
128 } else {
129 Actions.ActOnAtEnd(getCurScope(), AtLoc);
130 }
Erik Verbruggend64251f2011-12-06 09:25:23 +0000131 Diag(AtLoc, diag::err_objc_missing_end)
132 << FixItHint::CreateInsertion(AtLoc, "@end\n");
133 if (Decl)
134 Diag(Decl->getLocStart(), diag::note_objc_container_start)
135 << (int) ock;
Erik Verbruggend64251f2011-12-06 09:25:23 +0000136}
137
Steve Naroffdac269b2007-08-20 21:31:48 +0000138///
139/// objc-interface:
140/// objc-class-interface-attributes[opt] objc-class-interface
141/// objc-category-interface
142///
143/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000144/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000145/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000146/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000147/// objc-interface-decl-list
148/// @end
149///
150/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000151/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000152/// objc-protocol-refs[opt]
153/// objc-interface-decl-list
154/// @end
155///
156/// objc-superclass:
157/// ':' identifier
158///
159/// objc-class-interface-attributes:
160/// __attribute__((visibility("default")))
161/// __attribute__((visibility("hidden")))
162/// __attribute__((deprecated))
163/// __attribute__((unavailable))
164/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardb2f68202012-04-06 18:12:22 +0000165/// __attribute__((objc_root_class))
Steve Naroffdac269b2007-08-20 21:31:48 +0000166///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000167Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000168 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000169 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000171 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000174 // Code completion after '@interface'.
175 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000176 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000177 cutOffParsing();
178 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000179 }
180
Chris Lattnerdf195262007-10-09 17:51:17 +0000181 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000183 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000185
Steve Naroffdac269b2007-08-20 21:31:48 +0000186 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000187 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000188 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000189 if (Tok.is(tok::l_paren) &&
190 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000191
192 BalancedDelimiterTracker T(*this, tok::l_paren);
193 T.consumeOpen();
194
195 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000196 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000197 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000198 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000199 cutOffParsing();
200 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000201 }
202
Steve Naroff527fe232007-08-23 19:56:30 +0000203 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000204 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000205 categoryId = Tok.getIdentifierInfo();
206 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000207 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000208 else if (!getLangOpts().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000209 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000210 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000211 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000212
213 T.consumeClose();
214 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000215 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000216
217 if (!attrs.empty()) { // categories don't support attributes.
218 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
219 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000220 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000221
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000222 // Next, we need to check for any protocol references.
223 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 SmallVector<Decl *, 8> ProtocolRefs;
225 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000226 if (Tok.is(tok::less) &&
227 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000228 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000229 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
John McCalld226f652010-08-21 09:40:31 +0000231 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000232 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000233 nameId, nameLoc,
234 categoryId, categoryLoc,
235 ProtocolRefs.data(),
236 ProtocolRefs.size(),
237 ProtocolLocs.data(),
238 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000239
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000240 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000241 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000242
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000243 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000244 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000245 }
246 // Parse a class interface.
247 IdentifierInfo *superClassId = 0;
248 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000249
Chris Lattnerdf195262007-10-09 17:51:17 +0000250 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000251 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000252
253 // Code completion of superclass names.
254 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000255 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000256 cutOffParsing();
257 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000258 }
259
Chris Lattnerdf195262007-10-09 17:51:17 +0000260 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000261 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000262 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000263 }
264 superClassId = Tok.getIdentifierInfo();
265 superClassLoc = ConsumeToken();
266 }
267 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000268 SmallVector<Decl *, 8> ProtocolRefs;
269 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000270 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000271 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000272 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
273 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000274 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
John McCalld226f652010-08-21 09:40:31 +0000276 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000277 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000278 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000279 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000280 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000281 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattnerdf195262007-10-09 17:51:17 +0000283 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000284 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000285
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000286 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000287 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000288}
289
John McCalld0014542009-12-03 22:31:13 +0000290/// The Objective-C property callback. This should be defined where
291/// it's used, but instead it's been lifted to here to support VS2005.
292struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000293private:
294 virtual void anchor();
295public:
John McCalld0014542009-12-03 22:31:13 +0000296 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000298 ObjCDeclSpec &OCDS;
299 SourceLocation AtLoc;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000300 SourceLocation LParenLoc;
John McCalld0014542009-12-03 22:31:13 +0000301 tok::ObjCKeywordKind MethodImplKind;
302
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000303 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000304 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000305 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000306 SourceLocation LParenLoc,
John McCalld0014542009-12-03 22:31:13 +0000307 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000308 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCalld0014542009-12-03 22:31:13 +0000309 MethodImplKind(MethodImplKind) {
310 }
311
Eli Friedmandcdff462012-08-08 23:53:27 +0000312 void invoke(ParsingFieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000313 if (FD.D.getIdentifier() == 0) {
314 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
315 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000316 return;
John McCalld0014542009-12-03 22:31:13 +0000317 }
318 if (FD.BitfieldSize) {
319 P.Diag(AtLoc, diag::err_objc_property_bitfield)
320 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000321 return;
John McCalld0014542009-12-03 22:31:13 +0000322 }
323
324 // Install the property declarator into interfaceDecl.
325 IdentifierInfo *SelName =
326 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
327
328 Selector GetterSel =
329 P.PP.getSelectorTable().getNullarySelector(SelName);
330 IdentifierInfo *SetterName = OCDS.getSetterName();
331 Selector SetterSel;
332 if (SetterName)
333 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
334 else
335 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
336 P.PP.getSelectorTable(),
337 FD.D.getIdentifier());
338 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000339 Decl *Property =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000340 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
341 FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000342 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000343 &isOverridingProperty,
344 MethodImplKind);
345 if (!isOverridingProperty)
346 Props.push_back(Property);
347
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000348 FD.complete(Property);
John McCalld0014542009-12-03 22:31:13 +0000349 }
350};
351
David Blaikie99ba9e32011-12-20 02:48:34 +0000352void Parser::ObjCPropertyCallback::anchor() {
353}
354
Steve Naroffdac269b2007-08-20 21:31:48 +0000355/// objc-interface-decl-list:
356/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000357/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000358/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000359/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000360/// objc-interface-decl-list declaration
361/// objc-interface-decl-list ';'
362///
Steve Naroff294494e2007-08-22 16:35:03 +0000363/// objc-method-requirement: [OBJC2]
364/// @required
365/// @optional
366///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000367void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
368 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000369 SmallVector<Decl *, 32> allMethods;
370 SmallVector<Decl *, 16> allProperties;
371 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000372 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek782f2f52010-01-07 01:20:12 +0000374 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000375
Steve Naroff294494e2007-08-22 16:35:03 +0000376 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000377 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000378 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniand30ec702012-07-26 17:32:28 +0000379 if (Decl *methodPrototype =
380 ParseObjCMethodPrototype(MethodImplKind, false))
381 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000382 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
383 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000384 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
385 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
386 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
387 if (Tok.is(tok::semi))
388 ConsumeToken();
389 }
Steve Naroff294494e2007-08-22 16:35:03 +0000390 continue;
391 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000392 if (Tok.is(tok::l_paren)) {
393 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000394 ParseObjCMethodDecl(Tok.getLocation(),
395 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000396 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000397 continue;
398 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000399 // Ignore excess semicolons.
400 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000401 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000402 continue;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnerbc662af2008-10-20 06:10:06 +0000405 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000406 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000407 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000409 // Code completion within an Objective-C interface.
410 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000411 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000412 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallf312b1e2010-08-26 23:41:50 +0000413 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000414 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000415 }
416
Chris Lattnere82a10f2008-10-20 05:46:22 +0000417 // If we don't have an @ directive, parse it as a function definition.
418 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000419 // The code below does not consume '}'s because it is afraid of eating the
420 // end of a namespace. Because of the way this code is structured, an
421 // erroneous r_brace would cause an infinite loop if not handled here.
422 if (Tok.is(tok::r_brace))
423 break;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000424 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000425 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000426 continue;
427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattnere82a10f2008-10-20 05:46:22 +0000429 // Otherwise, we have an @ directive, eat the @.
430 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000431 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000432 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000433 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000434 }
435
Chris Lattnera2449b22008-10-20 05:57:40 +0000436 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattnera2449b22008-10-20 05:57:40 +0000438 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000439 AtEnd.setBegin(AtLoc);
440 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000441 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000442 } else if (DirectiveKind == tok::objc_not_keyword) {
443 Diag(Tok, diag::err_objc_unknown_at);
444 SkipUntil(tok::semi);
445 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattnerbc662af2008-10-20 06:10:06 +0000448 // Eat the identifier.
449 ConsumeToken();
450
Chris Lattnera2449b22008-10-20 05:57:40 +0000451 switch (DirectiveKind) {
452 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000453 // FIXME: If someone forgets an @end on a protocol, this loop will
454 // continue to eat up tons of stuff and spew lots of nonsense errors. It
455 // would probably be better to bail out if we saw an @class or @interface
456 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000457 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000458 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000459 SkipUntil(tok::r_brace, tok::at);
460 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000461
462 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000463 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000464 Diag(AtLoc, diag::err_objc_missing_end)
465 << FixItHint::CreateInsertion(AtLoc, "@end\n");
466 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
467 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000468 ConsumeToken();
469 break;
470
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000472 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000473 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000474 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000475 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000476 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000477 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000478 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000479 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattnera2449b22008-10-20 05:57:40 +0000481 case tok::objc_property:
David Blaikie4e4d0842012-03-11 07:00:24 +0000482 if (!getLangOpts().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000483 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000484
Chris Lattnere82a10f2008-10-20 05:46:22 +0000485 ObjCDeclSpec OCDS;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000486 SourceLocation LParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000487 // Parse property attribute list, if any.
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000488 if (Tok.is(tok::l_paren)) {
489 LParenLoc = Tok.getLocation();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000490 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000493 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000494 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000495
Chris Lattnere82a10f2008-10-20 05:46:22 +0000496 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000497 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +0000498 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
John McCall7da19ea2011-03-26 01:53:26 +0000500 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000501 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000502 }
Steve Naroff294494e2007-08-22 16:35:03 +0000503 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000504
505 // We break out of the big loop in two cases: when we see @end or when we see
506 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000507 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000508 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000509 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000510 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000511 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000512 } else {
513 Diag(Tok, diag::err_objc_missing_end)
514 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
515 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
516 << (int) Actions.getObjCContainerKind();
517 AtEnd.setBegin(Tok.getLocation());
518 AtEnd.setEnd(Tok.getLocation());
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattnera2449b22008-10-20 05:57:40 +0000521 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000522 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000523 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000524 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000525 allProperties.data(), allProperties.size(),
526 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000527}
528
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000529/// Parse property attribute declarations.
530///
531/// property-attr-decl: '(' property-attrlist ')'
532/// property-attrlist:
533/// property-attribute
534/// property-attrlist ',' property-attribute
535/// property-attribute:
536/// getter '=' identifier
537/// setter '=' identifier ':'
538/// readonly
539/// readwrite
540/// assign
541/// retain
542/// copy
543/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000544/// atomic
545/// strong
546/// weak
547/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000548///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000549void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000550 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000551 BalancedDelimiterTracker T(*this, tok::l_paren);
552 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000554 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000555 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000556 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000557 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000558 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000559 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000561 // If this is not an identifier at all, bail out early.
562 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000563 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000564 return;
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner156b0612008-10-20 07:37:22 +0000567 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattner92e62b02008-11-20 04:42:34 +0000569 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000570 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000571 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000572 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000573 else if (II->isStr("unsafe_unretained"))
574 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000575 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000577 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000578 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000579 else if (II->isStr("strong"))
580 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000581 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000582 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000583 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000584 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000585 else if (II->isStr("atomic"))
586 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000587 else if (II->isStr("weak"))
588 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000589 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000590 bool IsSetter = II->getNameStart()[0] == 's';
591
Chris Lattnere00da7c2008-10-20 07:39:53 +0000592 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000593 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
594 diag::err_objc_expected_equal_for_getter;
595
596 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000597 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor4ad96852009-11-19 07:41:15 +0000599 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000600 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000601 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000602 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000603 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000604 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000605 }
606
Anders Carlsson42499be2010-10-02 17:45:21 +0000607
608 SourceLocation SelLoc;
609 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
610
611 if (!SelIdent) {
612 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
613 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000614 SkipUntil(tok::r_paren);
615 return;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson42499be2010-10-02 17:45:21 +0000618 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000619 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000620 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000622 if (ExpectAndConsume(tok::colon,
623 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000624 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000625 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000626 } else {
627 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000628 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000629 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000630 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000631 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000632 SkipUntil(tok::r_paren);
633 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Chris Lattner156b0612008-10-20 07:37:22 +0000636 if (Tok.isNot(tok::comma))
637 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Chris Lattner156b0612008-10-20 07:37:22 +0000639 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000642 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000643}
644
Steve Naroff3536b442007-09-06 21:24:23 +0000645/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000646/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000647/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000648///
649/// objc-instance-method: '-'
650/// objc-class-method: '+'
651///
Steve Naroff4985ace2007-08-22 18:35:33 +0000652/// objc-method-attributes: [OBJC2]
653/// __attribute__((deprecated))
654///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000655Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000656 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000657 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000658
Mike Stump1eb44332009-09-09 15:08:12 +0000659 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000660 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000661 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000662 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000663 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000664 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000665 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000666}
667
668/// objc-selector:
669/// identifier
670/// one of
671/// enum struct union if else while do for switch case default
672/// break continue return goto asm sizeof typeof __alignof
673/// unsigned long const short volatile signed restrict _Complex
674/// in out inout bycopy byref oneway int char float double void _Bool
675///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000676IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000677
Chris Lattnerff384912007-10-07 02:00:24 +0000678 switch (Tok.getKind()) {
679 default:
680 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000681 case tok::ampamp:
682 case tok::ampequal:
683 case tok::amp:
684 case tok::pipe:
685 case tok::tilde:
686 case tok::exclaim:
687 case tok::exclaimequal:
688 case tok::pipepipe:
689 case tok::pipeequal:
690 case tok::caret:
691 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000692 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000693 if (isalpha(ThisTok[0])) {
694 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
695 Tok.setKind(tok::identifier);
696 SelectorLoc = ConsumeToken();
697 return II;
698 }
699 return 0;
700 }
701
Chris Lattnerff384912007-10-07 02:00:24 +0000702 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000703 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000704 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000705 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000706 case tok::kw_break:
707 case tok::kw_case:
708 case tok::kw_catch:
709 case tok::kw_char:
710 case tok::kw_class:
711 case tok::kw_const:
712 case tok::kw_const_cast:
713 case tok::kw_continue:
714 case tok::kw_default:
715 case tok::kw_delete:
716 case tok::kw_do:
717 case tok::kw_double:
718 case tok::kw_dynamic_cast:
719 case tok::kw_else:
720 case tok::kw_enum:
721 case tok::kw_explicit:
722 case tok::kw_export:
723 case tok::kw_extern:
724 case tok::kw_false:
725 case tok::kw_float:
726 case tok::kw_for:
727 case tok::kw_friend:
728 case tok::kw_goto:
729 case tok::kw_if:
730 case tok::kw_inline:
731 case tok::kw_int:
732 case tok::kw_long:
733 case tok::kw_mutable:
734 case tok::kw_namespace:
735 case tok::kw_new:
736 case tok::kw_operator:
737 case tok::kw_private:
738 case tok::kw_protected:
739 case tok::kw_public:
740 case tok::kw_register:
741 case tok::kw_reinterpret_cast:
742 case tok::kw_restrict:
743 case tok::kw_return:
744 case tok::kw_short:
745 case tok::kw_signed:
746 case tok::kw_sizeof:
747 case tok::kw_static:
748 case tok::kw_static_cast:
749 case tok::kw_struct:
750 case tok::kw_switch:
751 case tok::kw_template:
752 case tok::kw_this:
753 case tok::kw_throw:
754 case tok::kw_true:
755 case tok::kw_try:
756 case tok::kw_typedef:
757 case tok::kw_typeid:
758 case tok::kw_typename:
759 case tok::kw_typeof:
760 case tok::kw_union:
761 case tok::kw_unsigned:
762 case tok::kw_using:
763 case tok::kw_virtual:
764 case tok::kw_void:
765 case tok::kw_volatile:
766 case tok::kw_wchar_t:
767 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000768 case tok::kw__Bool:
769 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000770 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000771 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000772 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000773 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000774 }
Steve Naroff294494e2007-08-22 16:35:03 +0000775}
776
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000777/// objc-for-collection-in: 'in'
778///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000779bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000780 // FIXME: May have to do additional look-ahead to only allow for
781 // valid tokens following an 'in'; such as an identifier, unary operators,
782 // '[' etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000783 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000784 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000785}
786
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000788/// qualifier list and builds their bitmask representation in the input
789/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000790///
791/// objc-type-qualifiers:
792/// objc-type-qualifier
793/// objc-type-qualifiers objc-type-qualifier
794///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000795void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000796 Declarator::TheContext Context) {
797 assert(Context == Declarator::ObjCParameterContext ||
798 Context == Declarator::ObjCResultContext);
799
Chris Lattnere8b724d2007-12-12 06:56:32 +0000800 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000801 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000802 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000803 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000804 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000805 }
806
Chris Lattnercb53b362007-12-27 19:57:00 +0000807 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000808 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattnere8b724d2007-12-12 06:56:32 +0000810 const IdentifierInfo *II = Tok.getIdentifierInfo();
811 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000813 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000815 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000816 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000817 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
819 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
820 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
821 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
822 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
823 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000824 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000826 ConsumeToken();
827 II = 0;
828 break;
829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattnere8b724d2007-12-12 06:56:32 +0000831 // If this wasn't a recognized qualifier, bail out.
832 if (II) return;
833 }
834}
835
John McCallcdda47f2011-10-01 09:56:14 +0000836/// Take all the decl attributes out of the given list and add
837/// them to the given attribute set.
838static void takeDeclAttributes(ParsedAttributes &attrs,
839 AttributeList *list) {
840 while (list) {
841 AttributeList *cur = list;
842 list = cur->getNext();
843
844 if (!cur->isUsedAsTypeAttr()) {
845 // Clear out the next pointer. We're really completely
846 // destroying the internal invariants of the declarator here,
847 // but it doesn't matter because we're done with it.
848 cur->setNext(0);
849 attrs.add(cur);
850 }
851 }
852}
853
854/// takeDeclAttributes - Take all the decl attributes from the given
855/// declarator and add them to the given list.
856static void takeDeclAttributes(ParsedAttributes &attrs,
857 Declarator &D) {
858 // First, take ownership of all attributes.
859 attrs.getPool().takeAllFrom(D.getAttributePool());
860 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
861
862 // Now actually move the attributes over.
863 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
864 takeDeclAttributes(attrs, D.getAttributes());
865 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
866 takeDeclAttributes(attrs,
867 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
868}
869
Chris Lattnere8b724d2007-12-12 06:56:32 +0000870/// objc-type-name:
871/// '(' objc-type-qualifiers[opt] type-name ')'
872/// '(' objc-type-qualifiers[opt] ')'
873///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000874ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000875 Declarator::TheContext context,
876 ParsedAttributes *paramAttrs) {
877 assert(context == Declarator::ObjCParameterContext ||
878 context == Declarator::ObjCResultContext);
879 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
880
Chris Lattnerdf195262007-10-09 17:51:17 +0000881 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000883 BalancedDelimiterTracker T(*this, tok::l_paren);
884 T.consumeOpen();
885
Chris Lattnere8904e92008-08-23 01:48:03 +0000886 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000887 ObjCDeclContextSwitch ObjCDC(*this);
888
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000889 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000890 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000891
John McCallb3d87482010-08-24 05:47:05 +0000892 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000893 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000894 // Parse an abstract declarator.
895 DeclSpec declSpec(AttrFactory);
896 declSpec.setObjCQualifiers(&DS);
897 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanian6d1de1b2012-05-29 21:52:45 +0000898 declSpec.SetRangeEnd(Tok.getLocation());
John McCallcdda47f2011-10-01 09:56:14 +0000899 Declarator declarator(declSpec, context);
900 ParseDeclarator(declarator);
901
902 // If that's not invalid, extract a type.
903 if (!declarator.isInvalidType()) {
904 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
905 if (!type.isInvalid())
906 Ty = type.get();
907
908 // If we're parsing a parameter, steal all the decl attributes
909 // and add them to the decl spec.
910 if (context == Declarator::ObjCParameterContext)
911 takeDeclAttributes(*paramAttrs, declarator);
912 }
913 } else if (context == Declarator::ObjCResultContext &&
914 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000915 if (!Ident_instancetype)
916 Ident_instancetype = PP.getIdentifierInfo("instancetype");
917
918 if (Tok.getIdentifierInfo() == Ident_instancetype) {
919 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
920 ConsumeToken();
921 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000922 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000923
Steve Naroffd7333c22008-10-21 14:15:04 +0000924 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000925 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000926 else if (Tok.getLocation() == TypeStartLoc) {
927 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000928 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000929 SkipUntil(tok::r_paren);
930 } else {
931 // Otherwise, we found *something*, but didn't get a ')' in the right
932 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000933 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000934 }
Steve Narofff28b2642007-09-05 23:30:30 +0000935 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000936}
937
938/// objc-method-decl:
939/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000940/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000941/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000942/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000943///
944/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000945/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000946/// objc-keyword-selector objc-keyword-decl
947///
948/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000949/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
950/// objc-selector ':' objc-keyword-attributes[opt] identifier
951/// ':' objc-type-name objc-keyword-attributes[opt] identifier
952/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000953///
Steve Naroff4985ace2007-08-22 18:35:33 +0000954/// objc-parmlist:
955/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000956///
Steve Naroff4985ace2007-08-22 18:35:33 +0000957/// objc-parms:
958/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000959///
Steve Naroff4985ace2007-08-22 18:35:33 +0000960/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000961/// , ...
962///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000963/// objc-keyword-attributes: [OBJC2]
964/// __attribute__((unused))
965///
John McCalld226f652010-08-21 09:40:31 +0000966Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000967 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000968 tok::ObjCKeywordKind MethodImplKind,
969 bool MethodDefinition) {
John McCall92576642012-05-07 06:16:41 +0000970 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall54abf7d2009-11-04 02:18:39 +0000971
Douglas Gregore8f5a172010-04-07 00:21:17 +0000972 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000973 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000974 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000975 cutOffParsing();
976 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000977 }
978
Chris Lattnere8904e92008-08-23 01:48:03 +0000979 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000980 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000981 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000982 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000983 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenek9e049352010-02-18 23:05:16 +0000985 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000986 ParsedAttributes methodAttrs(AttrFactory);
David Blaikie4e4d0842012-03-11 07:00:24 +0000987 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000988 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000989
Douglas Gregore8f5a172010-04-07 00:21:17 +0000990 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000991 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000992 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000993 cutOffParsing();
994 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000995 }
996
Ted Kremenek9e049352010-02-18 23:05:16 +0000997 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000998 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000999 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +00001000
Steve Naroff84c43102009-02-11 20:43:13 +00001001 // An unnamed colon is valid.
1002 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001003 Diag(Tok, diag::err_expected_selector_for_method)
1004 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniand30ec702012-07-26 17:32:28 +00001005 // Skip until we get a ; or @.
1006 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCalld226f652010-08-21 09:40:31 +00001007 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +00001008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattner5f9e2722011-07-23 10:55:15 +00001010 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001011 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001012 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001013 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001014 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattnerff384912007-10-07 02:00:24 +00001016 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001017 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001018 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001019 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001020 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001021 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001022 methodAttrs.getList(), MethodImplKind,
1023 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001024 PD.complete(Result);
1025 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001026 }
Steve Narofff28b2642007-09-05 23:30:30 +00001027
Chris Lattner5f9e2722011-07-23 10:55:15 +00001028 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001029 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001030 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001031 ParseScope PrototypeScope(this,
1032 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001033
1034 AttributePool allParamAttrs(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001035 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001036 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001037 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Chris Lattnerff384912007-10-07 02:00:24 +00001039 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001040 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001041 Diag(Tok, diag::err_expected_colon);
1042 break;
1043 }
1044 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001045
John McCallb3d87482010-08-24 05:47:05 +00001046 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001047 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001048 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1049 Declarator::ObjCParameterContext,
1050 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001051
Chris Lattnerff384912007-10-07 02:00:24 +00001052 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001053 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001054 ArgInfo.ArgAttrs = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001055 if (getLangOpts().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001056 MaybeParseGNUAttributes(paramAttrs);
1057 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001058 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001059
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001060 // Code completion for the next piece of the selector.
1061 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001062 KeyIdents.push_back(SelIdent);
1063 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1064 mType == tok::minus,
1065 /*AtParameterName=*/true,
1066 ReturnType,
1067 KeyIdents.data(),
1068 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001069 cutOffParsing();
1070 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001071 }
1072
Chris Lattnerdf195262007-10-09 17:51:17 +00001073 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001074 Diag(Tok, diag::err_expected_ident); // missing argument name.
1075 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattnere294d3f2009-04-11 18:57:04 +00001078 ArgInfo.Name = Tok.getIdentifierInfo();
1079 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001080 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Chris Lattnere294d3f2009-04-11 18:57:04 +00001082 ArgInfos.push_back(ArgInfo);
1083 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001084 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001085
John McCall0b7e6782011-03-24 11:26:52 +00001086 // Make sure the attributes persist.
1087 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1088
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001089 // Code completion for the next piece of the selector.
1090 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001091 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1092 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001093 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001094 ReturnType,
1095 KeyIdents.data(),
1096 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001097 cutOffParsing();
1098 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001099 }
1100
Chris Lattnerff384912007-10-07 02:00:24 +00001101 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001102 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenekb648cf02012-09-12 16:50:35 +00001103 if (!SelIdent && Tok.isNot(tok::colon))
1104 break;
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001105 if (!SelIdent) {
Fariborz Jahanian08602352012-09-17 19:15:26 +00001106 SourceLocation ColonLoc = Tok.getLocation();
1107 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian03ebd3b2012-09-17 23:09:59 +00001108 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1109 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1110 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanian08602352012-09-17 19:15:26 +00001111 }
1112 }
Chris Lattnerff384912007-10-07 02:00:24 +00001113 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Steve Naroff335eafa2007-11-15 12:35:21 +00001116 bool isVariadic = false;
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001117 bool cStyleParamWarned = false;
Chris Lattnerff384912007-10-07 02:00:24 +00001118 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001119 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001120 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001121 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001122 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001123 ConsumeToken();
1124 break;
1125 }
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001126 if (!cStyleParamWarned) {
1127 Diag(Tok, diag::warn_cstyle_param);
1128 cStyleParamWarned = true;
1129 }
John McCall0b7e6782011-03-24 11:26:52 +00001130 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001131 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001132 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001133 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1134 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001135 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001136 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001137 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1138 ParmDecl.getIdentifierLoc(),
1139 Param,
1140 0));
Chris Lattnerff384912007-10-07 02:00:24 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001143 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001144 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001145 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001146 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001147
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001148 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001149 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001150
Chris Lattnerff384912007-10-07 02:00:24 +00001151 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1152 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001153 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001154 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001155 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001156 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001157 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001158 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001159 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001160
John McCall54abf7d2009-11-04 02:18:39 +00001161 PD.complete(Result);
1162 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001163}
1164
Steve Naroffdac269b2007-08-20 21:31:48 +00001165/// objc-protocol-refs:
1166/// '<' identifier-list '>'
1167///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001168bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001169ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1170 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001171 bool WarnOnDeclarations,
1172 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001173 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001175 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Chris Lattner5f9e2722011-07-23 10:55:15 +00001177 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattnere13b9592008-07-26 04:03:38 +00001179 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001180 if (Tok.is(tok::code_completion)) {
1181 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1182 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001183 cutOffParsing();
1184 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001185 }
1186
Chris Lattnere13b9592008-07-26 04:03:38 +00001187 if (Tok.isNot(tok::identifier)) {
1188 Diag(Tok, diag::err_expected_ident);
1189 SkipUntil(tok::greater);
1190 return true;
1191 }
1192 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1193 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001194 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001195 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Chris Lattnere13b9592008-07-26 04:03:38 +00001197 if (Tok.isNot(tok::comma))
1198 break;
1199 ConsumeToken();
1200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Chris Lattnere13b9592008-07-26 04:03:38 +00001202 // Consume the '>'.
Nico Weberb707a472012-12-14 18:22:38 +00001203 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattnere13b9592008-07-26 04:03:38 +00001204 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Chris Lattnere13b9592008-07-26 04:03:38 +00001206 // Convert the list of protocols identifiers into a list of protocol decls.
1207 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1208 &ProtocolIdents[0], ProtocolIdents.size(),
1209 Protocols);
1210 return false;
1211}
1212
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001213/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1214/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001215bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001216 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikie4e4d0842012-03-11 07:00:24 +00001217 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001218 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001219 SmallVector<Decl *, 8> ProtocolDecl;
1220 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001221 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1222 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001223 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1224 ProtocolLocs.data(), LAngleLoc);
1225 if (EndProtoLoc.isValid())
1226 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001227 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001228}
1229
1230
Steve Naroffdac269b2007-08-20 21:31:48 +00001231/// objc-class-instance-variables:
1232/// '{' objc-instance-variable-decl-list[opt] '}'
1233///
1234/// objc-instance-variable-decl-list:
1235/// objc-visibility-spec
1236/// objc-instance-variable-decl ';'
1237/// ';'
1238/// objc-instance-variable-decl-list objc-visibility-spec
1239/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1240/// objc-instance-variable-decl-list ';'
1241///
1242/// objc-visibility-spec:
1243/// @private
1244/// @protected
1245/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001246/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001247///
1248/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001249/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001250///
John McCalld226f652010-08-21 09:40:31 +00001251void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001252 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001253 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001255 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001256
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001257 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001258 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001259
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001260 BalancedDelimiterTracker T(*this, tok::l_brace);
1261 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Steve Naroffddbff782007-08-21 21:17:12 +00001263 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001264 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001265 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Steve Naroffddbff782007-08-21 21:17:12 +00001267 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001268 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001269 ConsumeExtraSemi(InstanceVariableList);
Steve Naroffddbff782007-08-21 21:17:12 +00001270 continue;
1271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Steve Naroffddbff782007-08-21 21:17:12 +00001273 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001274 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001275 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001276
1277 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001278 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001279 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001280 }
1281
Steve Naroff861cf3e2007-08-23 18:16:40 +00001282 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001283 case tok::objc_private:
1284 case tok::objc_public:
1285 case tok::objc_protected:
1286 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001287 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001288 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001289 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001290 default:
1291 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001292 continue;
1293 }
1294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001296 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001297 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001298 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001299 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001300 }
1301
John McCallbdd563e2009-11-03 02:38:08 +00001302 struct ObjCIvarCallback : FieldCallback {
1303 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001304 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001305 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001306 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001307
John McCalld226f652010-08-21 09:40:31 +00001308 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001309 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001310 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1311 }
1312
Eli Friedmandcdff462012-08-08 23:53:27 +00001313 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001314 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001315 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001316 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001317 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001318 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001319 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001320 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001321 if (Field)
1322 AllIvarDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001323 FD.complete(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001324 }
1325 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001326
Chris Lattnere1359422008-04-10 06:46:29 +00001327 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001328 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001329 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Chris Lattnerdf195262007-10-09 17:51:17 +00001331 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001332 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001333 } else {
1334 Diag(Tok, diag::err_expected_semi_decl_list);
1335 // Skip to end of block or statement
1336 SkipUntil(tok::r_brace, true, true);
1337 }
1338 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001339 T.consumeClose();
1340
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001341 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001342 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001343 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001344 // Call ActOnFields() even if we don't have any decls. This is useful
1345 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001346 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001347 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001348 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001349 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001350}
Steve Naroffdac269b2007-08-20 21:31:48 +00001351
1352/// objc-protocol-declaration:
1353/// objc-protocol-definition
1354/// objc-protocol-forward-reference
1355///
1356/// objc-protocol-definition:
James Dennett17d26a62012-06-11 06:19:40 +00001357/// \@protocol identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001358/// objc-protocol-refs[opt]
1359/// objc-interface-decl-list
James Dennett17d26a62012-06-11 06:19:40 +00001360/// \@end
Steve Naroffdac269b2007-08-20 21:31:48 +00001361///
1362/// objc-protocol-forward-reference:
James Dennett17d26a62012-06-11 06:19:40 +00001363/// \@protocol identifier-list ';'
Steve Naroffdac269b2007-08-20 21:31:48 +00001364///
James Dennett17d26a62012-06-11 06:19:40 +00001365/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001366/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001367/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001368Parser::DeclGroupPtrTy
1369Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1370 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001371 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001372 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1373 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregor083128f2009-11-18 04:49:41 +00001375 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001376 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001377 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001378 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001379 }
1380
Chris Lattnerdf195262007-10-09 17:51:17 +00001381 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001382 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001383 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001384 }
1385 // Save the protocol name, then consume it.
1386 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1387 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Chris Lattnerdf195262007-10-09 17:51:17 +00001389 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001390 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001391 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001392 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001393 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001394 }
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001396 CheckNestedObjCContexts(AtLoc);
1397
Chris Lattnerdf195262007-10-09 17:51:17 +00001398 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001399 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001400 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1401
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001402 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001403 while (1) {
1404 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001405 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001406 Diag(Tok, diag::err_expected_ident);
1407 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001408 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001409 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001410 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1411 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001412 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Chris Lattnerdf195262007-10-09 17:51:17 +00001414 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001415 break;
1416 }
1417 // Consume the ';'.
1418 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001419 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Steve Naroffe440eb82007-10-10 17:32:04 +00001421 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001422 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001423 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001424 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001425 }
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001427 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001428 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001429
Chris Lattner5f9e2722011-07-23 10:55:15 +00001430 SmallVector<Decl *, 8> ProtocolRefs;
1431 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001432 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001433 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1434 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001435 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001436
John McCalld226f652010-08-21 09:40:31 +00001437 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001438 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001439 ProtocolRefs.data(),
1440 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001441 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001442 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001443
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001444 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001445 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001446}
Steve Naroffdac269b2007-08-20 21:31:48 +00001447
1448/// objc-implementation:
1449/// objc-class-implementation-prologue
1450/// objc-category-implementation-prologue
1451///
1452/// objc-class-implementation-prologue:
1453/// @implementation identifier objc-superclass[opt]
1454/// objc-class-instance-variables[opt]
1455///
1456/// objc-category-implementation-prologue:
1457/// @implementation identifier ( identifier )
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001458Parser::DeclGroupPtrTy
1459Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1461 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001462 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001463 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001465 // Code completion after '@implementation'.
1466 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001467 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001468 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001469 return DeclGroupPtrTy();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001470 }
1471
Chris Lattnerdf195262007-10-09 17:51:17 +00001472 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001473 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001474 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001475 }
1476 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001477 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001478 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001479 Decl *ObjCImpDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001480
1481 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001482 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001483 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001484 SourceLocation categoryLoc, rparenLoc;
1485 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001487 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001488 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001489 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001490 return DeclGroupPtrTy();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001491 }
1492
Chris Lattnerdf195262007-10-09 17:51:17 +00001493 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001494 categoryId = Tok.getIdentifierInfo();
1495 categoryLoc = ConsumeToken();
1496 } else {
1497 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001498 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001499 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001500 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001501 Diag(Tok, diag::err_expected_rparen);
1502 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001503 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001504 }
1505 rparenLoc = ConsumeParen();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001506 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001507 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001508 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001509
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001510 } else {
1511 // We have a class implementation
1512 SourceLocation superClassLoc;
1513 IdentifierInfo *superClassId = 0;
1514 if (Tok.is(tok::colon)) {
1515 // We have a super class
1516 ConsumeToken();
1517 if (Tok.isNot(tok::identifier)) {
1518 Diag(Tok, diag::err_expected_ident); // missing super class name.
1519 return DeclGroupPtrTy();
1520 }
1521 superClassId = Tok.getIdentifierInfo();
1522 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001523 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001524 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1525 AtLoc, nameId, nameLoc,
1526 superClassId, superClassLoc);
1527
1528 if (Tok.is(tok::l_brace)) // we have ivars
1529 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001530 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001531 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001533 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001534
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001535 {
1536 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1537 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1538 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001539 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001540 MaybeParseMicrosoftAttributes(attrs);
1541 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1542 DeclGroupRef DG = DGP.get();
1543 DeclsInGroup.append(DG.begin(), DG.end());
1544 }
1545 }
1546 }
1547
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001548 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001549}
Steve Naroff60fccee2007-10-29 21:38:07 +00001550
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001551Parser::DeclGroupPtrTy
1552Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001553 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1554 "ParseObjCAtEndDeclaration(): Expected @end");
1555 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001556 if (CurParsedObjCImpl)
1557 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001558 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001559 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001560 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001561 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001562}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001563
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001564Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1565 if (!Finished) {
1566 finish(P.Tok.getLocation());
1567 if (P.Tok.is(tok::eof)) {
1568 P.Diag(P.Tok, diag::err_objc_missing_end)
1569 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1570 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1571 << Sema::OCK_Implementation;
1572 }
1573 }
1574 P.CurParsedObjCImpl = 0;
1575 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001576}
1577
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001578void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1579 assert(!Finished);
1580 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1581 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001582 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1583 true/*Methods*/);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001584
1585 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1586
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001587 if (HasCFunction)
1588 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1589 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1590 false/*c-functions*/);
1591
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001592 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001593 for (LateParsedObjCMethodContainer::iterator
1594 I = LateParsedObjCMethods.begin(),
1595 E = LateParsedObjCMethods.end(); I != E; ++I)
1596 delete *I;
1597 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001598
1599 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001600}
1601
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001602/// compatibility-alias-decl:
1603/// @compatibility_alias alias-name class-name ';'
1604///
John McCalld226f652010-08-21 09:40:31 +00001605Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001606 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1607 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1608 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001609 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001610 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001611 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001612 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001613 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1614 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001615 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001616 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001617 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001618 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001619 IdentifierInfo *classId = Tok.getIdentifierInfo();
1620 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001621 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1622 "@compatibility_alias");
Richard Smithde01b7a2012-08-08 23:32:13 +00001623 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1624 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001625}
1626
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001627/// property-synthesis:
1628/// @synthesize property-ivar-list ';'
1629///
1630/// property-ivar-list:
1631/// property-ivar
1632/// property-ivar-list ',' property-ivar
1633///
1634/// property-ivar:
1635/// identifier
1636/// identifier '=' identifier
1637///
John McCalld226f652010-08-21 09:40:31 +00001638Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001639 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1640 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001641 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregorb328c422009-11-18 19:45:45 +00001643 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001644 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001645 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001646 cutOffParsing();
1647 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001648 }
1649
Douglas Gregorb328c422009-11-18 19:45:45 +00001650 if (Tok.isNot(tok::identifier)) {
1651 Diag(Tok, diag::err_synthesized_property_name);
1652 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001653 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001654 }
1655
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001656 IdentifierInfo *propertyIvar = 0;
1657 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1658 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001659 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001660 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001661 // property '=' ivar-name
1662 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001663
1664 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001665 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001666 cutOffParsing();
1667 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001668 }
1669
Chris Lattnerdf195262007-10-09 17:51:17 +00001670 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001671 Diag(Tok, diag::err_expected_ident);
1672 break;
1673 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001674 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001675 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001676 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001677 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001678 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001679 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001680 break;
1681 ConsumeToken(); // consume ','
1682 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001683 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001684 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001685}
1686
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001687/// property-dynamic:
1688/// @dynamic property-list
1689///
1690/// property-list:
1691/// identifier
1692/// property-list ',' identifier
1693///
John McCalld226f652010-08-21 09:40:31 +00001694Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001695 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1696 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001697 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001698 while (true) {
1699 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001700 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001701 cutOffParsing();
1702 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001703 }
1704
1705 if (Tok.isNot(tok::identifier)) {
1706 Diag(Tok, diag::err_expected_ident);
1707 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001708 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001709 }
1710
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001711 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1712 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001713 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001714 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001715
Chris Lattnerdf195262007-10-09 17:51:17 +00001716 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001717 break;
1718 ConsumeToken(); // consume ','
1719 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001720 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001721 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001722}
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001724/// objc-throw-statement:
1725/// throw expression[opt];
1726///
John McCall60d7b3a2010-08-24 06:29:42 +00001727StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1728 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001729 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001730 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001731 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001732 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001733 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001734 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001735 }
1736 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001737 // consume ';'
1738 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001739 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001740}
1741
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001742/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001743/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001744///
John McCall60d7b3a2010-08-24 06:29:42 +00001745StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001746Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001747 ConsumeToken(); // consume synchronized
1748 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001749 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001750 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001751 }
John McCall07524032011-07-27 21:50:02 +00001752
1753 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001754 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001755 ExprResult operand(ParseExpression());
1756
1757 if (Tok.is(tok::r_paren)) {
1758 ConsumeParen(); // ')'
1759 } else {
1760 if (!operand.isInvalid())
1761 Diag(Tok, diag::err_expected_rparen);
1762
1763 // Skip forward until we see a left brace, but don't consume it.
1764 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001765 }
John McCall07524032011-07-27 21:50:02 +00001766
1767 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001768 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001769 if (!operand.isInvalid())
1770 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001771 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001772 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001773
John McCall07524032011-07-27 21:50:02 +00001774 // Check the @synchronized operand now.
1775 if (!operand.isInvalid())
1776 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001777
John McCall07524032011-07-27 21:50:02 +00001778 // Parse the compound statement within a new scope.
1779 ParseScope bodyScope(this, Scope::DeclScope);
1780 StmtResult body(ParseCompoundStatementBody());
1781 bodyScope.Exit();
1782
1783 // If there was a semantic or parse error earlier with the
1784 // operand, fail now.
1785 if (operand.isInvalid())
1786 return StmtError();
1787
1788 if (body.isInvalid())
1789 body = Actions.ActOnNullStmt(Tok.getLocation());
1790
1791 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001792}
1793
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001794/// objc-try-catch-statement:
1795/// @try compound-statement objc-catch-list[opt]
1796/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1797///
1798/// objc-catch-list:
1799/// @catch ( parameter-declaration ) compound-statement
1800/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1801/// catch-parameter-declaration:
1802/// parameter-declaration
1803/// '...' [OBJC2]
1804///
John McCall60d7b3a2010-08-24 06:29:42 +00001805StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001806 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001807
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001808 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001809 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001810 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001811 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001812 }
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001813 StmtVector CatchStmts;
John McCall60d7b3a2010-08-24 06:29:42 +00001814 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001815 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001816 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001817 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001818 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001819 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001820
Chris Lattnerdf195262007-10-09 17:51:17 +00001821 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001822 // At this point, we need to lookahead to determine if this @ is the start
1823 // of an @catch or @finally. We don't want to consume the @ token if this
1824 // is an @try or @encode or something else.
1825 Token AfterAt = GetLookAheadToken(1);
1826 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1827 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1828 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001829
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001830 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001831 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001832 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001833 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001834 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001835 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001836 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001837 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001838 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001839 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001840 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001841 ParseDeclarator(ParmDecl);
1842
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001843 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001844 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001845 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001846 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001847 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Steve Naroff93a25952009-04-07 22:56:58 +00001849 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Steve Naroff93a25952009-04-07 22:56:58 +00001851 if (Tok.is(tok::r_paren))
1852 RParenLoc = ConsumeParen();
1853 else // Skip over garbage, until we get to ')'. Eat the ')'.
1854 SkipUntil(tok::r_paren, true, false);
1855
John McCall60d7b3a2010-08-24 06:29:42 +00001856 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001857 if (Tok.is(tok::l_brace))
1858 CatchBody = ParseCompoundStatementBody();
1859 else
1860 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001861 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001862 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001863
John McCall60d7b3a2010-08-24 06:29:42 +00001864 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001865 RParenLoc,
1866 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001867 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001868 if (!Catch.isInvalid())
1869 CatchStmts.push_back(Catch.release());
1870
Steve Naroff64515f32008-02-05 21:27:35 +00001871 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001872 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1873 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001874 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001875 }
1876 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001877 } else {
1878 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001879 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001880 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001881
John McCall60d7b3a2010-08-24 06:29:42 +00001882 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001883 if (Tok.is(tok::l_brace))
1884 FinallyBody = ParseCompoundStatementBody();
1885 else
1886 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001887 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001888 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001889 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001890 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001891 catch_or_finally_seen = true;
1892 break;
1893 }
1894 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001895 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001896 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001897 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001898 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001899
John McCall9ae2f072010-08-23 23:25:46 +00001900 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001901 CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001902 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001903}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001904
John McCallf85e1932011-06-15 23:02:42 +00001905/// objc-autoreleasepool-statement:
1906/// @autoreleasepool compound-statement
1907///
1908StmtResult
1909Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1910 ConsumeToken(); // consume autoreleasepool
1911 if (Tok.isNot(tok::l_brace)) {
1912 Diag(Tok, diag::err_expected_lbrace);
1913 return StmtError();
1914 }
1915 // Enter a scope to hold everything within the compound stmt. Compound
1916 // statements can always hold declarations.
1917 ParseScope BodyScope(this, Scope::DeclScope);
1918
1919 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1920
1921 BodyScope.Exit();
1922 if (AutoreleasePoolBody.isInvalid())
1923 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1924 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1925 AutoreleasePoolBody.take());
1926}
1927
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001928/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1929/// for later parsing.
1930void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1931 LexedMethod* LM = new LexedMethod(this, MDecl);
1932 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1933 CachedTokens &Toks = LM->Toks;
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001934 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001935 Toks.push_back(Tok);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001936 if (Tok.is(tok::kw_try)) {
1937 ConsumeToken();
Fariborz Jahaniandbd69452012-08-10 20:34:17 +00001938 if (Tok.is(tok::colon)) {
1939 Toks.push_back(Tok);
1940 ConsumeToken();
1941 while (Tok.isNot(tok::l_brace)) {
1942 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1943 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1944 }
1945 }
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001946 Toks.push_back(Tok); // also store '{'
1947 }
1948 else if (Tok.is(tok::colon)) {
1949 ConsumeToken();
1950 while (Tok.isNot(tok::l_brace)) {
1951 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1952 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1953 }
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001954 Toks.push_back(Tok); // also store '{'
1955 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001956 ConsumeBrace();
1957 // Consume everything up to (and including) the matching right brace.
1958 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001959 while (Tok.is(tok::kw_catch)) {
1960 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1961 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1962 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001963}
1964
Steve Naroff3536b442007-09-06 21:24:23 +00001965/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001966///
John McCalld226f652010-08-21 09:40:31 +00001967Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001968 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001969
John McCallf312b1e2010-08-26 23:41:50 +00001970 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1971 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001973 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001974 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001975 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001976 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001977 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001978 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001979 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001980 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001981
Steve Naroff409be832007-11-11 19:54:21 +00001982 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001983 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001984 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Steve Naroff409be832007-11-11 19:54:21 +00001986 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1987 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Steve Naroff409be832007-11-11 19:54:21 +00001989 // If we didn't find the '{', bail out.
1990 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001991 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001992 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001993
1994 if (!MDecl) {
1995 ConsumeBrace();
1996 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1997 return 0;
1998 }
1999
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002000 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002001 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahanianc9b97092012-08-09 17:15:00 +00002002 assert (CurParsedObjCImpl
2003 && "ParseObjCMethodDefinition - Method out of @implementation");
2004 // Consume the tokens and store them for later parsing.
2005 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff71c0a952007-11-13 23:01:27 +00002006 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00002007}
Anders Carlsson55085182007-08-21 17:43:55 +00002008
John McCall60d7b3a2010-08-24 06:29:42 +00002009StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002010 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002011 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002012 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002013 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00002014 }
2015
2016 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00002017 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002018
2019 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00002020 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002021
2022 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00002023 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00002024
2025 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2026 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002027
John McCall60d7b3a2010-08-24 06:29:42 +00002028 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002029 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00002030 // If the expression is invalid, skip ahead to the next semicolon. Not
2031 // doing this opens us up to the possibility of infinite loops if
2032 // ParseExpression does not consume any tokens.
2033 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00002034 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00002035 }
Chris Lattner5d803162009-12-07 16:33:19 +00002036
Steve Naroff64515f32008-02-05 21:27:35 +00002037 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00002038 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +00002039 return Actions.ActOnExprStmt(Res);
Steve Naroff64515f32008-02-05 21:27:35 +00002040}
2041
John McCall60d7b3a2010-08-24 06:29:42 +00002042ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002043 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002044 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002045 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002046 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002047 return ExprError();
2048
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002049 case tok::minus:
2050 case tok::plus: {
2051 tok::TokenKind Kind = Tok.getKind();
2052 SourceLocation OpLoc = ConsumeToken();
2053
2054 if (!Tok.is(tok::numeric_constant)) {
2055 const char *Symbol = 0;
2056 switch (Kind) {
2057 case tok::minus: Symbol = "-"; break;
2058 case tok::plus: Symbol = "+"; break;
2059 default: llvm_unreachable("missing unary operator case");
2060 }
2061 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2062 << Symbol;
2063 return ExprError();
2064 }
2065
2066 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2067 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002068 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002069 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002070 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002071
2072 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2073 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002074 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002075
2076 return ParsePostfixExpressionSuffix(
2077 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2078 }
2079
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002080 case tok::string_literal: // primary-expression: string-literal
2081 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002082 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002083
2084 case tok::char_constant:
2085 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2086
2087 case tok::numeric_constant:
2088 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2089
2090 case tok::kw_true: // Objective-C++, etc.
2091 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2092 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2093 case tok::kw_false: // Objective-C++, etc.
2094 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2095 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2096
2097 case tok::l_square:
2098 // Objective-C array literal
2099 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2100
2101 case tok::l_brace:
2102 // Objective-C dictionary literal
2103 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2104
Patrick Beardeb382ec2012-04-19 00:25:12 +00002105 case tok::l_paren:
2106 // Objective-C boxed expression
2107 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2108
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002109 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002110 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002111 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002112
Chris Lattner4fef81d2008-08-05 06:19:09 +00002113 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2114 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002115 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002116 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002117 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002118 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002119 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian6749ae12012-07-09 20:00:35 +00002120 default: {
2121 const char *str = 0;
2122 if (GetLookAheadToken(1).is(tok::l_brace)) {
2123 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2124 str =
2125 ch == 't' ? "try"
2126 : (ch == 'f' ? "finally"
2127 : (ch == 'a' ? "autoreleasepool" : 0));
2128 }
2129 if (str) {
2130 SourceLocation kwLoc = Tok.getLocation();
2131 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2132 FixItHint::CreateReplacement(kwLoc, str));
2133 }
2134 else
2135 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2136 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002137 }
Anders Carlsson55085182007-08-21 17:43:55 +00002138 }
Anders Carlsson55085182007-08-21 17:43:55 +00002139}
2140
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002141/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002142///
2143/// This routine parses the receiver of a message send in
2144/// Objective-C++ either as a type or as an expression. Note that this
2145/// routine must not be called to parse a send to 'super', since it
2146/// has no way to return such a result.
2147///
2148/// \param IsExpr Whether the receiver was parsed as an expression.
2149///
2150/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2151/// IsExpr is true), the parsed expression. If the receiver was parsed
2152/// as a type (\c IsExpr is false), the parsed type.
2153///
2154/// \returns True if an error occurred during parsing or semantic
2155/// analysis, in which case the arguments do not have valid
2156/// values. Otherwise, returns false for a successful parse.
2157///
2158/// objc-receiver: [C++]
2159/// 'super' [not parsed here]
2160/// expression
2161/// simple-type-specifier
2162/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002163bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002164 InMessageExpressionRAIIObject InMessage(*this, true);
2165
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002166 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2167 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2168 TryAnnotateTypeOrScopeToken();
2169
Kaelyn Uhrain7bf33402012-06-15 23:45:51 +00002170 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002171 // objc-receiver:
2172 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002173 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002174 if (Receiver.isInvalid())
2175 return true;
2176
2177 IsExpr = true;
2178 TypeOrExpr = Receiver.take();
2179 return false;
2180 }
2181
2182 // objc-receiver:
2183 // typename-specifier
2184 // simple-type-specifier
2185 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002186 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002187 ParseCXXSimpleTypeSpecifier(DS);
2188
2189 if (Tok.is(tok::l_paren)) {
2190 // If we see an opening parentheses at this point, we are
2191 // actually parsing an expression that starts with a
2192 // function-style cast, e.g.,
2193 //
2194 // postfix-expression:
2195 // simple-type-specifier ( expression-list [opt] )
2196 // typename-specifier ( expression-list [opt] )
2197 //
2198 // Parse the remainder of this case, then the (optional)
2199 // postfix-expression suffix, followed by the (optional)
2200 // right-hand side of the binary expression. We have an
2201 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002202 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002203 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002204 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002205 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002206 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002207 if (Receiver.isInvalid())
2208 return true;
2209
2210 IsExpr = true;
2211 TypeOrExpr = Receiver.take();
2212 return false;
2213 }
2214
2215 // We have a class message. Turn the simple-type-specifier or
2216 // typename-specifier we parsed into a type and parse the
2217 // remainder of the class message.
2218 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002219 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002220 if (Type.isInvalid())
2221 return true;
2222
2223 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002224 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002225 return false;
2226}
2227
Douglas Gregor1b730e82010-05-31 14:40:22 +00002228/// \brief Determine whether the parser is currently referring to a an
2229/// Objective-C message send, using a simplified heuristic to avoid overhead.
2230///
2231/// This routine will only return true for a subset of valid message-send
2232/// expressions.
2233bool Parser::isSimpleObjCMessageExpression() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002234 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002235 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002236 return GetLookAheadToken(1).is(tok::identifier) &&
2237 GetLookAheadToken(2).is(tok::identifier);
2238}
2239
Douglas Gregor9497a732010-09-16 01:51:54 +00002240bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002241 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregor9497a732010-09-16 01:51:54 +00002242 InMessageExpression)
2243 return false;
2244
2245
2246 ParsedType Type;
2247
2248 if (Tok.is(tok::annot_typename))
2249 Type = getTypeAnnotation(Tok);
2250 else if (Tok.is(tok::identifier))
2251 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2252 getCurScope());
2253 else
2254 return false;
2255
2256 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2257 const Token &AfterNext = GetLookAheadToken(2);
2258 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2259 if (Tok.is(tok::identifier))
2260 TryAnnotateTypeOrScopeToken();
2261
2262 return Tok.is(tok::annot_typename);
2263 }
2264 }
2265
2266 return false;
2267}
2268
Mike Stump1eb44332009-09-09 15:08:12 +00002269/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002270/// '[' objc-receiver objc-message-args ']'
2271///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002272/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002273/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002274/// expression
2275/// class-name
2276/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002277///
John McCall60d7b3a2010-08-24 06:29:42 +00002278ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002279 assert(Tok.is(tok::l_square) && "'[' expected");
2280 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2281
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002282 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002283 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002284 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002285 return ExprError();
2286 }
2287
Douglas Gregor0fbda682010-09-15 14:51:05 +00002288 InMessageExpressionRAIIObject InMessage(*this, true);
2289
David Blaikie4e4d0842012-03-11 07:00:24 +00002290 if (getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002291 // We completely separate the C and C++ cases because C++ requires
2292 // more complicated (read: slower) parsing.
2293
2294 // Handle send to super.
2295 // FIXME: This doesn't benefit from the same typo-correction we
2296 // get in Objective-C.
2297 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002298 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002299 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2300 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002301
2302 // Parse the receiver, which is either a type or an expression.
2303 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002304 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002305 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2306 SkipUntil(tok::r_square);
2307 return ExprError();
2308 }
2309
2310 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002311 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2312 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002313 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002314
2315 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002316 ParsedType::getFromOpaquePtr(TypeOrExpr),
2317 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002318 }
2319
2320 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002321 IdentifierInfo *Name = Tok.getIdentifierInfo();
2322 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002323 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002324 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002325 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002326 NextToken().is(tok::period),
2327 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002328 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002329 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2330 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002331
John McCallf312b1e2010-08-26 23:41:50 +00002332 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002333 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002334 SkipUntil(tok::r_square);
2335 return ExprError();
2336 }
2337
Douglas Gregor1569f952010-04-21 20:38:13 +00002338 ConsumeToken(); // the type name
2339
2340 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002341 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002342
John McCallf312b1e2010-08-26 23:41:50 +00002343 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002344 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002345 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002346 }
Chris Lattner699b6612008-01-25 18:59:06 +00002347 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002348
2349 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002350 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002351 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002352 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002353 return Res;
Chris Lattner699b6612008-01-25 18:59:06 +00002354 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002355
John McCallb3d87482010-08-24 05:47:05 +00002356 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2357 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002358}
Sebastian Redl1d922962008-12-13 15:32:12 +00002359
Douglas Gregor2725ca82010-04-21 19:57:20 +00002360/// \brief Parse the remainder of an Objective-C message following the
2361/// '[' objc-receiver.
2362///
2363/// This routine handles sends to super, class messages (sent to a
2364/// class name), and instance messages (sent to an object), and the
2365/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2366/// ReceiverExpr, respectively. Only one of these parameters may have
2367/// a valid value.
2368///
2369/// \param LBracLoc The location of the opening '['.
2370///
2371/// \param SuperLoc If this is a send to 'super', the location of the
2372/// 'super' keyword that indicates a send to the superclass.
2373///
2374/// \param ReceiverType If this is a class message, the type of the
2375/// class we are sending a message to.
2376///
2377/// \param ReceiverExpr If this is an instance message, the expression
2378/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002379///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002380/// objc-message-args:
2381/// objc-selector
2382/// objc-keywordarg-list
2383///
2384/// objc-keywordarg-list:
2385/// objc-keywordarg
2386/// objc-keywordarg-list objc-keywordarg
2387///
Mike Stump1eb44332009-09-09 15:08:12 +00002388/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002389/// selector-name[opt] ':' objc-keywordexpr
2390///
2391/// objc-keywordexpr:
2392/// nonempty-expr-list
2393///
2394/// nonempty-expr-list:
2395/// assignment-expression
2396/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002397///
John McCall60d7b3a2010-08-24 06:29:42 +00002398ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002399Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002400 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002401 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002402 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002403 InMessageExpressionRAIIObject InMessage(*this, true);
2404
Steve Naroffc4df6d22009-11-07 02:08:14 +00002405 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002406 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002407 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2408 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002409 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002410 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2411 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002412 else
John McCall9ae2f072010-08-23 23:25:46 +00002413 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002414 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002415 cutOffParsing();
2416 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002417 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002418
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002419 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002420 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002421 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002422
Chris Lattner5f9e2722011-07-23 10:55:15 +00002423 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002424 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002425 ExprVector KeyExprs;
Steve Naroff68d331a2007-09-27 14:38:14 +00002426
Chris Lattnerdf195262007-10-09 17:51:17 +00002427 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002428 while (1) {
2429 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002430 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002431 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002432
Chris Lattnerdf195262007-10-09 17:51:17 +00002433 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002434 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002435 // We must manually skip to a ']', otherwise the expression skipper will
2436 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2437 // the enclosing expression.
2438 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002439 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002440 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002441
Steve Naroff68d331a2007-09-27 14:38:14 +00002442 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002443 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002444
2445 if (Tok.is(tok::code_completion)) {
2446 if (SuperLoc.isValid())
2447 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2448 KeyIdents.data(),
2449 KeyIdents.size(),
2450 /*AtArgumentEpression=*/true);
2451 else if (ReceiverType)
2452 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2453 KeyIdents.data(),
2454 KeyIdents.size(),
2455 /*AtArgumentEpression=*/true);
2456 else
2457 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2458 KeyIdents.data(),
2459 KeyIdents.size(),
2460 /*AtArgumentEpression=*/true);
2461
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002462 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002463 return ExprError();
2464 }
2465
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002466 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002467 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002468 // We must manually skip to a ']', otherwise the expression skipper will
2469 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2470 // the enclosing expression.
2471 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002472 return Res;
Steve Naroff37387c92007-09-17 20:25:27 +00002473 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002474
Steve Naroff37387c92007-09-17 20:25:27 +00002475 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002476 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002477
Douglas Gregord3c68542009-11-19 01:08:35 +00002478 // Code completion after each argument.
2479 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002480 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002481 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002482 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002483 KeyIdents.size(),
2484 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002485 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002486 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002487 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002488 KeyIdents.size(),
2489 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002490 else
John McCall9ae2f072010-08-23 23:25:46 +00002491 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002492 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002493 KeyIdents.size(),
2494 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002495 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002496 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002497 }
2498
Steve Naroff37387c92007-09-17 20:25:27 +00002499 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002500 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002501 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002502 break;
2503 // We have a selector or a colon, continue parsing.
2504 }
2505 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002506 while (Tok.is(tok::comma)) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002507 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002508 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002509 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002510 if (Res.isInvalid()) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002511 if (Tok.is(tok::colon)) {
2512 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2513 FixItHint::CreateRemoval(commaLoc);
2514 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002515 // We must manually skip to a ']', otherwise the expression skipper will
2516 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2517 // the enclosing expression.
2518 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002519 return Res;
Steve Naroff49f109c2007-11-15 13:05:42 +00002520 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002521
Steve Naroff49f109c2007-11-15 13:05:42 +00002522 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002523 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002524 }
2525 } else if (!selIdent) {
2526 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002527
Chris Lattner4fef81d2008-08-05 06:19:09 +00002528 // We must manually skip to a ']', otherwise the expression skipper will
2529 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2530 // the enclosing expression.
2531 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002532 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002533 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002534
Chris Lattnerdf195262007-10-09 17:51:17 +00002535 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002536 if (Tok.is(tok::identifier))
2537 Diag(Tok, diag::err_expected_colon);
2538 else
2539 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002540 // We must manually skip to a ']', otherwise the expression skipper will
2541 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2542 // the enclosing expression.
2543 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002544 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002545 }
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002546
Chris Lattner699b6612008-01-25 18:59:06 +00002547 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002548
Steve Naroff29238a02007-10-05 18:42:47 +00002549 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002550 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002551 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002552 KeyLocs.push_back(Loc);
2553 }
Chris Lattnerff384912007-10-07 02:00:24 +00002554 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002555
Douglas Gregor2725ca82010-04-21 19:57:20 +00002556 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002557 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002558 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002559 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002560 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002561 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCall9ae2f072010-08-23 23:25:46 +00002562 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002563 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002564}
2565
John McCall60d7b3a2010-08-24 06:29:42 +00002566ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2567 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002568 if (Res.isInvalid()) return Res;
Sebastian Redl1d922962008-12-13 15:32:12 +00002569
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002570 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2571 // expressions. At this point, we know that the only valid thing that starts
2572 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002573 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002574 ExprVector AtStrings;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002575 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002576 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002577
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002578 while (Tok.is(tok::at)) {
2579 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002580
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002581 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002582 if (!isTokenStringLiteral())
2583 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002584
John McCall60d7b3a2010-08-24 06:29:42 +00002585 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002586 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002587 return Lit;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002588
Sebastian Redleffa8d12008-12-10 00:02:53 +00002589 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002590 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002591
Nico Weberd32b94d2012-12-31 00:28:03 +00002592 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2593 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00002594}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002595
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002596/// ParseObjCBooleanLiteral -
2597/// objc-scalar-literal : '@' boolean-keyword
2598/// ;
2599/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2600/// ;
2601ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2602 bool ArgValue) {
2603 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2604 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2605}
2606
2607/// ParseObjCCharacterLiteral -
2608/// objc-scalar-literal : '@' character-literal
2609/// ;
2610ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2611 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2612 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002613 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002614 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002615 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002616 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002617}
2618
2619/// ParseObjCNumericLiteral -
2620/// objc-scalar-literal : '@' scalar-literal
2621/// ;
2622/// scalar-literal : | numeric-constant /* any numeric constant. */
2623/// ;
2624ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2625 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2626 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002627 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002628 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002629 ConsumeToken(); // Consume the literal token.
Nico Weberd32b94d2012-12-31 00:28:03 +00002630 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002631}
2632
Patrick Beardeb382ec2012-04-19 00:25:12 +00002633/// ParseObjCBoxedExpr -
2634/// objc-box-expression:
2635/// @( assignment-expression )
2636ExprResult
2637Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2638 if (Tok.isNot(tok::l_paren))
2639 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2640
2641 BalancedDelimiterTracker T(*this, tok::l_paren);
2642 T.consumeOpen();
2643 ExprResult ValueExpr(ParseAssignmentExpression());
2644 if (T.consumeClose())
2645 return ExprError();
Argyrios Kyrtzidisedd27602012-05-10 20:02:36 +00002646
2647 if (ValueExpr.isInvalid())
2648 return ExprError();
2649
Patrick Beardeb382ec2012-04-19 00:25:12 +00002650 // Wrap the sub-expression in a parenthesized expression, to distinguish
2651 // a boxed expression from a literal.
2652 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2653 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Weberd32b94d2012-12-31 00:28:03 +00002654 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2655 ValueExpr.take());
Patrick Beardeb382ec2012-04-19 00:25:12 +00002656}
2657
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002658ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002659 ExprVector ElementExprs; // array elements.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002660 ConsumeBracket(); // consume the l_square.
2661
2662 while (Tok.isNot(tok::r_square)) {
2663 // Parse list of array element expressions (all must be id types).
2664 ExprResult Res(ParseAssignmentExpression());
2665 if (Res.isInvalid()) {
2666 // We must manually skip to a ']', otherwise the expression skipper will
2667 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2668 // the enclosing expression.
2669 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002670 return Res;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002671 }
2672
2673 // Parse the ellipsis that indicates a pack expansion.
2674 if (Tok.is(tok::ellipsis))
2675 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2676 if (Res.isInvalid())
2677 return true;
2678
2679 ElementExprs.push_back(Res.release());
2680
2681 if (Tok.is(tok::comma))
2682 ConsumeToken(); // Eat the ','.
2683 else if (Tok.isNot(tok::r_square))
2684 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2685 }
2686 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002687 MultiExprArg Args(ElementExprs);
Nico Weberd32b94d2012-12-31 00:28:03 +00002688 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002689}
2690
2691ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2692 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2693 ConsumeBrace(); // consume the l_square.
2694 while (Tok.isNot(tok::r_brace)) {
2695 // Parse the comma separated key : value expressions.
2696 ExprResult KeyExpr;
2697 {
2698 ColonProtectionRAIIObject X(*this);
2699 KeyExpr = ParseAssignmentExpression();
2700 if (KeyExpr.isInvalid()) {
2701 // We must manually skip to a '}', otherwise the expression skipper will
2702 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2703 // the enclosing expression.
2704 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002705 return KeyExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002706 }
2707 }
2708
2709 if (Tok.is(tok::colon)) {
2710 ConsumeToken();
2711 } else {
2712 return ExprError(Diag(Tok, diag::err_expected_colon));
2713 }
2714
2715 ExprResult ValueExpr(ParseAssignmentExpression());
2716 if (ValueExpr.isInvalid()) {
2717 // We must manually skip to a '}', otherwise the expression skipper will
2718 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2719 // the enclosing expression.
2720 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002721 return ValueExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002722 }
2723
2724 // Parse the ellipsis that designates this as a pack expansion.
2725 SourceLocation EllipsisLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00002726 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002727 EllipsisLoc = ConsumeToken();
2728
2729 // We have a valid expression. Collect it in a vector so we can
2730 // build the argument list.
2731 ObjCDictionaryElement Element = {
2732 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, llvm::Optional<unsigned>()
2733 };
2734 Elements.push_back(Element);
2735
2736 if (Tok.is(tok::comma))
2737 ConsumeToken(); // Eat the ','.
2738 else if (Tok.isNot(tok::r_brace))
2739 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2740 }
2741 SourceLocation EndLoc = ConsumeBrace();
2742
2743 // Create the ObjCDictionaryLiteral.
Nico Weberd32b94d2012-12-31 00:28:03 +00002744 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2745 Elements.data(), Elements.size());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002746}
2747
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002748/// objc-encode-expression:
Dmitri Gribenkoe23fb902012-09-12 17:01:48 +00002749/// \@encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002750ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002751Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002752 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002753
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002754 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002755
Chris Lattner4fef81d2008-08-05 06:19:09 +00002756 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002757 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2758
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002759 BalancedDelimiterTracker T(*this, tok::l_paren);
2760 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002761
Douglas Gregor809070a2009-02-18 17:45:20 +00002762 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002763
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002764 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002765
Douglas Gregor809070a2009-02-18 17:45:20 +00002766 if (Ty.isInvalid())
2767 return ExprError();
2768
Nico Weberd32b94d2012-12-31 00:28:03 +00002769 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2770 Ty.get(), T.getCloseLocation());
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002771}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002772
2773/// objc-protocol-expression
James Dennett17d26a62012-06-11 06:19:40 +00002774/// \@protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002775ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002776Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002777 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002778
Chris Lattner4fef81d2008-08-05 06:19:09 +00002779 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002780 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2781
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002782 BalancedDelimiterTracker T(*this, tok::l_paren);
2783 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002784
Chris Lattner4fef81d2008-08-05 06:19:09 +00002785 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002786 return ExprError(Diag(Tok, diag::err_expected_ident));
2787
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002788 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002789 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002790
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002791 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002792
Nico Weberd32b94d2012-12-31 00:28:03 +00002793 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2794 T.getOpenLocation(), ProtoIdLoc,
2795 T.getCloseLocation());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002796}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002797
2798/// objc-selector-expression
2799/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002800ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002801 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002802
Chris Lattner4fef81d2008-08-05 06:19:09 +00002803 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002804 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2805
Chris Lattner5f9e2722011-07-23 10:55:15 +00002806 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002807 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002808
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002809 BalancedDelimiterTracker T(*this, tok::l_paren);
2810 T.consumeOpen();
2811
Douglas Gregor458433d2010-08-26 15:07:07 +00002812 if (Tok.is(tok::code_completion)) {
2813 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2814 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002815 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002816 return ExprError();
2817 }
2818
Chris Lattner2fc5c242009-04-11 18:13:45 +00002819 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002820 if (!SelIdent && // missing selector name.
2821 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002822 return ExprError(Diag(Tok, diag::err_expected_ident));
2823
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002824 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002825 unsigned nColons = 0;
2826 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002827 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002828 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2829 ++nColons;
2830 KeyIdents.push_back(0);
2831 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002832 return ExprError(Diag(Tok, diag::err_expected_colon));
2833
Chris Lattner5add7542010-08-27 22:32:41 +00002834 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002835 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002836 if (Tok.is(tok::r_paren))
2837 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002838
2839 if (Tok.is(tok::code_completion)) {
2840 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2841 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002842 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002843 return ExprError();
2844 }
2845
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002846 // Check for another keyword selector.
2847 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002848 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002849 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002850 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002851 break;
2852 }
Steve Naroff887407e2007-12-05 22:21:29 +00002853 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002854 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002855 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Weberd32b94d2012-12-31 00:28:03 +00002856 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2857 T.getOpenLocation(),
Fariborz Jahanian48f3cc22013-01-22 18:35:43 +00002858 T.getCloseLocation());
Gabor Greif58065b22007-10-19 15:38:32 +00002859 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002860
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002861void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2862 // MCDecl might be null due to error in method or c-function prototype, etc.
2863 Decl *MCDecl = LM.D;
2864 bool skip = MCDecl &&
2865 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2866 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2867 if (skip)
2868 return;
2869
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002870 // Save the current token position.
2871 SourceLocation OrigLoc = Tok.getLocation();
2872
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002873 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2874 // Append the current token at the end of the new token stream so that it
2875 // doesn't get lost.
2876 LM.Toks.push_back(Tok);
2877 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2878
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002879 // Consume the previously pushed token.
2880 ConsumeAnyToken();
2881
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002882 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2883 Tok.is(tok::colon)) &&
2884 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002885 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002886 ParseScope BodyScope(this,
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002887 parseMethod
2888 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2889 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002890
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002891 // Tell the actions module that we have entered a method or c-function definition
2892 // with the specified Declarator for the method/function.
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +00002893 if (parseMethod)
2894 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2895 else
2896 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002897 if (Tok.is(tok::kw_try))
2898 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002899 else {
2900 if (Tok.is(tok::colon))
2901 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002902 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002903 }
Fariborz Jahanian69409722012-08-09 21:12:39 +00002904
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002905 if (Tok.getLocation() != OrigLoc) {
2906 // Due to parsing error, we either went over the cached tokens or
2907 // there are still cached tokens left. If it's the latter case skip the
2908 // leftover tokens.
2909 // Since this is an uncommon situation that should be avoided, use the
2910 // expensive isBeforeInTranslationUnit call.
2911 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2912 OrigLoc))
2913 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2914 ConsumeAnyToken();
2915 }
2916
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002917 return;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002918}