blob: 48c5efac6bab963a8edf1f1d9f129e00ee5292e6 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000020#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000022using namespace clang;
23
24
Chris Lattner3a907162008-12-08 21:53:24 +000025/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000026/// external-declaration: [C99 6.9]
27/// [OBJC] objc-class-definition
Steve Naroffe0933392007-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 Jahanian3a039e32011-08-27 20:50:59 +000033Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000034 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000035
Douglas Gregorf48706c2009-12-07 09:27:33 +000036 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000037 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000038 cutOffParsing();
39 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000040 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000041
42 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000043 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000044 case tok::objc_class:
45 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall53fa7142010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000053 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000054 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000055 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000056 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000057 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000058 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000059 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000060 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
61 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000062 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000063 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
64 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000065 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
67 break;
Ted Kremenekc1e4dd02012-03-01 22:07:04 +000068 case tok::objc___experimental_modules_import:
David Blaikiebbafb8a2012-03-11 07:00:24 +000069 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000070 return ParseModuleImport(AtLoc);
71
72 // Fall through
73
Chris Lattnerce90ef52008-08-23 02:02:23 +000074 default:
75 Diag(AtLoc, diag::err_unexpected_at);
76 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 SingleDecl = 0;
78 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000079 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000080 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000081}
82
83///
Mike Stump11289f42009-09-09 15:08:12 +000084/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000085/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000086///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000087Parser::DeclGroupPtrTy
88Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000089 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000090 SmallVector<IdentifierInfo *, 8> ClassNames;
91 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +000092
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000095 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000096 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000097 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000098 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000099 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000100 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000101 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000102 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner0ef13522007-10-09 17:51:17 +0000104 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000105 break;
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000107 ConsumeToken();
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000110 // Consume the ';'.
111 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000112 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000113
Ted Kremeneka26da852009-11-17 23:12:20 +0000114 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
115 ClassLocs.data(),
116 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000117}
118
Erik Verbruggenc6c8d932011-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 Kyrtzidisb6c6a582012-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 Verbruggenc6c8d932011-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 Verbruggenc6c8d932011-12-06 09:25:23 +0000136}
137
Steve Naroff1eb1ad62007-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 Stump11289f42009-09-09 15:08:12 +0000144/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000145/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000146/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147/// objc-interface-decl-list
148/// @end
149///
150/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000151/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-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 Beardacfbe9e2012-04-06 18:12:22 +0000165/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000166///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000167Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000168 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000169 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000170 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000171 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000172 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000173
Douglas Gregor49c22a72009-11-18 16:26:39 +0000174 // Code completion after '@interface'.
175 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000176 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000177 cutOffParsing();
178 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000179 }
180
Chris Lattner0ef13522007-10-09 17:51:17 +0000181 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000183 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000184 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000185
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000186 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000187 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000188 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000189 if (Tok.is(tok::l_paren) &&
190 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000191
192 BalancedDelimiterTracker T(*this, tok::l_paren);
193 T.consumeOpen();
194
195 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000196 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000197 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000198 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000199 cutOffParsing();
200 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000201 }
202
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000203 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000204 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000205 categoryId = Tok.getIdentifierInfo();
206 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000207 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000208 else if (!getLangOpts().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000209 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000210 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000211 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000212
213 T.consumeClose();
214 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000215 return 0;
Douglas Gregor0c254a02011-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 Naroff1eb1ad62007-08-20 21:31:48 +0000220 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000221
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000222 // Next, we need to check for any protocol references.
223 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000224 SmallVector<Decl *, 8> ProtocolRefs;
225 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000226 if (Tok.is(tok::less) &&
227 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000228 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000229 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000230
John McCall48871652010-08-21 09:40:31 +0000231 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000232 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000233 nameId, nameLoc,
234 categoryId, categoryLoc,
235 ProtocolRefs.data(),
236 ProtocolRefs.size(),
237 ProtocolLocs.data(),
238 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000239
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000240 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000241 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000242
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000243 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000244 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000245 }
246 // Parse a class interface.
247 IdentifierInfo *superClassId = 0;
248 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000249
Chris Lattner0ef13522007-10-09 17:51:17 +0000250 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000251 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000252
253 // Code completion of superclass names.
254 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000255 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000256 cutOffParsing();
257 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000258 }
259
Chris Lattner0ef13522007-10-09 17:51:17 +0000260 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000261 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000262 return 0;
Steve Naroff1eb1ad62007-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 Lattner0e62c1c2011-07-23 10:55:15 +0000268 SmallVector<Decl *, 8> ProtocolRefs;
269 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000270 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000271 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000272 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
273 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000274 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000275
John McCall48871652010-08-21 09:40:31 +0000276 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000277 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000278 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000279 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000280 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000281 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner0ef13522007-10-09 17:51:17 +0000283 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000284 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000285
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000286 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000287 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000288}
289
John McCall064d77b2009-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 Blaikie68e081d2011-12-20 02:48:34 +0000293private:
294 virtual void anchor();
295public:
John McCall064d77b2009-12-03 22:31:13 +0000296 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000297 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000298 ObjCDeclSpec &OCDS;
299 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000300 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000301 tok::ObjCKeywordKind MethodImplKind;
302
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000303 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000304 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000305 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000306 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000307 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000308 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000309 MethodImplKind(MethodImplKind) {
310 }
311
Eli Friedman934dbbf2012-08-08 23:53:27 +0000312 void invoke(ParsingFieldDeclarator &FD) {
John McCall064d77b2009-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 Friedman934dbbf2012-08-08 23:53:27 +0000316 return;
John McCall064d77b2009-12-03 22:31:13 +0000317 }
318 if (FD.BitfieldSize) {
319 P.Diag(AtLoc, diag::err_objc_property_bitfield)
320 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000321 return;
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000339 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000340 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
341 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000342 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000343 &isOverridingProperty,
344 MethodImplKind);
345 if (!isOverridingProperty)
346 Props.push_back(Property);
347
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000348 FD.complete(Property);
John McCall064d77b2009-12-03 22:31:13 +0000349 }
350};
351
David Blaikie68e081d2011-12-20 02:48:34 +0000352void Parser::ObjCPropertyCallback::anchor() {
353}
354
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000355/// objc-interface-decl-list:
356/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000357/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000358/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000359/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000360/// objc-interface-decl-list declaration
361/// objc-interface-decl-list ';'
362///
Steve Naroff99264b42007-08-22 16:35:03 +0000363/// objc-method-requirement: [OBJC2]
364/// @required
365/// @optional
366///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000367void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
368 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000369 SmallVector<Decl *, 32> allMethods;
370 SmallVector<Decl *, 16> allProperties;
371 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000372 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenekc7c64312010-01-07 01:20:12 +0000374 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000375
Steve Naroff99264b42007-08-22 16:35:03 +0000376 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000377 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000378 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000379 if (Decl *methodPrototype =
380 ParseObjCMethodPrototype(MethodImplKind, false))
381 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000382 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
383 // method definitions.
Argyrios Kyrtzidise1ee6232011-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 Naroff99264b42007-08-22 16:35:03 +0000390 continue;
391 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000392 if (Tok.is(tok::l_paren)) {
393 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000394 ParseObjCMethodDecl(Tok.getLocation(),
395 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000396 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000397 continue;
398 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000399 // Ignore excess semicolons.
400 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000401 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000402 continue;
403 }
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattnerda9fb152008-10-20 06:10:06 +0000405 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000406 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000407 break;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Douglas Gregorf1934162010-01-13 21:24:21 +0000409 // Code completion within an Objective-C interface.
410 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000411 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000412 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000413 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000414 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000415 }
416
Chris Lattner038a3e32008-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 Lattnerc7c9ab72009-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;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000424 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000425 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000426 continue;
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner038a3e32008-10-20 05:46:22 +0000429 // Otherwise, we have an @ directive, eat the @.
430 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000431 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000432 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000433 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000434 }
435
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000436 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000438 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000439 AtEnd.setBegin(AtLoc);
440 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000441 break;
Douglas Gregor00a0cf72010-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 Lattnerda9fb152008-10-20 06:10:06 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Chris Lattnerda9fb152008-10-20 06:10:06 +0000448 // Eat the identifier.
449 ConsumeToken();
450
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000451 switch (DirectiveKind) {
452 default:
Chris Lattnerda9fb152008-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 Lattner76619232008-10-20 07:22:18 +0000457 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000458 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 SkipUntil(tok::r_brace, tok::at);
460 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000461
462 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000463 case tok::objc_interface:
Erik Verbruggenc6c8d932011-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 Jahaniand4c53482010-11-02 00:44:43 +0000468 ConsumeToken();
469 break;
470
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000471 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000472 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000473 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000474 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000475 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000476 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000477 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000478 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000479 break;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000481 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000482 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000483 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000484
Chris Lattner038a3e32008-10-20 05:46:22 +0000485 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000486 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000487 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000488 if (Tok.is(tok::l_paren)) {
489 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000490 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000493 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000494 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000495
Chris Lattner038a3e32008-10-20 05:46:22 +0000496 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000497 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +0000498 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000499
John McCall405988b2011-03-26 01:53:26 +0000500 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000501 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000502 }
Steve Naroff99264b42007-08-22 16:35:03 +0000503 }
Chris Lattnerda9fb152008-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 Gregorf48706c2009-12-07 09:27:33 +0000507 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000508 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000509 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000510 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000511 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-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 Stump11289f42009-09-09 15:08:12 +0000520
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000521 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000522 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000523 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000524 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000525 allProperties.data(), allProperties.size(),
526 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000527}
528
Fariborz Jahanian9fca6df2007-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 McCall31168b02011-06-15 23:02:42 +0000544/// atomic
545/// strong
546/// weak
547/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000548///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000549void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000550 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000551 BalancedDelimiterTracker T(*this, tok::l_paren);
552 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000554 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000555 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000556 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000557 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000558 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000559 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner76619232008-10-20 07:22:18 +0000561 // If this is not an identifier at all, bail out early.
562 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000563 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000564 return;
565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattner1db33542008-10-20 07:37:22 +0000567 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner68e48682008-11-20 04:42:34 +0000569 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000570 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000571 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000572 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000573 else if (II->isStr("unsafe_unretained"))
574 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000575 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000577 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000578 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000579 else if (II->isStr("strong"))
580 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000581 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000582 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000583 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000584 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000585 else if (II->isStr("atomic"))
586 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000587 else if (II->isStr("weak"))
588 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000589 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000590 bool IsSetter = II->getNameStart()[0] == 's';
591
Chris Lattner825bca12008-10-20 07:39:53 +0000592 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-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 Lattner43c76c32008-10-20 07:00:43 +0000597 return;
Mike Stump11289f42009-09-09 15:08:12 +0000598
Douglas Gregorc8537c52009-11-19 07:41:15 +0000599 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000600 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000601 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000602 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000603 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000604 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000605 }
606
Anders Carlssonfe15a782010-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 Lattnerbeca7702008-10-20 07:24:39 +0000614 SkipUntil(tok::r_paren);
615 return;
616 }
Mike Stump11289f42009-09-09 15:08:12 +0000617
Anders Carlssonfe15a782010-10-02 17:45:21 +0000618 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000619 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000620 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000622 if (ExpectAndConsume(tok::colon,
623 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000624 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000625 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000626 } else {
627 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000628 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000629 }
Chris Lattner825bca12008-10-20 07:39:53 +0000630 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000631 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000632 SkipUntil(tok::r_paren);
633 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Chris Lattner1db33542008-10-20 07:37:22 +0000636 if (Tok.isNot(tok::comma))
637 break;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Chris Lattner1db33542008-10-20 07:37:22 +0000639 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000642 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000643}
644
Steve Naroff09bf8152007-09-06 21:24:23 +0000645/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000646/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000647/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000648///
649/// objc-instance-method: '-'
650/// objc-class-method: '+'
651///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000652/// objc-method-attributes: [OBJC2]
653/// __attribute__((deprecated))
654///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000655Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000656 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000657 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000658
Mike Stump11289f42009-09-09 15:08:12 +0000659 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000660 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000661 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000662 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000663 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000664 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000665 return MDecl;
Steve Naroff99264b42007-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 Lattner4f472a32009-04-11 18:13:45 +0000676IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000677
Chris Lattner5700fab2007-10-07 02:00:24 +0000678 switch (Tok.getKind()) {
679 default:
680 return 0;
Fariborz Jahanian9e42a952010-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 Jahaniandadfc1c2010-09-03 18:01:09 +0000692 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-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 Lattner5700fab2007-10-07 02:00:24 +0000702 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000703 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000704 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000705 case tok::kw_bool:
Anders Carlssonf93f56a2008-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 Lattner5700fab2007-10-07 02:00:24 +0000768 case tok::kw__Bool:
769 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000770 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000771 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000772 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000773 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000774 }
Steve Naroff99264b42007-08-22 16:35:03 +0000775}
776
Fariborz Jahanian83615522008-01-02 22:54:34 +0000777/// objc-for-collection-in: 'in'
778///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000779bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-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 Blaikiebbafb8a2012-03-11 07:00:24 +0000783 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000784 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000785}
786
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000787/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000788/// qualifier list and builds their bitmask representation in the input
789/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000790///
791/// objc-type-qualifiers:
792/// objc-type-qualifier
793/// objc-type-qualifiers objc-type-qualifier
794///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000795void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000796 Declarator::TheContext Context) {
797 assert(Context == Declarator::ObjCParameterContext ||
798 Context == Declarator::ObjCResultContext);
799
Chris Lattner61511e12007-12-12 06:56:32 +0000800 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000801 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000802 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000803 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000804 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000805 }
806
Chris Lattner5e530bc2007-12-27 19:57:00 +0000807 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000808 return;
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner61511e12007-12-12 06:56:32 +0000810 const IdentifierInfo *II = Tok.getIdentifierInfo();
811 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000812 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000813 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000815 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000816 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000817 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-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 Lattner61511e12007-12-12 06:56:32 +0000824 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000825 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000826 ConsumeToken();
827 II = 0;
828 break;
829 }
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner61511e12007-12-12 06:56:32 +0000831 // If this wasn't a recognized qualifier, bail out.
832 if (II) return;
833 }
834}
835
John McCalla55902b2011-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 Lattner61511e12007-12-12 06:56:32 +0000870/// objc-type-name:
871/// '(' objc-type-qualifiers[opt] type-name ')'
872/// '(' objc-type-qualifiers[opt] ')'
873///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000874ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-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 Lattner0ef13522007-10-09 17:51:17 +0000881 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000882
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000883 BalancedDelimiterTracker T(*this, tok::l_paren);
884 T.consumeOpen();
885
Chris Lattner2ebb1782008-08-23 01:48:03 +0000886 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000887 ObjCDeclContextSwitch ObjCDC(*this);
888
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000889 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000890 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000891
John McCallba7bf592010-08-24 05:47:05 +0000892 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000893 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000894 // Parse an abstract declarator.
895 DeclSpec declSpec(AttrFactory);
896 declSpec.setObjCQualifiers(&DS);
897 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +0000898 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-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 Gregorbab8a962011-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 Gregor220cac52009-02-18 17:45:20 +0000922 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000923
Steve Naroff90255b42008-10-21 14:15:04 +0000924 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000925 T.consumeClose();
Chris Lattnerb7954432008-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 Lattner6d29c102008-11-18 07:48:38 +0000928 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-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 Gregore7a8e3b2011-10-12 16:37:45 +0000933 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000934 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000935 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000936}
937
938/// objc-method-decl:
939/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000940/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000941/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000942/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000943///
944/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000945/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000946/// objc-keyword-selector objc-keyword-decl
947///
948/// objc-keyword-decl:
Steve Naroff0b6a01a2007-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 Naroff99264b42007-08-22 16:35:03 +0000953///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000954/// objc-parmlist:
955/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000956///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000957/// objc-parms:
958/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000959///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000960/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000961/// , ...
962///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000963/// objc-keyword-attributes: [OBJC2]
964/// __attribute__((unused))
965///
John McCall48871652010-08-21 09:40:31 +0000966Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000967 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000968 tok::ObjCKeywordKind MethodImplKind,
969 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000970 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000971
Douglas Gregor636a61e2010-04-07 00:21:17 +0000972 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000973 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000974 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000975 cutOffParsing();
976 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000977 }
978
Chris Lattner2ebb1782008-08-23 01:48:03 +0000979 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000980 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000981 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000982 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000983 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000984
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000985 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000986 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000987 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000988 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000989
Douglas Gregor636a61e2010-04-07 00:21:17 +0000990 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000991 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000992 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000993 cutOffParsing();
994 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000995 }
996
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000997 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000998 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000999 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001000
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001001 // An unnamed colon is valid.
1002 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001003 Diag(Tok, diag::err_expected_selector_for_method)
1004 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001005 // Skip until we get a ; or @.
1006 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCall48871652010-08-21 09:40:31 +00001007 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001010 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001011 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001012 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001013 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001014 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner5700fab2007-10-07 02:00:24 +00001016 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001017 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001018 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001019 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001020 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001021 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001022 methodAttrs.getList(), MethodImplKind,
1023 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001024 PD.complete(Result);
1025 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001026 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001027
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001028 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001029 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001030 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001031 ParseScope PrototypeScope(this,
1032 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001033
1034 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001035 bool warnSelectorName = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001036 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001037 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001038 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001039
Chris Lattner5700fab2007-10-07 02:00:24 +00001040 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001041 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001042 Diag(Tok, diag::err_expected_colon);
1043 break;
1044 }
1045 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001046
John McCallba7bf592010-08-24 05:47:05 +00001047 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001048 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001049 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1050 Declarator::ObjCParameterContext,
1051 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001052
Chris Lattner5700fab2007-10-07 02:00:24 +00001053 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001054 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001055 ArgInfo.ArgAttrs = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001056 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001057 MaybeParseGNUAttributes(paramAttrs);
1058 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001059 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001060
Douglas Gregor45879692010-07-08 23:37:41 +00001061 // Code completion for the next piece of the selector.
1062 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001063 KeyIdents.push_back(SelIdent);
1064 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1065 mType == tok::minus,
1066 /*AtParameterName=*/true,
1067 ReturnType,
1068 KeyIdents.data(),
1069 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001070 cutOffParsing();
1071 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001072 }
1073
Chris Lattner0ef13522007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001075 Diag(Tok, diag::err_expected_ident); // missing argument name.
1076 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001077 }
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001079 ArgInfo.Name = Tok.getIdentifierInfo();
1080 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001081 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001083 ArgInfos.push_back(ArgInfo);
1084 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001085 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001086
John McCall084e83d2011-03-24 11:26:52 +00001087 // Make sure the attributes persist.
1088 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1089
Douglas Gregor95887f92010-07-08 23:20:03 +00001090 // Code completion for the next piece of the selector.
1091 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001092 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1093 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001094 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +00001095 ReturnType,
1096 KeyIdents.data(),
1097 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001098 cutOffParsing();
1099 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001100 }
1101
Chris Lattner5700fab2007-10-07 02:00:24 +00001102 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001103 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001104 if (!SelIdent && Tok.isNot(tok::colon))
1105 break;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001106 if (MethodDefinition && !SelIdent) {
1107 SourceLocation ColonLoc = Tok.getLocation();
1108 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
1109 warnSelectorName = true;
1110 Diag(ArgInfo.NameLoc, diag::missing_selector_name);
1111 }
1112 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001113 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001116 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001117 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001118 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001119 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001120 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001121 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001122 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001123 ConsumeToken();
1124 break;
1125 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001126 if (!cStyleParamWarned) {
1127 Diag(Tok, diag::warn_cstyle_param);
1128 cStyleParamWarned = true;
1129 }
John McCall084e83d2011-03-24 11:26:52 +00001130 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001131 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001132 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001133 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1134 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001135 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001136 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001137 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1138 ParmDecl.getIdentifierLoc(),
1139 Param,
1140 0));
Chris Lattner5700fab2007-10-07 02:00:24 +00001141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001143 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001144 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001145 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001146 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001147
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001148 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001149 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001150
Chris Lattner5700fab2007-10-07 02:00:24 +00001151 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1152 &KeyIdents[0]);
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001153 if (warnSelectorName) {
1154 SmallVector<IdentifierInfo *, 12> DiagKeyIdents;
1155 for (unsigned i = 0, size = KeyIdents.size(); i < size; i++)
1156 if (KeyIdents[i])
1157 DiagKeyIdents.push_back(KeyIdents[i]);
1158 else {
1159 std::string name = "Name";
1160 name += llvm::utostr(i+1);
1161 IdentifierInfo *NamedMissingId = &PP.getIdentifierTable().get(name);
1162 DiagKeyIdents.push_back(NamedMissingId);
1163 }
1164 Selector NewSel =
1165 PP.getSelectorTable().getSelector(DiagKeyIdents.size(), &DiagKeyIdents[0]);
1166 Diag(mLoc, diag::note_missing_argument_name)
1167 << NewSel.getAsString() << Sel.getAsString();
1168 }
John McCall48871652010-08-21 09:40:31 +00001169 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001170 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001171 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001172 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001173 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001174 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001175 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001176
John McCall28a6aea2009-11-04 02:18:39 +00001177 PD.complete(Result);
1178 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001179}
1180
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001181/// objc-protocol-refs:
1182/// '<' identifier-list '>'
1183///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001184bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001185ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1186 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001187 bool WarnOnDeclarations,
1188 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001189 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001190
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001191 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001192
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001193 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattner3bbae002008-07-26 04:03:38 +00001195 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001196 if (Tok.is(tok::code_completion)) {
1197 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1198 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001199 cutOffParsing();
1200 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001201 }
1202
Chris Lattner3bbae002008-07-26 04:03:38 +00001203 if (Tok.isNot(tok::identifier)) {
1204 Diag(Tok, diag::err_expected_ident);
1205 SkipUntil(tok::greater);
1206 return true;
1207 }
1208 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1209 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001210 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001211 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner3bbae002008-07-26 04:03:38 +00001213 if (Tok.isNot(tok::comma))
1214 break;
1215 ConsumeToken();
1216 }
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattner3bbae002008-07-26 04:03:38 +00001218 // Consume the '>'.
1219 if (Tok.isNot(tok::greater)) {
1220 Diag(Tok, diag::err_expected_greater);
1221 return true;
1222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001224 EndLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001225
Chris Lattner3bbae002008-07-26 04:03:38 +00001226 // Convert the list of protocols identifiers into a list of protocol decls.
1227 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1228 &ProtocolIdents[0], ProtocolIdents.size(),
1229 Protocols);
1230 return false;
1231}
1232
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001233/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1234/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001235bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001236 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001237 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001238 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001239 SmallVector<Decl *, 8> ProtocolDecl;
1240 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001241 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1242 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001243 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1244 ProtocolLocs.data(), LAngleLoc);
1245 if (EndProtoLoc.isValid())
1246 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001247 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001248}
1249
1250
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001251/// objc-class-instance-variables:
1252/// '{' objc-instance-variable-decl-list[opt] '}'
1253///
1254/// objc-instance-variable-decl-list:
1255/// objc-visibility-spec
1256/// objc-instance-variable-decl ';'
1257/// ';'
1258/// objc-instance-variable-decl-list objc-visibility-spec
1259/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1260/// objc-instance-variable-decl-list ';'
1261///
1262/// objc-visibility-spec:
1263/// @private
1264/// @protected
1265/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001266/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001267///
1268/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001269/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001270///
John McCall48871652010-08-21 09:40:31 +00001271void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001272 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001273 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001274 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001275 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001276
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001277 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001278 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001279
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001280 BalancedDelimiterTracker T(*this, tok::l_brace);
1281 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001282
Steve Naroff00433d32007-08-21 21:17:12 +00001283 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001284 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001285 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001286
Steve Naroff00433d32007-08-21 21:17:12 +00001287 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001288 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001289 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001290 continue;
1291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Steve Naroff00433d32007-08-21 21:17:12 +00001293 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001294 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001295 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001296
1297 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001298 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001299 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001300 }
1301
Steve Naroff7c348172007-08-23 18:16:40 +00001302 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001303 case tok::objc_private:
1304 case tok::objc_public:
1305 case tok::objc_protected:
1306 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001307 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001308 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001309 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001310 default:
1311 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001312 continue;
1313 }
1314 }
Mike Stump11289f42009-09-09 15:08:12 +00001315
Douglas Gregor48d46252010-01-13 21:54:15 +00001316 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001317 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001318 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001319 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001320 }
1321
John McCallcfefb6d2009-11-03 02:38:08 +00001322 struct ObjCIvarCallback : FieldCallback {
1323 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001324 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001325 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001326 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001327
John McCall48871652010-08-21 09:40:31 +00001328 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001329 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001330 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1331 }
1332
Eli Friedman934dbbf2012-08-08 23:53:27 +00001333 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001334 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001335 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001336 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001337 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001338 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001339 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001340 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001341 if (Field)
1342 AllIvarDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001343 FD.complete(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001344 }
1345 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001346
Chris Lattnera12405b2008-04-10 06:46:29 +00001347 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001348 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001349 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001350
Chris Lattner0ef13522007-10-09 17:51:17 +00001351 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001352 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001353 } else {
1354 Diag(Tok, diag::err_expected_semi_decl_list);
1355 // Skip to end of block or statement
1356 SkipUntil(tok::r_brace, true, true);
1357 }
1358 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001359 T.consumeClose();
1360
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001361 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001362 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001363 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001364 // Call ActOnFields() even if we don't have any decls. This is useful
1365 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001366 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie751c5582011-09-22 02:58:26 +00001367 AllIvarDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001368 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001369 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001370}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001371
1372/// objc-protocol-declaration:
1373/// objc-protocol-definition
1374/// objc-protocol-forward-reference
1375///
1376/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001377/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001378/// objc-protocol-refs[opt]
1379/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001380/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001381///
1382/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001383/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001384///
James Dennett1355bd12012-06-11 06:19:40 +00001385/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001386/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001387/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001388Parser::DeclGroupPtrTy
1389Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1390 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001391 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001392 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1393 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001394
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001395 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001396 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001397 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001398 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001399 }
1400
Chris Lattner0ef13522007-10-09 17:51:17 +00001401 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001402 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001403 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001404 }
1405 // Save the protocol name, then consume it.
1406 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1407 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001408
Chris Lattner0ef13522007-10-09 17:51:17 +00001409 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001410 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001411 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001412 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001413 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001414 }
Mike Stump11289f42009-09-09 15:08:12 +00001415
Erik Verbruggenf9887852011-12-08 09:58:43 +00001416 CheckNestedObjCContexts(AtLoc);
1417
Chris Lattner0ef13522007-10-09 17:51:17 +00001418 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001419 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001420 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1421
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001422 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001423 while (1) {
1424 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001425 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001426 Diag(Tok, diag::err_expected_ident);
1427 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001428 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001429 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001430 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1431 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001432 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001433
Chris Lattner0ef13522007-10-09 17:51:17 +00001434 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001435 break;
1436 }
1437 // Consume the ';'.
1438 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001439 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001440
Steve Naroff93eb5f12007-10-10 17:32:04 +00001441 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001442 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001443 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001444 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001445 }
Mike Stump11289f42009-09-09 15:08:12 +00001446
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001447 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001448 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001449
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001450 SmallVector<Decl *, 8> ProtocolRefs;
1451 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001452 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001453 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1454 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001455 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001456
John McCall48871652010-08-21 09:40:31 +00001457 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001458 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001459 ProtocolRefs.data(),
1460 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001461 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001462 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001463
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001464 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001465 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001466}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001467
1468/// objc-implementation:
1469/// objc-class-implementation-prologue
1470/// objc-category-implementation-prologue
1471///
1472/// objc-class-implementation-prologue:
1473/// @implementation identifier objc-superclass[opt]
1474/// objc-class-instance-variables[opt]
1475///
1476/// objc-category-implementation-prologue:
1477/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001478Parser::DeclGroupPtrTy
1479Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001480 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1481 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001482 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001483 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001484
Douglas Gregor49c22a72009-11-18 16:26:39 +00001485 // Code completion after '@implementation'.
1486 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001487 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001488 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001489 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001490 }
1491
Chris Lattner0ef13522007-10-09 17:51:17 +00001492 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001493 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001494 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001495 }
1496 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001497 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001498 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001499 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001500
1501 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001502 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001503 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001504 SourceLocation categoryLoc, rparenLoc;
1505 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001506
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001507 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001508 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001509 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001510 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001511 }
1512
Chris Lattner0ef13522007-10-09 17:51:17 +00001513 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001514 categoryId = Tok.getIdentifierInfo();
1515 categoryLoc = ConsumeToken();
1516 } else {
1517 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001518 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001519 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001520 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001521 Diag(Tok, diag::err_expected_rparen);
1522 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001523 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001524 }
1525 rparenLoc = ConsumeParen();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001526 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001527 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001528 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001529
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001530 } else {
1531 // We have a class implementation
1532 SourceLocation superClassLoc;
1533 IdentifierInfo *superClassId = 0;
1534 if (Tok.is(tok::colon)) {
1535 // We have a super class
1536 ConsumeToken();
1537 if (Tok.isNot(tok::identifier)) {
1538 Diag(Tok, diag::err_expected_ident); // missing super class name.
1539 return DeclGroupPtrTy();
1540 }
1541 superClassId = Tok.getIdentifierInfo();
1542 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001543 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001544 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1545 AtLoc, nameId, nameLoc,
1546 superClassId, superClassLoc);
1547
1548 if (Tok.is(tok::l_brace)) // we have ivars
1549 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001550 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001551 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001552
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001553 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001554
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001555 {
1556 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1557 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1558 ParsedAttributesWithRange attrs(AttrFactory);
1559 MaybeParseCXX0XAttributes(attrs);
1560 MaybeParseMicrosoftAttributes(attrs);
1561 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1562 DeclGroupRef DG = DGP.get();
1563 DeclsInGroup.append(DG.begin(), DG.end());
1564 }
1565 }
1566 }
1567
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001568 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001569}
Steve Naroff33a1e802007-10-29 21:38:07 +00001570
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001571Parser::DeclGroupPtrTy
1572Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001573 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1574 "ParseObjCAtEndDeclaration(): Expected @end");
1575 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001576 if (CurParsedObjCImpl)
1577 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001578 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001579 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001580 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001581 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001582}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001583
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001584Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1585 if (!Finished) {
1586 finish(P.Tok.getLocation());
1587 if (P.Tok.is(tok::eof)) {
1588 P.Diag(P.Tok, diag::err_objc_missing_end)
1589 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1590 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1591 << Sema::OCK_Implementation;
1592 }
1593 }
1594 P.CurParsedObjCImpl = 0;
1595 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001596}
1597
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001598void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1599 assert(!Finished);
1600 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1601 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001602 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1603 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001604
1605 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1606
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001607 if (HasCFunction)
1608 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1609 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1610 false/*c-functions*/);
1611
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001612 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001613 for (LateParsedObjCMethodContainer::iterator
1614 I = LateParsedObjCMethods.begin(),
1615 E = LateParsedObjCMethods.end(); I != E; ++I)
1616 delete *I;
1617 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001618
1619 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001620}
1621
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001622/// compatibility-alias-decl:
1623/// @compatibility_alias alias-name class-name ';'
1624///
John McCall48871652010-08-21 09:40:31 +00001625Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001626 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1627 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1628 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001629 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001630 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001631 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001632 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001633 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1634 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001635 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001636 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001637 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001638 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001639 IdentifierInfo *classId = Tok.getIdentifierInfo();
1640 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001641 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1642 "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001643 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1644 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001645}
1646
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001647/// property-synthesis:
1648/// @synthesize property-ivar-list ';'
1649///
1650/// property-ivar-list:
1651/// property-ivar
1652/// property-ivar-list ',' property-ivar
1653///
1654/// property-ivar:
1655/// identifier
1656/// identifier '=' identifier
1657///
John McCall48871652010-08-21 09:40:31 +00001658Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001659 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1660 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001661 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001662
Douglas Gregor88e72a02009-11-18 19:45:45 +00001663 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001664 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001665 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001666 cutOffParsing();
1667 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001668 }
1669
Douglas Gregor88e72a02009-11-18 19:45:45 +00001670 if (Tok.isNot(tok::identifier)) {
1671 Diag(Tok, diag::err_synthesized_property_name);
1672 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001673 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001674 }
1675
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001676 IdentifierInfo *propertyIvar = 0;
1677 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1678 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001679 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001680 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001681 // property '=' ivar-name
1682 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001683
1684 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001685 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001686 cutOffParsing();
1687 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001688 }
1689
Chris Lattner0ef13522007-10-09 17:51:17 +00001690 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001691 Diag(Tok, diag::err_expected_ident);
1692 break;
1693 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001694 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001695 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001696 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001697 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001698 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001699 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001700 break;
1701 ConsumeToken(); // consume ','
1702 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001703 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001704 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001705}
1706
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001707/// property-dynamic:
1708/// @dynamic property-list
1709///
1710/// property-list:
1711/// identifier
1712/// property-list ',' identifier
1713///
John McCall48871652010-08-21 09:40:31 +00001714Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001715 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1716 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001717 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001718 while (true) {
1719 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001720 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001721 cutOffParsing();
1722 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001723 }
1724
1725 if (Tok.isNot(tok::identifier)) {
1726 Diag(Tok, diag::err_expected_ident);
1727 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001728 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001729 }
1730
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001731 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1732 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001733 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001734 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001735
Chris Lattner0ef13522007-10-09 17:51:17 +00001736 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001737 break;
1738 ConsumeToken(); // consume ','
1739 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001740 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001741 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001742}
Mike Stump11289f42009-09-09 15:08:12 +00001743
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001744/// objc-throw-statement:
1745/// throw expression[opt];
1746///
John McCalldadc5752010-08-24 06:29:42 +00001747StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1748 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001749 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001750 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001751 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001752 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001753 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001754 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001755 }
1756 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001757 // consume ';'
1758 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001759 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001760}
1761
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001762/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001763/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001764///
John McCalldadc5752010-08-24 06:29:42 +00001765StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001766Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001767 ConsumeToken(); // consume synchronized
1768 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001769 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001770 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001771 }
John McCalld9bb7432011-07-27 21:50:02 +00001772
1773 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001774 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001775 ExprResult operand(ParseExpression());
1776
1777 if (Tok.is(tok::r_paren)) {
1778 ConsumeParen(); // ')'
1779 } else {
1780 if (!operand.isInvalid())
1781 Diag(Tok, diag::err_expected_rparen);
1782
1783 // Skip forward until we see a left brace, but don't consume it.
1784 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001785 }
John McCalld9bb7432011-07-27 21:50:02 +00001786
1787 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001788 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001789 if (!operand.isInvalid())
1790 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001791 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001792 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001793
John McCalld9bb7432011-07-27 21:50:02 +00001794 // Check the @synchronized operand now.
1795 if (!operand.isInvalid())
1796 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001797
John McCalld9bb7432011-07-27 21:50:02 +00001798 // Parse the compound statement within a new scope.
1799 ParseScope bodyScope(this, Scope::DeclScope);
1800 StmtResult body(ParseCompoundStatementBody());
1801 bodyScope.Exit();
1802
1803 // If there was a semantic or parse error earlier with the
1804 // operand, fail now.
1805 if (operand.isInvalid())
1806 return StmtError();
1807
1808 if (body.isInvalid())
1809 body = Actions.ActOnNullStmt(Tok.getLocation());
1810
1811 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001812}
1813
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001814/// objc-try-catch-statement:
1815/// @try compound-statement objc-catch-list[opt]
1816/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1817///
1818/// objc-catch-list:
1819/// @catch ( parameter-declaration ) compound-statement
1820/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1821/// catch-parameter-declaration:
1822/// parameter-declaration
1823/// '...' [OBJC2]
1824///
John McCalldadc5752010-08-24 06:29:42 +00001825StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001826 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001827
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001828 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001829 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001830 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001831 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001832 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001833 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001834 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001835 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001836 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001837 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001838 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001839 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001840
Chris Lattner0ef13522007-10-09 17:51:17 +00001841 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001842 // At this point, we need to lookahead to determine if this @ is the start
1843 // of an @catch or @finally. We don't want to consume the @ token if this
1844 // is an @try or @encode or something else.
1845 Token AfterAt = GetLookAheadToken(1);
1846 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1847 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1848 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001849
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001850 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001851 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001852 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001853 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001854 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001855 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001856 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001857 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001858 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001859 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001860 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001861 ParseDeclarator(ParmDecl);
1862
Douglas Gregore11ee112010-04-23 23:01:43 +00001863 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001864 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001865 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001866 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001867 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001868
Steve Naroff65a00892009-04-07 22:56:58 +00001869 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Steve Naroff65a00892009-04-07 22:56:58 +00001871 if (Tok.is(tok::r_paren))
1872 RParenLoc = ConsumeParen();
1873 else // Skip over garbage, until we get to ')'. Eat the ')'.
1874 SkipUntil(tok::r_paren, true, false);
1875
John McCalldadc5752010-08-24 06:29:42 +00001876 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001877 if (Tok.is(tok::l_brace))
1878 CatchBody = ParseCompoundStatementBody();
1879 else
1880 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001881 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001882 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001883
John McCalldadc5752010-08-24 06:29:42 +00001884 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001885 RParenLoc,
1886 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001887 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001888 if (!Catch.isInvalid())
1889 CatchStmts.push_back(Catch.release());
1890
Steve Naroffe6016792008-02-05 21:27:35 +00001891 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001892 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1893 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001894 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001895 }
1896 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001897 } else {
1898 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001899 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001900 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001901
John McCalldadc5752010-08-24 06:29:42 +00001902 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001903 if (Tok.is(tok::l_brace))
1904 FinallyBody = ParseCompoundStatementBody();
1905 else
1906 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001907 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001908 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001909 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001910 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001911 catch_or_finally_seen = true;
1912 break;
1913 }
1914 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001915 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001916 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001917 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001918 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001919
John McCallb268a282010-08-23 23:25:46 +00001920 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001921 CatchStmts,
John McCallb268a282010-08-23 23:25:46 +00001922 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001923}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001924
John McCall31168b02011-06-15 23:02:42 +00001925/// objc-autoreleasepool-statement:
1926/// @autoreleasepool compound-statement
1927///
1928StmtResult
1929Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1930 ConsumeToken(); // consume autoreleasepool
1931 if (Tok.isNot(tok::l_brace)) {
1932 Diag(Tok, diag::err_expected_lbrace);
1933 return StmtError();
1934 }
1935 // Enter a scope to hold everything within the compound stmt. Compound
1936 // statements can always hold declarations.
1937 ParseScope BodyScope(this, Scope::DeclScope);
1938
1939 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1940
1941 BodyScope.Exit();
1942 if (AutoreleasePoolBody.isInvalid())
1943 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1944 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1945 AutoreleasePoolBody.take());
1946}
1947
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001948/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1949/// for later parsing.
1950void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1951 LexedMethod* LM = new LexedMethod(this, MDecl);
1952 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1953 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001954 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001955 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001956 if (Tok.is(tok::kw_try)) {
1957 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00001958 if (Tok.is(tok::colon)) {
1959 Toks.push_back(Tok);
1960 ConsumeToken();
1961 while (Tok.isNot(tok::l_brace)) {
1962 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1963 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1964 }
1965 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001966 Toks.push_back(Tok); // also store '{'
1967 }
1968 else if (Tok.is(tok::colon)) {
1969 ConsumeToken();
1970 while (Tok.isNot(tok::l_brace)) {
1971 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1972 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1973 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001974 Toks.push_back(Tok); // also store '{'
1975 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001976 ConsumeBrace();
1977 // Consume everything up to (and including) the matching right brace.
1978 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001979 while (Tok.is(tok::kw_catch)) {
1980 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1981 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1982 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001983}
1984
Steve Naroff09bf8152007-09-06 21:24:23 +00001985/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001986///
John McCall48871652010-08-21 09:40:31 +00001987Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001988 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001989
John McCallfaf5fb42010-08-26 23:41:50 +00001990 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1991 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001992
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001993 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001994 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001995 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001996 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001997 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001998 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001999 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002000 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002001
Steve Naroffbb875722007-11-11 19:54:21 +00002002 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002003 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002004 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002005
Steve Naroffbb875722007-11-11 19:54:21 +00002006 // Skip over garbage, until we get to '{'. Don't eat the '{'.
2007 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00002008
Steve Naroffbb875722007-11-11 19:54:21 +00002009 // If we didn't find the '{', bail out.
2010 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00002011 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002012 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002013
2014 if (!MDecl) {
2015 ConsumeBrace();
2016 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2017 return 0;
2018 }
2019
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002020 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002021 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002022 assert (CurParsedObjCImpl
2023 && "ParseObjCMethodDefinition - Method out of @implementation");
2024 // Consume the tokens and store them for later parsing.
2025 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002026 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002027}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002028
John McCalldadc5752010-08-24 06:29:42 +00002029StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002030 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002031 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002032 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002033 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002034 }
2035
2036 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002037 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002038
2039 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002040 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002041
2042 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002043 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002044
2045 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2046 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002047
John McCalldadc5752010-08-24 06:29:42 +00002048 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002049 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002050 // If the expression is invalid, skip ahead to the next semicolon. Not
2051 // doing this opens us up to the possibility of infinite loops if
2052 // ParseExpression does not consume any tokens.
2053 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002054 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002055 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002056
Steve Naroffe6016792008-02-05 21:27:35 +00002057 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002058 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00002059 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00002060}
2061
John McCalldadc5752010-08-24 06:29:42 +00002062ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002063 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002064 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002065 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002066 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002067 return ExprError();
2068
Ted Kremeneke65b0862012-03-06 20:05:56 +00002069 case tok::minus:
2070 case tok::plus: {
2071 tok::TokenKind Kind = Tok.getKind();
2072 SourceLocation OpLoc = ConsumeToken();
2073
2074 if (!Tok.is(tok::numeric_constant)) {
2075 const char *Symbol = 0;
2076 switch (Kind) {
2077 case tok::minus: Symbol = "-"; break;
2078 case tok::plus: Symbol = "+"; break;
2079 default: llvm_unreachable("missing unary operator case");
2080 }
2081 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2082 << Symbol;
2083 return ExprError();
2084 }
2085
2086 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2087 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002088 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002089 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002090 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002091
2092 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2093 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002094 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002095
2096 return ParsePostfixExpressionSuffix(
2097 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2098 }
2099
Chris Lattnere002fbe2007-12-12 01:04:12 +00002100 case tok::string_literal: // primary-expression: string-literal
2101 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002102 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002103
2104 case tok::char_constant:
2105 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2106
2107 case tok::numeric_constant:
2108 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2109
2110 case tok::kw_true: // Objective-C++, etc.
2111 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2112 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2113 case tok::kw_false: // Objective-C++, etc.
2114 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2115 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2116
2117 case tok::l_square:
2118 // Objective-C array literal
2119 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2120
2121 case tok::l_brace:
2122 // Objective-C dictionary literal
2123 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2124
Patrick Beard0caa3942012-04-19 00:25:12 +00002125 case tok::l_paren:
2126 // Objective-C boxed expression
2127 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2128
Chris Lattnere002fbe2007-12-12 01:04:12 +00002129 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002130 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002131 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002132
Chris Lattner197a3012008-08-05 06:19:09 +00002133 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2134 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002135 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002136 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002137 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002138 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002139 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002140 default: {
2141 const char *str = 0;
2142 if (GetLookAheadToken(1).is(tok::l_brace)) {
2143 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2144 str =
2145 ch == 't' ? "try"
2146 : (ch == 'f' ? "finally"
2147 : (ch == 'a' ? "autoreleasepool" : 0));
2148 }
2149 if (str) {
2150 SourceLocation kwLoc = Tok.getLocation();
2151 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2152 FixItHint::CreateReplacement(kwLoc, str));
2153 }
2154 else
2155 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2156 }
Chris Lattner197a3012008-08-05 06:19:09 +00002157 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002158 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002159}
2160
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002161/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002162///
2163/// This routine parses the receiver of a message send in
2164/// Objective-C++ either as a type or as an expression. Note that this
2165/// routine must not be called to parse a send to 'super', since it
2166/// has no way to return such a result.
2167///
2168/// \param IsExpr Whether the receiver was parsed as an expression.
2169///
2170/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2171/// IsExpr is true), the parsed expression. If the receiver was parsed
2172/// as a type (\c IsExpr is false), the parsed type.
2173///
2174/// \returns True if an error occurred during parsing or semantic
2175/// analysis, in which case the arguments do not have valid
2176/// values. Otherwise, returns false for a successful parse.
2177///
2178/// objc-receiver: [C++]
2179/// 'super' [not parsed here]
2180/// expression
2181/// simple-type-specifier
2182/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002183bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002184 InMessageExpressionRAIIObject InMessage(*this, true);
2185
Douglas Gregor8d4de672010-04-21 22:36:40 +00002186 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2187 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2188 TryAnnotateTypeOrScopeToken();
2189
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002190 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002191 // objc-receiver:
2192 // expression
John McCalldadc5752010-08-24 06:29:42 +00002193 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002194 if (Receiver.isInvalid())
2195 return true;
2196
2197 IsExpr = true;
2198 TypeOrExpr = Receiver.take();
2199 return false;
2200 }
2201
2202 // objc-receiver:
2203 // typename-specifier
2204 // simple-type-specifier
2205 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002206 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002207 ParseCXXSimpleTypeSpecifier(DS);
2208
2209 if (Tok.is(tok::l_paren)) {
2210 // If we see an opening parentheses at this point, we are
2211 // actually parsing an expression that starts with a
2212 // function-style cast, e.g.,
2213 //
2214 // postfix-expression:
2215 // simple-type-specifier ( expression-list [opt] )
2216 // typename-specifier ( expression-list [opt] )
2217 //
2218 // Parse the remainder of this case, then the (optional)
2219 // postfix-expression suffix, followed by the (optional)
2220 // right-hand side of the binary expression. We have an
2221 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002222 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002223 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002224 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002225 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002226 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002227 if (Receiver.isInvalid())
2228 return true;
2229
2230 IsExpr = true;
2231 TypeOrExpr = Receiver.take();
2232 return false;
2233 }
2234
2235 // We have a class message. Turn the simple-type-specifier or
2236 // typename-specifier we parsed into a type and parse the
2237 // remainder of the class message.
2238 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002239 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002240 if (Type.isInvalid())
2241 return true;
2242
2243 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002244 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002245 return false;
2246}
2247
Douglas Gregor990ccac2010-05-31 14:40:22 +00002248/// \brief Determine whether the parser is currently referring to a an
2249/// Objective-C message send, using a simplified heuristic to avoid overhead.
2250///
2251/// This routine will only return true for a subset of valid message-send
2252/// expressions.
2253bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002254 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002255 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002256 return GetLookAheadToken(1).is(tok::identifier) &&
2257 GetLookAheadToken(2).is(tok::identifier);
2258}
2259
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002260bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002261 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002262 InMessageExpression)
2263 return false;
2264
2265
2266 ParsedType Type;
2267
2268 if (Tok.is(tok::annot_typename))
2269 Type = getTypeAnnotation(Tok);
2270 else if (Tok.is(tok::identifier))
2271 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2272 getCurScope());
2273 else
2274 return false;
2275
2276 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2277 const Token &AfterNext = GetLookAheadToken(2);
2278 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2279 if (Tok.is(tok::identifier))
2280 TryAnnotateTypeOrScopeToken();
2281
2282 return Tok.is(tok::annot_typename);
2283 }
2284 }
2285
2286 return false;
2287}
2288
Mike Stump11289f42009-09-09 15:08:12 +00002289/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002290/// '[' objc-receiver objc-message-args ']'
2291///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002292/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002293/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002294/// expression
2295/// class-name
2296/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002297///
John McCalldadc5752010-08-24 06:29:42 +00002298ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002299 assert(Tok.is(tok::l_square) && "'[' expected");
2300 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2301
Douglas Gregora817a192010-05-27 23:06:34 +00002302 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002303 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002304 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002305 return ExprError();
2306 }
2307
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002308 InMessageExpressionRAIIObject InMessage(*this, true);
2309
David Blaikiebbafb8a2012-03-11 07:00:24 +00002310 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002311 // We completely separate the C and C++ cases because C++ requires
2312 // more complicated (read: slower) parsing.
2313
2314 // Handle send to super.
2315 // FIXME: This doesn't benefit from the same typo-correction we
2316 // get in Objective-C.
2317 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002318 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002319 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2320 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002321
2322 // Parse the receiver, which is either a type or an expression.
2323 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002324 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002325 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2326 SkipUntil(tok::r_square);
2327 return ExprError();
2328 }
2329
2330 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002331 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2332 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002333 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002334
2335 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002336 ParsedType::getFromOpaquePtr(TypeOrExpr),
2337 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002338 }
2339
2340 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002341 IdentifierInfo *Name = Tok.getIdentifierInfo();
2342 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002343 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002344 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002345 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002346 NextToken().is(tok::period),
2347 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002348 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002349 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2350 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002351
John McCallfaf5fb42010-08-26 23:41:50 +00002352 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002353 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002354 SkipUntil(tok::r_square);
2355 return ExprError();
2356 }
2357
Douglas Gregore5798dc2010-04-21 20:38:13 +00002358 ConsumeToken(); // the type name
2359
2360 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002361 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002362
John McCallfaf5fb42010-08-26 23:41:50 +00002363 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002364 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002365 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002366 }
Chris Lattner8f697062008-01-25 18:59:06 +00002367 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002368
2369 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002370 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002371 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002372 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002373 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002374 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002375
John McCallba7bf592010-08-24 05:47:05 +00002376 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2377 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002378}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002379
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002380/// \brief Parse the remainder of an Objective-C message following the
2381/// '[' objc-receiver.
2382///
2383/// This routine handles sends to super, class messages (sent to a
2384/// class name), and instance messages (sent to an object), and the
2385/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2386/// ReceiverExpr, respectively. Only one of these parameters may have
2387/// a valid value.
2388///
2389/// \param LBracLoc The location of the opening '['.
2390///
2391/// \param SuperLoc If this is a send to 'super', the location of the
2392/// 'super' keyword that indicates a send to the superclass.
2393///
2394/// \param ReceiverType If this is a class message, the type of the
2395/// class we are sending a message to.
2396///
2397/// \param ReceiverExpr If this is an instance message, the expression
2398/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002399///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002400/// objc-message-args:
2401/// objc-selector
2402/// objc-keywordarg-list
2403///
2404/// objc-keywordarg-list:
2405/// objc-keywordarg
2406/// objc-keywordarg-list objc-keywordarg
2407///
Mike Stump11289f42009-09-09 15:08:12 +00002408/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002409/// selector-name[opt] ':' objc-keywordexpr
2410///
2411/// objc-keywordexpr:
2412/// nonempty-expr-list
2413///
2414/// nonempty-expr-list:
2415/// assignment-expression
2416/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002417///
John McCalldadc5752010-08-24 06:29:42 +00002418ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002419Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002420 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002421 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002422 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002423 InMessageExpressionRAIIObject InMessage(*this, true);
2424
Steve Naroffeae65032009-11-07 02:08:14 +00002425 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002426 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002427 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2428 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002429 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002430 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2431 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002432 else
John McCallb268a282010-08-23 23:25:46 +00002433 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002434 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002435 cutOffParsing();
2436 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002437 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002438
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002439 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002440 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002441 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002442
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002443 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002444 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002445 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002446
Chris Lattner0ef13522007-10-09 17:51:17 +00002447 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002448 while (1) {
2449 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002450 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002451 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002452
Chris Lattner0ef13522007-10-09 17:51:17 +00002453 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002454 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002455 // We must manually skip to a ']', otherwise the expression skipper will
2456 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2457 // the enclosing expression.
2458 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002459 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002460 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002461
Steve Narofff73590d2007-09-27 14:38:14 +00002462 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002463 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002464
2465 if (Tok.is(tok::code_completion)) {
2466 if (SuperLoc.isValid())
2467 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2468 KeyIdents.data(),
2469 KeyIdents.size(),
2470 /*AtArgumentEpression=*/true);
2471 else if (ReceiverType)
2472 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2473 KeyIdents.data(),
2474 KeyIdents.size(),
2475 /*AtArgumentEpression=*/true);
2476 else
2477 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2478 KeyIdents.data(),
2479 KeyIdents.size(),
2480 /*AtArgumentEpression=*/true);
2481
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002482 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002483 return ExprError();
2484 }
2485
John McCalldadc5752010-08-24 06:29:42 +00002486 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002487 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002488 // We must manually skip to a ']', otherwise the expression skipper will
2489 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2490 // the enclosing expression.
2491 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002492 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002493 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002494
Steve Naroff486760a2007-09-17 20:25:27 +00002495 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002496 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002497
Douglas Gregor1b605f72009-11-19 01:08:35 +00002498 // Code completion after each argument.
2499 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002500 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002501 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002502 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002503 KeyIdents.size(),
2504 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002505 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002506 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002507 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002508 KeyIdents.size(),
2509 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002510 else
John McCallb268a282010-08-23 23:25:46 +00002511 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002512 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002513 KeyIdents.size(),
2514 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002515 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002516 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002517 }
2518
Steve Naroff486760a2007-09-17 20:25:27 +00002519 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002520 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002521 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002522 break;
2523 // We have a selector or a colon, continue parsing.
2524 }
2525 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002526 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002527 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002528 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002529 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002530 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002531 if (Tok.is(tok::colon)) {
2532 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2533 FixItHint::CreateRemoval(commaLoc);
2534 }
Chris Lattner197a3012008-08-05 06:19:09 +00002535 // We must manually skip to a ']', otherwise the expression skipper will
2536 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2537 // the enclosing expression.
2538 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002539 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002540 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002541
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002542 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002543 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002544 }
2545 } else if (!selIdent) {
2546 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002547
Chris Lattner197a3012008-08-05 06:19:09 +00002548 // We must manually skip to a ']', otherwise the expression skipper will
2549 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2550 // the enclosing expression.
2551 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002552 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002553 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002554
Chris Lattner0ef13522007-10-09 17:51:17 +00002555 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002556 if (Tok.is(tok::identifier))
2557 Diag(Tok, diag::err_expected_colon);
2558 else
2559 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002560 // We must manually skip to a ']', otherwise the expression skipper will
2561 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2562 // the enclosing expression.
2563 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002564 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002565 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002566
Chris Lattner8f697062008-01-25 18:59:06 +00002567 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002568
Steve Naroffe61bfa82007-10-05 18:42:47 +00002569 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002570 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002571 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002572 KeyLocs.push_back(Loc);
2573 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002574 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002575
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002576 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002577 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002578 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002579 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002580 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002581 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002582 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002583 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002584}
2585
John McCalldadc5752010-08-24 06:29:42 +00002586ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2587 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002588 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002589
Chris Lattnere002fbe2007-12-12 01:04:12 +00002590 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2591 // expressions. At this point, we know that the only valid thing that starts
2592 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002593 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002594 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002595 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002596 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002597
Chris Lattnere002fbe2007-12-12 01:04:12 +00002598 while (Tok.is(tok::at)) {
2599 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002600
Sebastian Redlc13f2682008-12-09 20:22:58 +00002601 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002602 if (!isTokenStringLiteral())
2603 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002604
John McCalldadc5752010-08-24 06:29:42 +00002605 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002606 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002607 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002608
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002609 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002610 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002611
Benjamin Kramerf0623432012-08-23 22:51:59 +00002612 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002613 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002614}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002615
Ted Kremeneke65b0862012-03-06 20:05:56 +00002616/// ParseObjCBooleanLiteral -
2617/// objc-scalar-literal : '@' boolean-keyword
2618/// ;
2619/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2620/// ;
2621ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2622 bool ArgValue) {
2623 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2624 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2625}
2626
2627/// ParseObjCCharacterLiteral -
2628/// objc-scalar-literal : '@' character-literal
2629/// ;
2630ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2631 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2632 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002633 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002634 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002635 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002636 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2637}
2638
2639/// ParseObjCNumericLiteral -
2640/// objc-scalar-literal : '@' scalar-literal
2641/// ;
2642/// scalar-literal : | numeric-constant /* any numeric constant. */
2643/// ;
2644ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2645 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2646 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002647 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002648 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002649 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002650 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2651}
2652
Patrick Beard0caa3942012-04-19 00:25:12 +00002653/// ParseObjCBoxedExpr -
2654/// objc-box-expression:
2655/// @( assignment-expression )
2656ExprResult
2657Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2658 if (Tok.isNot(tok::l_paren))
2659 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2660
2661 BalancedDelimiterTracker T(*this, tok::l_paren);
2662 T.consumeOpen();
2663 ExprResult ValueExpr(ParseAssignmentExpression());
2664 if (T.consumeClose())
2665 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002666
2667 if (ValueExpr.isInvalid())
2668 return ExprError();
2669
Patrick Beard0caa3942012-04-19 00:25:12 +00002670 // Wrap the sub-expression in a parenthesized expression, to distinguish
2671 // a boxed expression from a literal.
2672 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2673 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
2674 return Owned(Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2675 ValueExpr.take()));
2676}
2677
Ted Kremeneke65b0862012-03-06 20:05:56 +00002678ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002679 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002680 ConsumeBracket(); // consume the l_square.
2681
2682 while (Tok.isNot(tok::r_square)) {
2683 // Parse list of array element expressions (all must be id types).
2684 ExprResult Res(ParseAssignmentExpression());
2685 if (Res.isInvalid()) {
2686 // We must manually skip to a ']', otherwise the expression skipper will
2687 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2688 // the enclosing expression.
2689 SkipUntil(tok::r_square);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002690 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002691 }
2692
2693 // Parse the ellipsis that indicates a pack expansion.
2694 if (Tok.is(tok::ellipsis))
2695 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2696 if (Res.isInvalid())
2697 return true;
2698
2699 ElementExprs.push_back(Res.release());
2700
2701 if (Tok.is(tok::comma))
2702 ConsumeToken(); // Eat the ','.
2703 else if (Tok.isNot(tok::r_square))
2704 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2705 }
2706 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002707 MultiExprArg Args(ElementExprs);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002708 return Owned(Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args));
2709}
2710
2711ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2712 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2713 ConsumeBrace(); // consume the l_square.
2714 while (Tok.isNot(tok::r_brace)) {
2715 // Parse the comma separated key : value expressions.
2716 ExprResult KeyExpr;
2717 {
2718 ColonProtectionRAIIObject X(*this);
2719 KeyExpr = ParseAssignmentExpression();
2720 if (KeyExpr.isInvalid()) {
2721 // We must manually skip to a '}', otherwise the expression skipper will
2722 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2723 // the enclosing expression.
2724 SkipUntil(tok::r_brace);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002725 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002726 }
2727 }
2728
2729 if (Tok.is(tok::colon)) {
2730 ConsumeToken();
2731 } else {
2732 return ExprError(Diag(Tok, diag::err_expected_colon));
2733 }
2734
2735 ExprResult ValueExpr(ParseAssignmentExpression());
2736 if (ValueExpr.isInvalid()) {
2737 // We must manually skip to a '}', otherwise the expression skipper will
2738 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2739 // the enclosing expression.
2740 SkipUntil(tok::r_brace);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002741 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002742 }
2743
2744 // Parse the ellipsis that designates this as a pack expansion.
2745 SourceLocation EllipsisLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002746 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremeneke65b0862012-03-06 20:05:56 +00002747 EllipsisLoc = ConsumeToken();
2748
2749 // We have a valid expression. Collect it in a vector so we can
2750 // build the argument list.
2751 ObjCDictionaryElement Element = {
2752 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, llvm::Optional<unsigned>()
2753 };
2754 Elements.push_back(Element);
2755
2756 if (Tok.is(tok::comma))
2757 ConsumeToken(); // Eat the ','.
2758 else if (Tok.isNot(tok::r_brace))
2759 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2760 }
2761 SourceLocation EndLoc = ConsumeBrace();
2762
2763 // Create the ObjCDictionaryLiteral.
2764 return Owned(Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2765 Elements.data(),
2766 Elements.size()));
2767}
2768
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002769/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002770/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002771ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002772Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002773 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002774
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002775 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002776
Chris Lattner197a3012008-08-05 06:19:09 +00002777 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002778 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2779
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002780 BalancedDelimiterTracker T(*this, tok::l_paren);
2781 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002782
Douglas Gregor220cac52009-02-18 17:45:20 +00002783 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002784
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002785 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002786
Douglas Gregor220cac52009-02-18 17:45:20 +00002787 if (Ty.isInvalid())
2788 return ExprError();
2789
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002790 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2791 T.getOpenLocation(), Ty.get(),
2792 T.getCloseLocation()));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002793}
Anders Carlssone01493d2007-08-23 15:25:28 +00002794
2795/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002796/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002797ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002798Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002799 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002800
Chris Lattner197a3012008-08-05 06:19:09 +00002801 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002802 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2803
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002804 BalancedDelimiterTracker T(*this, tok::l_paren);
2805 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002806
Chris Lattner197a3012008-08-05 06:19:09 +00002807 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002808 return ExprError(Diag(Tok, diag::err_expected_ident));
2809
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002810 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002811 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002812
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002813 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002814
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002815 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002816 T.getOpenLocation(),
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002817 ProtoIdLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002818 T.getCloseLocation()));
Anders Carlssone01493d2007-08-23 15:25:28 +00002819}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002820
2821/// objc-selector-expression
2822/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002823ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002824 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002825
Chris Lattner197a3012008-08-05 06:19:09 +00002826 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002827 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2828
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002829 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002830 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002831
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002832 BalancedDelimiterTracker T(*this, tok::l_paren);
2833 T.consumeOpen();
2834
Douglas Gregor67c692c2010-08-26 15:07:07 +00002835 if (Tok.is(tok::code_completion)) {
2836 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2837 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002838 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002839 return ExprError();
2840 }
2841
Chris Lattner4f472a32009-04-11 18:13:45 +00002842 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002843 if (!SelIdent && // missing selector name.
2844 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002845 return ExprError(Diag(Tok, diag::err_expected_ident));
2846
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002847 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002848 unsigned nColons = 0;
2849 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002850 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002851 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2852 ++nColons;
2853 KeyIdents.push_back(0);
2854 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002855 return ExprError(Diag(Tok, diag::err_expected_colon));
2856
Chris Lattner1ba64452010-08-27 22:32:41 +00002857 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002858 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002859 if (Tok.is(tok::r_paren))
2860 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002861
2862 if (Tok.is(tok::code_completion)) {
2863 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2864 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002865 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002866 return ExprError();
2867 }
2868
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002869 // Check for another keyword selector.
2870 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002871 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002872 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002873 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002874 break;
2875 }
Steve Naroff152dd812007-12-05 22:21:29 +00002876 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002877 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002878 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002879 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002880 T.getOpenLocation(),
2881 T.getCloseLocation()));
Gabor Greif24032f12007-10-19 15:38:32 +00002882 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002883
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002884void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2885 // MCDecl might be null due to error in method or c-function prototype, etc.
2886 Decl *MCDecl = LM.D;
2887 bool skip = MCDecl &&
2888 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2889 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2890 if (skip)
2891 return;
2892
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002893 // Save the current token position.
2894 SourceLocation OrigLoc = Tok.getLocation();
2895
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002896 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2897 // Append the current token at the end of the new token stream so that it
2898 // doesn't get lost.
2899 LM.Toks.push_back(Tok);
2900 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2901
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002902 // Consume the previously pushed token.
2903 ConsumeAnyToken();
2904
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002905 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2906 Tok.is(tok::colon)) &&
2907 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002908 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002909 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002910 parseMethod
2911 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2912 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002913
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002914 // Tell the actions module that we have entered a method or c-function definition
2915 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00002916 if (parseMethod)
2917 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2918 else
2919 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002920 if (Tok.is(tok::kw_try))
2921 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002922 else {
2923 if (Tok.is(tok::colon))
2924 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002925 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002926 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00002927
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002928 if (Tok.getLocation() != OrigLoc) {
2929 // Due to parsing error, we either went over the cached tokens or
2930 // there are still cached tokens left. If it's the latter case skip the
2931 // leftover tokens.
2932 // Since this is an uncommon situation that should be avoided, use the
2933 // expensive isBeforeInTranslationUnit call.
2934 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2935 OrigLoc))
2936 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2937 ConsumeAnyToken();
2938 }
2939
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002940 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002941}