blob: 8260d188a341b23583ab8ac2dc2f4e8d7f83ef07 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000039 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000045 break;
John McCall7f040a92010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall7f040a92010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Douglas Gregorbd9482d2012-01-01 21:23:57 +000053 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall7f040a92010-12-24 02:08:15 +000054 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000055 case tok::objc_implementation:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000056 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
57 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000058 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000059 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000060 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
62 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000063 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000064 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
65 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000066 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000067 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
68 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000069 default:
70 Diag(AtLoc, diag::err_unexpected_at);
71 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000072 SingleDecl = 0;
73 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000075 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000076}
77
78///
Mike Stump1eb44332009-09-09 15:08:12 +000079/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000080/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000081///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000082Parser::DeclGroupPtrTy
83Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000084 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000085 SmallVector<IdentifierInfo *, 8> ClassNames;
86 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000087
Mike Stump1eb44332009-09-09 15:08:12 +000088
Reid Spencer5f016e22007-07-11 17:01:13 +000089 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000090 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 Diag(Tok, diag::err_expected_ident);
92 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000093 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000094 }
Reid Spencer5f016e22007-07-11 17:01:13 +000095 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000096 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattnerdf195262007-10-09 17:51:17 +000099 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 ConsumeToken();
103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // Consume the ';'.
106 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000107 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenekc09cba62009-11-17 23:12:20 +0000109 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
110 ClassLocs.data(),
111 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000112}
113
Erik Verbruggend64251f2011-12-06 09:25:23 +0000114void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
115{
116 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
117 if (ock == Sema::OCK_None)
118 return;
119
120 Decl *Decl = Actions.ActOnAtEnd(getCurScope(), AtLoc);
121 Diag(AtLoc, diag::err_objc_missing_end)
122 << FixItHint::CreateInsertion(AtLoc, "@end\n");
123 if (Decl)
124 Diag(Decl->getLocStart(), diag::note_objc_container_start)
125 << (int) ock;
126 if (!PendingObjCImpDecl.empty())
127 PendingObjCImpDecl.pop_back();
128 ObjCImpDecl = 0;
129}
130
Steve Naroffdac269b2007-08-20 21:31:48 +0000131///
132/// objc-interface:
133/// objc-class-interface-attributes[opt] objc-class-interface
134/// objc-category-interface
135///
136/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000137/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000138/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000139/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000140/// objc-interface-decl-list
141/// @end
142///
143/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000144/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000145/// objc-protocol-refs[opt]
146/// objc-interface-decl-list
147/// @end
148///
149/// objc-superclass:
150/// ':' identifier
151///
152/// objc-class-interface-attributes:
153/// __attribute__((visibility("default")))
154/// __attribute__((visibility("hidden")))
155/// __attribute__((deprecated))
156/// __attribute__((unavailable))
157/// __attribute__((objc_exception)) - used by NSException on 64-bit
158///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000159Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000160 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000161 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000162 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000163 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000166 // Code completion after '@interface'.
167 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000168 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000169 cutOffParsing();
170 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000171 }
172
Chris Lattnerdf195262007-10-09 17:51:17 +0000173 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000175 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000177
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000179 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000181 if (Tok.is(tok::l_paren) &&
182 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183
184 BalancedDelimiterTracker T(*this, tok::l_paren);
185 T.consumeOpen();
186
187 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000188 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000189 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000190 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000191 cutOffParsing();
192 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000193 }
194
Steve Naroff527fe232007-08-23 19:56:30 +0000195 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000196 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000197 categoryId = Tok.getIdentifierInfo();
198 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000199 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000200 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000201 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000202 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000204
205 T.consumeClose();
206 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000207 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000208
209 if (!attrs.empty()) { // categories don't support attributes.
210 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
211 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000212 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000213
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000214 // Next, we need to check for any protocol references.
215 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000216 SmallVector<Decl *, 8> ProtocolRefs;
217 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000218 if (Tok.is(tok::less) &&
219 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000220 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000221 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000222
John McCalld226f652010-08-21 09:40:31 +0000223 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000224 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000225 nameId, nameLoc,
226 categoryId, categoryLoc,
227 ProtocolRefs.data(),
228 ProtocolRefs.size(),
229 ProtocolLocs.data(),
230 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000231
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000232 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000233 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000234
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000235 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000236 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000237 }
238 // Parse a class interface.
239 IdentifierInfo *superClassId = 0;
240 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000241
Chris Lattnerdf195262007-10-09 17:51:17 +0000242 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000243 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000244
245 // Code completion of superclass names.
246 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000247 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000248 cutOffParsing();
249 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000250 }
251
Chris Lattnerdf195262007-10-09 17:51:17 +0000252 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000253 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000254 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000255 }
256 superClassId = Tok.getIdentifierInfo();
257 superClassLoc = ConsumeToken();
258 }
259 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000260 SmallVector<Decl *, 8> ProtocolRefs;
261 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000262 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000263 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000264 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
265 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000266 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
John McCalld226f652010-08-21 09:40:31 +0000268 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000269 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000270 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000271 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000272 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000273 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerdf195262007-10-09 17:51:17 +0000275 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000276 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000277
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000278 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000279 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000280}
281
John McCalld0014542009-12-03 22:31:13 +0000282/// The Objective-C property callback. This should be defined where
283/// it's used, but instead it's been lifted to here to support VS2005.
284struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000285private:
286 virtual void anchor();
287public:
John McCalld0014542009-12-03 22:31:13 +0000288 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000289 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000290 ObjCDeclSpec &OCDS;
291 SourceLocation AtLoc;
292 tok::ObjCKeywordKind MethodImplKind;
293
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000294 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000296 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
297 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000298 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000299 MethodImplKind(MethodImplKind) {
300 }
301
John McCalld226f652010-08-21 09:40:31 +0000302 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000303 if (FD.D.getIdentifier() == 0) {
304 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
305 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000306 return 0;
John McCalld0014542009-12-03 22:31:13 +0000307 }
308 if (FD.BitfieldSize) {
309 P.Diag(AtLoc, diag::err_objc_property_bitfield)
310 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000311 return 0;
John McCalld0014542009-12-03 22:31:13 +0000312 }
313
314 // Install the property declarator into interfaceDecl.
315 IdentifierInfo *SelName =
316 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
317
318 Selector GetterSel =
319 P.PP.getSelectorTable().getNullarySelector(SelName);
320 IdentifierInfo *SetterName = OCDS.getSetterName();
321 Selector SetterSel;
322 if (SetterName)
323 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
324 else
325 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
326 P.PP.getSelectorTable(),
327 FD.D.getIdentifier());
328 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000329 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000330 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000331 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000332 &isOverridingProperty,
333 MethodImplKind);
334 if (!isOverridingProperty)
335 Props.push_back(Property);
336
337 return Property;
338 }
339};
340
David Blaikie99ba9e32011-12-20 02:48:34 +0000341void Parser::ObjCPropertyCallback::anchor() {
342}
343
Steve Naroffdac269b2007-08-20 21:31:48 +0000344/// objc-interface-decl-list:
345/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000346/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000347/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000348/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000349/// objc-interface-decl-list declaration
350/// objc-interface-decl-list ';'
351///
Steve Naroff294494e2007-08-22 16:35:03 +0000352/// objc-method-requirement: [OBJC2]
353/// @required
354/// @optional
355///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000356void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
357 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000358 SmallVector<Decl *, 32> allMethods;
359 SmallVector<Decl *, 16> allProperties;
360 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000361 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek782f2f52010-01-07 01:20:12 +0000363 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000364
Steve Naroff294494e2007-08-22 16:35:03 +0000365 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000366 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000367 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000368 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000370 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000371 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
372 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000373 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
374 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
375 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
376 if (Tok.is(tok::semi))
377 ConsumeToken();
378 }
Steve Naroff294494e2007-08-22 16:35:03 +0000379 continue;
380 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000381 if (Tok.is(tok::l_paren)) {
382 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000383 ParseObjCMethodDecl(Tok.getLocation(),
384 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000385 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000386 continue;
387 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000388 // Ignore excess semicolons.
389 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000390 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000391 continue;
392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattnerbc662af2008-10-20 06:10:06 +0000394 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000395 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000396 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000398 // Code completion within an Objective-C interface.
399 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000400 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000401 ObjCImpDecl? Sema::PCC_ObjCImplementation
402 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000403 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000404 }
405
Chris Lattnere82a10f2008-10-20 05:46:22 +0000406 // If we don't have an @ directive, parse it as a function definition.
407 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000408 // The code below does not consume '}'s because it is afraid of eating the
409 // end of a namespace. Because of the way this code is structured, an
410 // erroneous r_brace would cause an infinite loop if not handled here.
411 if (Tok.is(tok::r_brace))
412 break;
John McCall0b7e6782011-03-24 11:26:52 +0000413 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000414 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000415 continue;
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattnere82a10f2008-10-20 05:46:22 +0000418 // Otherwise, we have an @ directive, eat the @.
419 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000420 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000421 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000422 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000423 break;
424 }
425
Chris Lattnera2449b22008-10-20 05:57:40 +0000426 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattnera2449b22008-10-20 05:57:40 +0000428 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000429 AtEnd.setBegin(AtLoc);
430 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000431 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000432 } else if (DirectiveKind == tok::objc_not_keyword) {
433 Diag(Tok, diag::err_objc_unknown_at);
434 SkipUntil(tok::semi);
435 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattnerbc662af2008-10-20 06:10:06 +0000438 // Eat the identifier.
439 ConsumeToken();
440
Chris Lattnera2449b22008-10-20 05:57:40 +0000441 switch (DirectiveKind) {
442 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000443 // FIXME: If someone forgets an @end on a protocol, this loop will
444 // continue to eat up tons of stuff and spew lots of nonsense errors. It
445 // would probably be better to bail out if we saw an @class or @interface
446 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000447 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000448 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000449 SkipUntil(tok::r_brace, tok::at);
450 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000451
452 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000453 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000454 Diag(AtLoc, diag::err_objc_missing_end)
455 << FixItHint::CreateInsertion(AtLoc, "@end\n");
456 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
457 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000458 ConsumeToken();
459 break;
460
Chris Lattnera2449b22008-10-20 05:57:40 +0000461 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000462 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000463 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000464 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000465 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000466 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000467 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000468 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000469 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000472 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000473 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000474
Chris Lattnere82a10f2008-10-20 05:46:22 +0000475 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000476 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000477 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000478 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000480 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000481 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000482
Chris Lattnere82a10f2008-10-20 05:46:22 +0000483 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000484 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000485 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCall7da19ea2011-03-26 01:53:26 +0000487 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000488 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000489 }
Steve Naroff294494e2007-08-22 16:35:03 +0000490 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000491
492 // We break out of the big loop in two cases: when we see @end or when we see
493 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000494 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000495 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000496 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000497 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000498 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000499 } else {
500 Diag(Tok, diag::err_objc_missing_end)
501 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
502 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
503 << (int) Actions.getObjCContainerKind();
504 AtEnd.setBegin(Tok.getLocation());
505 AtEnd.setEnd(Tok.getLocation());
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattnera2449b22008-10-20 05:57:40 +0000508 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000509 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000510 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000511 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000512 allProperties.data(), allProperties.size(),
513 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000514}
515
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000516/// Parse property attribute declarations.
517///
518/// property-attr-decl: '(' property-attrlist ')'
519/// property-attrlist:
520/// property-attribute
521/// property-attrlist ',' property-attribute
522/// property-attribute:
523/// getter '=' identifier
524/// setter '=' identifier ':'
525/// readonly
526/// readwrite
527/// assign
528/// retain
529/// copy
530/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000531/// atomic
532/// strong
533/// weak
534/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000535///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000536void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000537 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000538 BalancedDelimiterTracker T(*this, tok::l_paren);
539 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000541 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000542 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000543 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000544 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000545 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000546 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000548 // If this is not an identifier at all, bail out early.
549 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000550 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000551 return;
552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattner156b0612008-10-20 07:37:22 +0000554 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Chris Lattner92e62b02008-11-20 04:42:34 +0000556 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000557 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000558 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000559 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000560 else if (II->isStr("unsafe_unretained"))
561 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000562 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000564 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000566 else if (II->isStr("strong"))
567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000568 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000570 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000572 else if (II->isStr("atomic"))
573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000574 else if (II->isStr("weak"))
575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000576 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000577 bool IsSetter = II->getNameStart()[0] == 's';
578
Chris Lattnere00da7c2008-10-20 07:39:53 +0000579 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000580 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
581 diag::err_objc_expected_equal_for_getter;
582
583 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000584 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Douglas Gregor4ad96852009-11-19 07:41:15 +0000586 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000587 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000588 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000589 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000590 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000591 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000592 }
593
Anders Carlsson42499be2010-10-02 17:45:21 +0000594
595 SourceLocation SelLoc;
596 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
597
598 if (!SelIdent) {
599 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
600 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000601 SkipUntil(tok::r_paren);
602 return;
603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlsson42499be2010-10-02 17:45:21 +0000605 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000606 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000607 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000609 if (ExpectAndConsume(tok::colon,
610 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000611 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000612 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000613 } else {
614 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000615 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000616 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000617 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000618 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000619 SkipUntil(tok::r_paren);
620 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Chris Lattner156b0612008-10-20 07:37:22 +0000623 if (Tok.isNot(tok::comma))
624 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Chris Lattner156b0612008-10-20 07:37:22 +0000626 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000629 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000630}
631
Steve Naroff3536b442007-09-06 21:24:23 +0000632/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000633/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000634/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000635///
636/// objc-instance-method: '-'
637/// objc-class-method: '+'
638///
Steve Naroff4985ace2007-08-22 18:35:33 +0000639/// objc-method-attributes: [OBJC2]
640/// __attribute__((deprecated))
641///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000642Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000643 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000644 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000645
Mike Stump1eb44332009-09-09 15:08:12 +0000646 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000647 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000648 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000649 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000650 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000651 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000652 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000653}
654
655/// objc-selector:
656/// identifier
657/// one of
658/// enum struct union if else while do for switch case default
659/// break continue return goto asm sizeof typeof __alignof
660/// unsigned long const short volatile signed restrict _Complex
661/// in out inout bycopy byref oneway int char float double void _Bool
662///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000663IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000664
Chris Lattnerff384912007-10-07 02:00:24 +0000665 switch (Tok.getKind()) {
666 default:
667 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000668 case tok::ampamp:
669 case tok::ampequal:
670 case tok::amp:
671 case tok::pipe:
672 case tok::tilde:
673 case tok::exclaim:
674 case tok::exclaimequal:
675 case tok::pipepipe:
676 case tok::pipeequal:
677 case tok::caret:
678 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000679 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000680 if (isalpha(ThisTok[0])) {
681 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
682 Tok.setKind(tok::identifier);
683 SelectorLoc = ConsumeToken();
684 return II;
685 }
686 return 0;
687 }
688
Chris Lattnerff384912007-10-07 02:00:24 +0000689 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000690 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000691 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000692 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000693 case tok::kw_break:
694 case tok::kw_case:
695 case tok::kw_catch:
696 case tok::kw_char:
697 case tok::kw_class:
698 case tok::kw_const:
699 case tok::kw_const_cast:
700 case tok::kw_continue:
701 case tok::kw_default:
702 case tok::kw_delete:
703 case tok::kw_do:
704 case tok::kw_double:
705 case tok::kw_dynamic_cast:
706 case tok::kw_else:
707 case tok::kw_enum:
708 case tok::kw_explicit:
709 case tok::kw_export:
710 case tok::kw_extern:
711 case tok::kw_false:
712 case tok::kw_float:
713 case tok::kw_for:
714 case tok::kw_friend:
715 case tok::kw_goto:
716 case tok::kw_if:
717 case tok::kw_inline:
718 case tok::kw_int:
719 case tok::kw_long:
720 case tok::kw_mutable:
721 case tok::kw_namespace:
722 case tok::kw_new:
723 case tok::kw_operator:
724 case tok::kw_private:
725 case tok::kw_protected:
726 case tok::kw_public:
727 case tok::kw_register:
728 case tok::kw_reinterpret_cast:
729 case tok::kw_restrict:
730 case tok::kw_return:
731 case tok::kw_short:
732 case tok::kw_signed:
733 case tok::kw_sizeof:
734 case tok::kw_static:
735 case tok::kw_static_cast:
736 case tok::kw_struct:
737 case tok::kw_switch:
738 case tok::kw_template:
739 case tok::kw_this:
740 case tok::kw_throw:
741 case tok::kw_true:
742 case tok::kw_try:
743 case tok::kw_typedef:
744 case tok::kw_typeid:
745 case tok::kw_typename:
746 case tok::kw_typeof:
747 case tok::kw_union:
748 case tok::kw_unsigned:
749 case tok::kw_using:
750 case tok::kw_virtual:
751 case tok::kw_void:
752 case tok::kw_volatile:
753 case tok::kw_wchar_t:
754 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000755 case tok::kw__Bool:
756 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000757 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000758 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000759 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000760 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000761 }
Steve Naroff294494e2007-08-22 16:35:03 +0000762}
763
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000764/// objc-for-collection-in: 'in'
765///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000766bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000767 // FIXME: May have to do additional look-ahead to only allow for
768 // valid tokens following an 'in'; such as an identifier, unary operators,
769 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000770 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000771 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000772}
773
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000774/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000775/// qualifier list and builds their bitmask representation in the input
776/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000777///
778/// objc-type-qualifiers:
779/// objc-type-qualifier
780/// objc-type-qualifiers objc-type-qualifier
781///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000782void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000783 Declarator::TheContext Context) {
784 assert(Context == Declarator::ObjCParameterContext ||
785 Context == Declarator::ObjCResultContext);
786
Chris Lattnere8b724d2007-12-12 06:56:32 +0000787 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000788 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000789 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000790 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000791 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000792 }
793
Chris Lattnercb53b362007-12-27 19:57:00 +0000794 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000795 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Chris Lattnere8b724d2007-12-12 06:56:32 +0000797 const IdentifierInfo *II = Tok.getIdentifierInfo();
798 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000799 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000800 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000803 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000804 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
806 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
807 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
808 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
809 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
810 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000811 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000813 ConsumeToken();
814 II = 0;
815 break;
816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattnere8b724d2007-12-12 06:56:32 +0000818 // If this wasn't a recognized qualifier, bail out.
819 if (II) return;
820 }
821}
822
John McCallcdda47f2011-10-01 09:56:14 +0000823/// Take all the decl attributes out of the given list and add
824/// them to the given attribute set.
825static void takeDeclAttributes(ParsedAttributes &attrs,
826 AttributeList *list) {
827 while (list) {
828 AttributeList *cur = list;
829 list = cur->getNext();
830
831 if (!cur->isUsedAsTypeAttr()) {
832 // Clear out the next pointer. We're really completely
833 // destroying the internal invariants of the declarator here,
834 // but it doesn't matter because we're done with it.
835 cur->setNext(0);
836 attrs.add(cur);
837 }
838 }
839}
840
841/// takeDeclAttributes - Take all the decl attributes from the given
842/// declarator and add them to the given list.
843static void takeDeclAttributes(ParsedAttributes &attrs,
844 Declarator &D) {
845 // First, take ownership of all attributes.
846 attrs.getPool().takeAllFrom(D.getAttributePool());
847 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
848
849 // Now actually move the attributes over.
850 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
851 takeDeclAttributes(attrs, D.getAttributes());
852 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
853 takeDeclAttributes(attrs,
854 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
855}
856
Chris Lattnere8b724d2007-12-12 06:56:32 +0000857/// objc-type-name:
858/// '(' objc-type-qualifiers[opt] type-name ')'
859/// '(' objc-type-qualifiers[opt] ')'
860///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000861ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000862 Declarator::TheContext context,
863 ParsedAttributes *paramAttrs) {
864 assert(context == Declarator::ObjCParameterContext ||
865 context == Declarator::ObjCResultContext);
866 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
867
Chris Lattnerdf195262007-10-09 17:51:17 +0000868 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000870 BalancedDelimiterTracker T(*this, tok::l_paren);
871 T.consumeOpen();
872
Chris Lattnere8904e92008-08-23 01:48:03 +0000873 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000874 ObjCDeclContextSwitch ObjCDC(*this);
875
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000876 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000877 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000878
John McCallb3d87482010-08-24 05:47:05 +0000879 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000880 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000881 // Parse an abstract declarator.
882 DeclSpec declSpec(AttrFactory);
883 declSpec.setObjCQualifiers(&DS);
884 ParseSpecifierQualifierList(declSpec);
885 Declarator declarator(declSpec, context);
886 ParseDeclarator(declarator);
887
888 // If that's not invalid, extract a type.
889 if (!declarator.isInvalidType()) {
890 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
891 if (!type.isInvalid())
892 Ty = type.get();
893
894 // If we're parsing a parameter, steal all the decl attributes
895 // and add them to the decl spec.
896 if (context == Declarator::ObjCParameterContext)
897 takeDeclAttributes(*paramAttrs, declarator);
898 }
899 } else if (context == Declarator::ObjCResultContext &&
900 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000901 if (!Ident_instancetype)
902 Ident_instancetype = PP.getIdentifierInfo("instancetype");
903
904 if (Tok.getIdentifierInfo() == Ident_instancetype) {
905 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
906 ConsumeToken();
907 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000908 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000909
Steve Naroffd7333c22008-10-21 14:15:04 +0000910 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000911 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000912 else if (Tok.getLocation() == TypeStartLoc) {
913 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000914 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000915 SkipUntil(tok::r_paren);
916 } else {
917 // Otherwise, we found *something*, but didn't get a ')' in the right
918 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000919 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000920 }
Steve Narofff28b2642007-09-05 23:30:30 +0000921 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000922}
923
924/// objc-method-decl:
925/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000926/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000927/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000928/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000929///
930/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000931/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000932/// objc-keyword-selector objc-keyword-decl
933///
934/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000935/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
936/// objc-selector ':' objc-keyword-attributes[opt] identifier
937/// ':' objc-type-name objc-keyword-attributes[opt] identifier
938/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000939///
Steve Naroff4985ace2007-08-22 18:35:33 +0000940/// objc-parmlist:
941/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000942///
Steve Naroff4985ace2007-08-22 18:35:33 +0000943/// objc-parms:
944/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000945///
Steve Naroff4985ace2007-08-22 18:35:33 +0000946/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000947/// , ...
948///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000949/// objc-keyword-attributes: [OBJC2]
950/// __attribute__((unused))
951///
John McCalld226f652010-08-21 09:40:31 +0000952Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000953 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000954 tok::ObjCKeywordKind MethodImplKind,
955 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000956 ParsingDeclRAIIObject PD(*this);
957
Douglas Gregore8f5a172010-04-07 00:21:17 +0000958 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000959 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000960 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000961 cutOffParsing();
962 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000963 }
964
Chris Lattnere8904e92008-08-23 01:48:03 +0000965 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000966 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000967 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000968 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000969 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Ted Kremenek9e049352010-02-18 23:05:16 +0000971 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000972 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000973 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000974 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000975
Douglas Gregore8f5a172010-04-07 00:21:17 +0000976 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000977 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000978 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000979 cutOffParsing();
980 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000981 }
982
Ted Kremenek9e049352010-02-18 23:05:16 +0000983 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000984 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000985 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000986
Steve Naroff84c43102009-02-11 20:43:13 +0000987 // An unnamed colon is valid.
988 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000989 Diag(Tok, diag::err_expected_selector_for_method)
990 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000991 // Skip until we get a ; or {}.
992 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000993 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner5f9e2722011-07-23 10:55:15 +0000996 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000997 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000998 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000999 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001000 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattnerff384912007-10-07 02:00:24 +00001002 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001003 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001004 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001005 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001006 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001007 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001008 methodAttrs.getList(), MethodImplKind,
1009 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001010 PD.complete(Result);
1011 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001012 }
Steve Narofff28b2642007-09-05 23:30:30 +00001013
Chris Lattner5f9e2722011-07-23 10:55:15 +00001014 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001015 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001016 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001017 ParseScope PrototypeScope(this,
1018 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001019
1020 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001021
Chris Lattnerff384912007-10-07 02:00:24 +00001022 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001023 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001024 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattnerff384912007-10-07 02:00:24 +00001026 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001027 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001028 Diag(Tok, diag::err_expected_colon);
1029 break;
1030 }
1031 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001032
John McCallb3d87482010-08-24 05:47:05 +00001033 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001034 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001035 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1036 Declarator::ObjCParameterContext,
1037 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001038
Chris Lattnerff384912007-10-07 02:00:24 +00001039 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001040 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001041 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001042 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001043 MaybeParseGNUAttributes(paramAttrs);
1044 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001045 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001046
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001047 // Code completion for the next piece of the selector.
1048 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001049 KeyIdents.push_back(SelIdent);
1050 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1051 mType == tok::minus,
1052 /*AtParameterName=*/true,
1053 ReturnType,
1054 KeyIdents.data(),
1055 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001056 cutOffParsing();
1057 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001058 }
1059
Chris Lattnerdf195262007-10-09 17:51:17 +00001060 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001061 Diag(Tok, diag::err_expected_ident); // missing argument name.
1062 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattnere294d3f2009-04-11 18:57:04 +00001065 ArgInfo.Name = Tok.getIdentifierInfo();
1066 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001067 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Chris Lattnere294d3f2009-04-11 18:57:04 +00001069 ArgInfos.push_back(ArgInfo);
1070 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001071 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001072
John McCall0b7e6782011-03-24 11:26:52 +00001073 // Make sure the attributes persist.
1074 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1075
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001076 // Code completion for the next piece of the selector.
1077 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001078 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1079 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001080 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001081 ReturnType,
1082 KeyIdents.data(),
1083 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001084 cutOffParsing();
1085 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001086 }
1087
Chris Lattnerff384912007-10-07 02:00:24 +00001088 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001089 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001091 break;
1092 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Steve Naroff335eafa2007-11-15 12:35:21 +00001095 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattnerff384912007-10-07 02:00:24 +00001097 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001098 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001099 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001100 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001101 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001102 ConsumeToken();
1103 break;
1104 }
John McCall0b7e6782011-03-24 11:26:52 +00001105 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001106 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001107 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001108 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1109 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001110 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001111 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001112 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1113 ParmDecl.getIdentifierLoc(),
1114 Param,
1115 0));
1116
Chris Lattnerff384912007-10-07 02:00:24 +00001117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001119 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001120 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001121 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001122 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001123
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001124 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001125 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001126
Chris Lattnerff384912007-10-07 02:00:24 +00001127 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1128 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001129 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001130 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001131 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001132 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001133 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001134 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001135 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001136
John McCall54abf7d2009-11-04 02:18:39 +00001137 PD.complete(Result);
1138 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001139}
1140
Steve Naroffdac269b2007-08-20 21:31:48 +00001141/// objc-protocol-refs:
1142/// '<' identifier-list '>'
1143///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001144bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001145ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1146 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001147 bool WarnOnDeclarations,
1148 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001149 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001151 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattner5f9e2722011-07-23 10:55:15 +00001153 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Chris Lattnere13b9592008-07-26 04:03:38 +00001155 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001156 if (Tok.is(tok::code_completion)) {
1157 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1158 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001159 cutOffParsing();
1160 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001161 }
1162
Chris Lattnere13b9592008-07-26 04:03:38 +00001163 if (Tok.isNot(tok::identifier)) {
1164 Diag(Tok, diag::err_expected_ident);
1165 SkipUntil(tok::greater);
1166 return true;
1167 }
1168 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1169 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001170 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001171 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Chris Lattnere13b9592008-07-26 04:03:38 +00001173 if (Tok.isNot(tok::comma))
1174 break;
1175 ConsumeToken();
1176 }
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Chris Lattnere13b9592008-07-26 04:03:38 +00001178 // Consume the '>'.
1179 if (Tok.isNot(tok::greater)) {
1180 Diag(Tok, diag::err_expected_greater);
1181 return true;
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattnere13b9592008-07-26 04:03:38 +00001184 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattnere13b9592008-07-26 04:03:38 +00001186 // Convert the list of protocols identifiers into a list of protocol decls.
1187 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1188 &ProtocolIdents[0], ProtocolIdents.size(),
1189 Protocols);
1190 return false;
1191}
1192
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001193/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1194/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001195bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001196 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1197 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1198 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001199 SmallVector<Decl *, 8> ProtocolDecl;
1200 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001201 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1202 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001203 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1204 ProtocolLocs.data(), LAngleLoc);
1205 if (EndProtoLoc.isValid())
1206 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001207 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001208}
1209
1210
Steve Naroffdac269b2007-08-20 21:31:48 +00001211/// objc-class-instance-variables:
1212/// '{' objc-instance-variable-decl-list[opt] '}'
1213///
1214/// objc-instance-variable-decl-list:
1215/// objc-visibility-spec
1216/// objc-instance-variable-decl ';'
1217/// ';'
1218/// objc-instance-variable-decl-list objc-visibility-spec
1219/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1220/// objc-instance-variable-decl-list ';'
1221///
1222/// objc-visibility-spec:
1223/// @private
1224/// @protected
1225/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001226/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001227///
1228/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001229/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001230///
John McCalld226f652010-08-21 09:40:31 +00001231void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001232 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001233 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001234 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001235 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001236
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001237 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001238 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001239
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001240 BalancedDelimiterTracker T(*this, tok::l_brace);
1241 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Steve Naroffddbff782007-08-21 21:17:12 +00001243 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001244 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001245 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Steve Naroffddbff782007-08-21 21:17:12 +00001247 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001248 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001249 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001250 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001251 ConsumeToken();
1252 continue;
1253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Steve Naroffddbff782007-08-21 21:17:12 +00001255 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001256 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001257 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001258
1259 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001260 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001261 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001262 }
1263
Steve Naroff861cf3e2007-08-23 18:16:40 +00001264 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001265 case tok::objc_private:
1266 case tok::objc_public:
1267 case tok::objc_protected:
1268 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001269 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001270 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001271 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001272 default:
1273 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001274 continue;
1275 }
1276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001278 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001279 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001280 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001281 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001282 }
1283
John McCallbdd563e2009-11-03 02:38:08 +00001284 struct ObjCIvarCallback : FieldCallback {
1285 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001286 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001287 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001288 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001289
John McCalld226f652010-08-21 09:40:31 +00001290 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001291 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001292 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1293 }
1294
John McCalld226f652010-08-21 09:40:31 +00001295 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001296 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001297 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001298 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001299 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001300 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001301 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001302 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001303 if (Field)
1304 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001305 return Field;
1306 }
1307 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001308
Chris Lattnere1359422008-04-10 06:46:29 +00001309 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001310 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001311 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Chris Lattnerdf195262007-10-09 17:51:17 +00001313 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001314 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001315 } else {
1316 Diag(Tok, diag::err_expected_semi_decl_list);
1317 // Skip to end of block or statement
1318 SkipUntil(tok::r_brace, true, true);
1319 }
1320 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001321 T.consumeClose();
1322
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001323 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001324 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001325 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001326 // Call ActOnFields() even if we don't have any decls. This is useful
1327 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001329 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001330 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001331 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
Steve Naroffdac269b2007-08-20 21:31:48 +00001333
1334/// objc-protocol-declaration:
1335/// objc-protocol-definition
1336/// objc-protocol-forward-reference
1337///
1338/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001339/// @protocol identifier
1340/// objc-protocol-refs[opt]
1341/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001342/// @end
1343///
1344/// objc-protocol-forward-reference:
1345/// @protocol identifier-list ';'
1346///
1347/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001348/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001349/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001350Parser::DeclGroupPtrTy
1351Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1352 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001353 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001354 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1355 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Douglas Gregor083128f2009-11-18 04:49:41 +00001357 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001358 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001359 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001360 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001361 }
1362
Chris Lattnerdf195262007-10-09 17:51:17 +00001363 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001364 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001365 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001366 }
1367 // Save the protocol name, then consume it.
1368 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1369 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Chris Lattnerdf195262007-10-09 17:51:17 +00001371 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001372 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001373 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001374 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001375 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001378 CheckNestedObjCContexts(AtLoc);
1379
Chris Lattnerdf195262007-10-09 17:51:17 +00001380 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001381 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001382 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1383
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001384 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001385 while (1) {
1386 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001387 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001388 Diag(Tok, diag::err_expected_ident);
1389 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001390 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001391 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001392 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1393 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001394 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Chris Lattnerdf195262007-10-09 17:51:17 +00001396 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001397 break;
1398 }
1399 // Consume the ';'.
1400 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001401 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Steve Naroffe440eb82007-10-10 17:32:04 +00001403 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001404 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001405 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001406 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001409 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001410 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001411
Chris Lattner5f9e2722011-07-23 10:55:15 +00001412 SmallVector<Decl *, 8> ProtocolRefs;
1413 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001414 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001415 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1416 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001417 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001418
John McCalld226f652010-08-21 09:40:31 +00001419 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001420 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001421 ProtocolRefs.data(),
1422 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001423 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001424 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001425
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001426 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001427 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001428}
Steve Naroffdac269b2007-08-20 21:31:48 +00001429
1430/// objc-implementation:
1431/// objc-class-implementation-prologue
1432/// objc-category-implementation-prologue
1433///
1434/// objc-class-implementation-prologue:
1435/// @implementation identifier objc-superclass[opt]
1436/// objc-class-instance-variables[opt]
1437///
1438/// objc-category-implementation-prologue:
1439/// @implementation identifier ( identifier )
Erik Verbruggend64251f2011-12-06 09:25:23 +00001440Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001441 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1442 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001443 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001444 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001446 // Code completion after '@implementation'.
1447 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001448 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001449 cutOffParsing();
1450 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001451 }
1452
Chris Lattnerdf195262007-10-09 17:51:17 +00001453 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001454 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001455 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001456 }
1457 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001458 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001459 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001460
1461 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001462 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001463 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001464 SourceLocation categoryLoc, rparenLoc;
1465 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001467 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001468 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001469 cutOffParsing();
1470 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001471 }
1472
Chris Lattnerdf195262007-10-09 17:51:17 +00001473 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001474 categoryId = Tok.getIdentifierInfo();
1475 categoryLoc = ConsumeToken();
1476 } else {
1477 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001478 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001479 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001480 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001481 Diag(Tok, diag::err_expected_rparen);
1482 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001483 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001484 }
1485 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001486 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001487 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001488 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001489
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001490 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001491 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001492 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001493 }
1494 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001495 SourceLocation superClassLoc;
1496 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001497 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001498 // We have a super class
1499 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001500 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001501 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001502 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001503 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001504 superClassId = Tok.getIdentifierInfo();
1505 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001506 }
John McCalld226f652010-08-21 09:40:31 +00001507 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001508 AtLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001509 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Steve Naroff60fccee2007-10-29 21:38:07 +00001511 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggend64251f2011-12-06 09:25:23 +00001512 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001513
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001515 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001516 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001517}
Steve Naroff60fccee2007-10-29 21:38:07 +00001518
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001519Parser::DeclGroupPtrTy
1520Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001521 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1522 "ParseObjCAtEndDeclaration(): Expected @end");
1523 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001524 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001525 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001526 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1527 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001528 if (D)
1529 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001530 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001531 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001532
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001533 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001534 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001535 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001536 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001537 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001538 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001539 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001540
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001541 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001542 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001543 return Actions.BuildDeclaratorGroup(
1544 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001545}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001546
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001547Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1548 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001549 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001550 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggend64251f2011-12-06 09:25:23 +00001551
John McCalld226f652010-08-21 09:40:31 +00001552 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggend64251f2011-12-06 09:25:23 +00001553 Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1554 Diag(Tok, diag::err_objc_missing_end)
1555 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1556 if (ImpDecl)
1557 Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1558 << Sema::OCK_Implementation;
1559
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001560 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1561}
1562
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001563void Parser::clearLateParsedObjCMethods() {
1564 for (LateParsedObjCMethodContainer::iterator
1565 I = LateParsedObjCMethods.begin(),
1566 E = LateParsedObjCMethods.end(); I != E; ++I)
1567 delete *I;
1568 LateParsedObjCMethods.clear();
1569}
1570
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001571/// compatibility-alias-decl:
1572/// @compatibility_alias alias-name class-name ';'
1573///
John McCalld226f652010-08-21 09:40:31 +00001574Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001575 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1576 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1577 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001578 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001579 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001580 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001581 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001582 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1583 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001584 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001585 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001586 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001587 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001588 IdentifierInfo *classId = Tok.getIdentifierInfo();
1589 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001590 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1591 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001592 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1593 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001594}
1595
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001596/// property-synthesis:
1597/// @synthesize property-ivar-list ';'
1598///
1599/// property-ivar-list:
1600/// property-ivar
1601/// property-ivar-list ',' property-ivar
1602///
1603/// property-ivar:
1604/// identifier
1605/// identifier '=' identifier
1606///
John McCalld226f652010-08-21 09:40:31 +00001607Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001608 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1609 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001610 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Douglas Gregorb328c422009-11-18 19:45:45 +00001612 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001613 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001614 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001615 cutOffParsing();
1616 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001617 }
1618
Douglas Gregorb328c422009-11-18 19:45:45 +00001619 if (Tok.isNot(tok::identifier)) {
1620 Diag(Tok, diag::err_synthesized_property_name);
1621 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001622 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001623 }
1624
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001625 IdentifierInfo *propertyIvar = 0;
1626 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1627 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001628 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001629 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001630 // property '=' ivar-name
1631 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001632
1633 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001634 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001635 cutOffParsing();
1636 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001637 }
1638
Chris Lattnerdf195262007-10-09 17:51:17 +00001639 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001640 Diag(Tok, diag::err_expected_ident);
1641 break;
1642 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001643 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001644 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001645 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001646 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001647 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001648 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001649 break;
1650 ConsumeToken(); // consume ','
1651 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001652 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001653 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001654}
1655
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001656/// property-dynamic:
1657/// @dynamic property-list
1658///
1659/// property-list:
1660/// identifier
1661/// property-list ',' identifier
1662///
John McCalld226f652010-08-21 09:40:31 +00001663Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001664 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1665 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001666 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001667 while (true) {
1668 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001669 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001670 cutOffParsing();
1671 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001672 }
1673
1674 if (Tok.isNot(tok::identifier)) {
1675 Diag(Tok, diag::err_expected_ident);
1676 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001677 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001678 }
1679
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001680 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1681 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001682 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001683 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001684
Chris Lattnerdf195262007-10-09 17:51:17 +00001685 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001686 break;
1687 ConsumeToken(); // consume ','
1688 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001689 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001690 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001691}
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001693/// objc-throw-statement:
1694/// throw expression[opt];
1695///
John McCall60d7b3a2010-08-24 06:29:42 +00001696StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1697 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001698 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001699 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001700 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001701 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001702 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001703 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001704 }
1705 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001706 // consume ';'
1707 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001708 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001709}
1710
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001711/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001712/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001713///
John McCall60d7b3a2010-08-24 06:29:42 +00001714StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001715Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001716 ConsumeToken(); // consume synchronized
1717 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001718 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001719 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001720 }
John McCall07524032011-07-27 21:50:02 +00001721
1722 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001723 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001724 ExprResult operand(ParseExpression());
1725
1726 if (Tok.is(tok::r_paren)) {
1727 ConsumeParen(); // ')'
1728 } else {
1729 if (!operand.isInvalid())
1730 Diag(Tok, diag::err_expected_rparen);
1731
1732 // Skip forward until we see a left brace, but don't consume it.
1733 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001734 }
John McCall07524032011-07-27 21:50:02 +00001735
1736 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001737 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001738 if (!operand.isInvalid())
1739 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001740 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001741 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001742
John McCall07524032011-07-27 21:50:02 +00001743 // Check the @synchronized operand now.
1744 if (!operand.isInvalid())
1745 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001746
John McCall07524032011-07-27 21:50:02 +00001747 // Parse the compound statement within a new scope.
1748 ParseScope bodyScope(this, Scope::DeclScope);
1749 StmtResult body(ParseCompoundStatementBody());
1750 bodyScope.Exit();
1751
1752 // If there was a semantic or parse error earlier with the
1753 // operand, fail now.
1754 if (operand.isInvalid())
1755 return StmtError();
1756
1757 if (body.isInvalid())
1758 body = Actions.ActOnNullStmt(Tok.getLocation());
1759
1760 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001761}
1762
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001763/// objc-try-catch-statement:
1764/// @try compound-statement objc-catch-list[opt]
1765/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1766///
1767/// objc-catch-list:
1768/// @catch ( parameter-declaration ) compound-statement
1769/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1770/// catch-parameter-declaration:
1771/// parameter-declaration
1772/// '...' [OBJC2]
1773///
John McCall60d7b3a2010-08-24 06:29:42 +00001774StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001775 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001776
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001777 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001778 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001779 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001780 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001781 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001782 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001783 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001784 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001785 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001786 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001787 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001788 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001789
Chris Lattnerdf195262007-10-09 17:51:17 +00001790 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001791 // At this point, we need to lookahead to determine if this @ is the start
1792 // of an @catch or @finally. We don't want to consume the @ token if this
1793 // is an @try or @encode or something else.
1794 Token AfterAt = GetLookAheadToken(1);
1795 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1796 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1797 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001798
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001799 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001800 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001801 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001802 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001803 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001804 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001805 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001806 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001807 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001808 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001809 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001810 ParseDeclarator(ParmDecl);
1811
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001812 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001813 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001814 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001815 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001816 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Steve Naroff93a25952009-04-07 22:56:58 +00001818 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Steve Naroff93a25952009-04-07 22:56:58 +00001820 if (Tok.is(tok::r_paren))
1821 RParenLoc = ConsumeParen();
1822 else // Skip over garbage, until we get to ')'. Eat the ')'.
1823 SkipUntil(tok::r_paren, true, false);
1824
John McCall60d7b3a2010-08-24 06:29:42 +00001825 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001826 if (Tok.is(tok::l_brace))
1827 CatchBody = ParseCompoundStatementBody();
1828 else
1829 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001830 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001831 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001832
John McCall60d7b3a2010-08-24 06:29:42 +00001833 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001834 RParenLoc,
1835 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001836 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001837 if (!Catch.isInvalid())
1838 CatchStmts.push_back(Catch.release());
1839
Steve Naroff64515f32008-02-05 21:27:35 +00001840 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001841 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1842 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001843 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001844 }
1845 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001846 } else {
1847 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001848 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001849 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001850
John McCall60d7b3a2010-08-24 06:29:42 +00001851 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001852 if (Tok.is(tok::l_brace))
1853 FinallyBody = ParseCompoundStatementBody();
1854 else
1855 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001856 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001857 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001858 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001859 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001860 catch_or_finally_seen = true;
1861 break;
1862 }
1863 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001864 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001865 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001866 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001867 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001868
John McCall9ae2f072010-08-23 23:25:46 +00001869 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001870 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001871 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001872}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001873
John McCallf85e1932011-06-15 23:02:42 +00001874/// objc-autoreleasepool-statement:
1875/// @autoreleasepool compound-statement
1876///
1877StmtResult
1878Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1879 ConsumeToken(); // consume autoreleasepool
1880 if (Tok.isNot(tok::l_brace)) {
1881 Diag(Tok, diag::err_expected_lbrace);
1882 return StmtError();
1883 }
1884 // Enter a scope to hold everything within the compound stmt. Compound
1885 // statements can always hold declarations.
1886 ParseScope BodyScope(this, Scope::DeclScope);
1887
1888 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1889
1890 BodyScope.Exit();
1891 if (AutoreleasePoolBody.isInvalid())
1892 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1893 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1894 AutoreleasePoolBody.take());
1895}
1896
Steve Naroff3536b442007-09-06 21:24:23 +00001897/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001898///
John McCalld226f652010-08-21 09:40:31 +00001899Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001900 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001901
John McCallf312b1e2010-08-26 23:41:50 +00001902 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1903 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001905 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001906 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001907 if (ObjCImpDecl) {
1908 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001909 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001910 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001911 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001912 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001913
Steve Naroff409be832007-11-11 19:54:21 +00001914 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001915 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001916 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Steve Naroff409be832007-11-11 19:54:21 +00001918 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1919 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Steve Naroff409be832007-11-11 19:54:21 +00001921 // If we didn't find the '{', bail out.
1922 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001923 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001924 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001925 // Allow the rest of sema to find private method decl implementations.
1926 if (MDecl)
1927 Actions.AddAnyMethodToGlobalPool(MDecl);
1928
1929 // Consume the tokens and store them for later parsing.
1930 LexedMethod* LM = new LexedMethod(this, MDecl);
1931 LateParsedObjCMethods.push_back(LM);
1932 CachedTokens &Toks = LM->Toks;
1933 // Begin by storing the '{' token.
1934 Toks.push_back(Tok);
1935 ConsumeBrace();
1936 // Consume everything up to (and including) the matching right brace.
1937 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001938 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001939}
Anders Carlsson55085182007-08-21 17:43:55 +00001940
John McCall60d7b3a2010-08-24 06:29:42 +00001941StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001942 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001943 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001944 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001945 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001946 }
1947
1948 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001949 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001950
1951 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001952 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001953
1954 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001955 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001956
1957 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1958 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001959
John McCall60d7b3a2010-08-24 06:29:42 +00001960 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001961 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001962 // If the expression is invalid, skip ahead to the next semicolon. Not
1963 // doing this opens us up to the possibility of infinite loops if
1964 // ParseExpression does not consume any tokens.
1965 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001966 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001967 }
Chris Lattner5d803162009-12-07 16:33:19 +00001968
Steve Naroff64515f32008-02-05 21:27:35 +00001969 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001970 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001971 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001972}
1973
John McCall60d7b3a2010-08-24 06:29:42 +00001974ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001975 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001976 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001977 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001978 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001979 return ExprError();
1980
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001981 case tok::string_literal: // primary-expression: string-literal
1982 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001983 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001984 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001985 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001986 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001987
Chris Lattner4fef81d2008-08-05 06:19:09 +00001988 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1989 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001990 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001991 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001992 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001993 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001994 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001995 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001996 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001997 }
Anders Carlsson55085182007-08-21 17:43:55 +00001998 }
Anders Carlsson55085182007-08-21 17:43:55 +00001999}
2000
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002001/// \brirg Parse the receiver of an Objective-C++ message send.
2002///
2003/// This routine parses the receiver of a message send in
2004/// Objective-C++ either as a type or as an expression. Note that this
2005/// routine must not be called to parse a send to 'super', since it
2006/// has no way to return such a result.
2007///
2008/// \param IsExpr Whether the receiver was parsed as an expression.
2009///
2010/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2011/// IsExpr is true), the parsed expression. If the receiver was parsed
2012/// as a type (\c IsExpr is false), the parsed type.
2013///
2014/// \returns True if an error occurred during parsing or semantic
2015/// analysis, in which case the arguments do not have valid
2016/// values. Otherwise, returns false for a successful parse.
2017///
2018/// objc-receiver: [C++]
2019/// 'super' [not parsed here]
2020/// expression
2021/// simple-type-specifier
2022/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002023bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002024 InMessageExpressionRAIIObject InMessage(*this, true);
2025
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002026 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2027 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2028 TryAnnotateTypeOrScopeToken();
2029
2030 if (!isCXXSimpleTypeSpecifier()) {
2031 // objc-receiver:
2032 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002033 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002034 if (Receiver.isInvalid())
2035 return true;
2036
2037 IsExpr = true;
2038 TypeOrExpr = Receiver.take();
2039 return false;
2040 }
2041
2042 // objc-receiver:
2043 // typename-specifier
2044 // simple-type-specifier
2045 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002046 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002047 ParseCXXSimpleTypeSpecifier(DS);
2048
2049 if (Tok.is(tok::l_paren)) {
2050 // If we see an opening parentheses at this point, we are
2051 // actually parsing an expression that starts with a
2052 // function-style cast, e.g.,
2053 //
2054 // postfix-expression:
2055 // simple-type-specifier ( expression-list [opt] )
2056 // typename-specifier ( expression-list [opt] )
2057 //
2058 // Parse the remainder of this case, then the (optional)
2059 // postfix-expression suffix, followed by the (optional)
2060 // right-hand side of the binary expression. We have an
2061 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002062 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002063 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002064 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002065 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002066 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002067 if (Receiver.isInvalid())
2068 return true;
2069
2070 IsExpr = true;
2071 TypeOrExpr = Receiver.take();
2072 return false;
2073 }
2074
2075 // We have a class message. Turn the simple-type-specifier or
2076 // typename-specifier we parsed into a type and parse the
2077 // remainder of the class message.
2078 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002079 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002080 if (Type.isInvalid())
2081 return true;
2082
2083 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002084 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002085 return false;
2086}
2087
Douglas Gregor1b730e82010-05-31 14:40:22 +00002088/// \brief Determine whether the parser is currently referring to a an
2089/// Objective-C message send, using a simplified heuristic to avoid overhead.
2090///
2091/// This routine will only return true for a subset of valid message-send
2092/// expressions.
2093bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002094 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002095 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002096 return GetLookAheadToken(1).is(tok::identifier) &&
2097 GetLookAheadToken(2).is(tok::identifier);
2098}
2099
Douglas Gregor9497a732010-09-16 01:51:54 +00002100bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2101 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2102 InMessageExpression)
2103 return false;
2104
2105
2106 ParsedType Type;
2107
2108 if (Tok.is(tok::annot_typename))
2109 Type = getTypeAnnotation(Tok);
2110 else if (Tok.is(tok::identifier))
2111 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2112 getCurScope());
2113 else
2114 return false;
2115
2116 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2117 const Token &AfterNext = GetLookAheadToken(2);
2118 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2119 if (Tok.is(tok::identifier))
2120 TryAnnotateTypeOrScopeToken();
2121
2122 return Tok.is(tok::annot_typename);
2123 }
2124 }
2125
2126 return false;
2127}
2128
Mike Stump1eb44332009-09-09 15:08:12 +00002129/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002130/// '[' objc-receiver objc-message-args ']'
2131///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002132/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002133/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002134/// expression
2135/// class-name
2136/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002137///
John McCall60d7b3a2010-08-24 06:29:42 +00002138ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002139 assert(Tok.is(tok::l_square) && "'[' expected");
2140 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2141
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002142 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002143 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002144 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002145 return ExprError();
2146 }
2147
Douglas Gregor0fbda682010-09-15 14:51:05 +00002148 InMessageExpressionRAIIObject InMessage(*this, true);
2149
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002150 if (getLang().CPlusPlus) {
2151 // We completely separate the C and C++ cases because C++ requires
2152 // more complicated (read: slower) parsing.
2153
2154 // Handle send to super.
2155 // FIXME: This doesn't benefit from the same typo-correction we
2156 // get in Objective-C.
2157 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002158 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002159 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2160 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002161
2162 // Parse the receiver, which is either a type or an expression.
2163 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002164 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002165 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2166 SkipUntil(tok::r_square);
2167 return ExprError();
2168 }
2169
2170 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002171 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2172 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002173 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002174
2175 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002176 ParsedType::getFromOpaquePtr(TypeOrExpr),
2177 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002178 }
2179
2180 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002181 IdentifierInfo *Name = Tok.getIdentifierInfo();
2182 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002183 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002184 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002185 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002186 NextToken().is(tok::period),
2187 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002188 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002189 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2190 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002191
John McCallf312b1e2010-08-26 23:41:50 +00002192 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002193 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002194 SkipUntil(tok::r_square);
2195 return ExprError();
2196 }
2197
Douglas Gregor1569f952010-04-21 20:38:13 +00002198 ConsumeToken(); // the type name
2199
2200 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002201 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002202
John McCallf312b1e2010-08-26 23:41:50 +00002203 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002204 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002205 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002206 }
Chris Lattner699b6612008-01-25 18:59:06 +00002207 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002208
2209 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002210 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002211 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002212 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002213 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002214 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002215
John McCallb3d87482010-08-24 05:47:05 +00002216 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2217 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002218}
Sebastian Redl1d922962008-12-13 15:32:12 +00002219
Douglas Gregor2725ca82010-04-21 19:57:20 +00002220/// \brief Parse the remainder of an Objective-C message following the
2221/// '[' objc-receiver.
2222///
2223/// This routine handles sends to super, class messages (sent to a
2224/// class name), and instance messages (sent to an object), and the
2225/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2226/// ReceiverExpr, respectively. Only one of these parameters may have
2227/// a valid value.
2228///
2229/// \param LBracLoc The location of the opening '['.
2230///
2231/// \param SuperLoc If this is a send to 'super', the location of the
2232/// 'super' keyword that indicates a send to the superclass.
2233///
2234/// \param ReceiverType If this is a class message, the type of the
2235/// class we are sending a message to.
2236///
2237/// \param ReceiverExpr If this is an instance message, the expression
2238/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002239///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002240/// objc-message-args:
2241/// objc-selector
2242/// objc-keywordarg-list
2243///
2244/// objc-keywordarg-list:
2245/// objc-keywordarg
2246/// objc-keywordarg-list objc-keywordarg
2247///
Mike Stump1eb44332009-09-09 15:08:12 +00002248/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002249/// selector-name[opt] ':' objc-keywordexpr
2250///
2251/// objc-keywordexpr:
2252/// nonempty-expr-list
2253///
2254/// nonempty-expr-list:
2255/// assignment-expression
2256/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002257///
John McCall60d7b3a2010-08-24 06:29:42 +00002258ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002259Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002260 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002261 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002262 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002263 InMessageExpressionRAIIObject InMessage(*this, true);
2264
Steve Naroffc4df6d22009-11-07 02:08:14 +00002265 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002266 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002267 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2268 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002269 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002270 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2271 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002272 else
John McCall9ae2f072010-08-23 23:25:46 +00002273 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002274 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002275 cutOffParsing();
2276 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002277 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002278
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002279 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002280 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002281 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002282
Chris Lattner5f9e2722011-07-23 10:55:15 +00002283 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002284 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002285 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002286
Chris Lattnerdf195262007-10-09 17:51:17 +00002287 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002288 while (1) {
2289 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002290 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002291 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002292
Chris Lattnerdf195262007-10-09 17:51:17 +00002293 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002294 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002295 // We must manually skip to a ']', otherwise the expression skipper will
2296 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2297 // the enclosing expression.
2298 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002299 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002300 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002301
Steve Naroff68d331a2007-09-27 14:38:14 +00002302 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002303 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002304
2305 if (Tok.is(tok::code_completion)) {
2306 if (SuperLoc.isValid())
2307 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2308 KeyIdents.data(),
2309 KeyIdents.size(),
2310 /*AtArgumentEpression=*/true);
2311 else if (ReceiverType)
2312 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2313 KeyIdents.data(),
2314 KeyIdents.size(),
2315 /*AtArgumentEpression=*/true);
2316 else
2317 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2318 KeyIdents.data(),
2319 KeyIdents.size(),
2320 /*AtArgumentEpression=*/true);
2321
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002322 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002323 return ExprError();
2324 }
2325
John McCall60d7b3a2010-08-24 06:29:42 +00002326 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002327 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002328 // We must manually skip to a ']', otherwise the expression skipper will
2329 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2330 // the enclosing expression.
2331 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002332 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002333 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002334
Steve Naroff37387c92007-09-17 20:25:27 +00002335 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002336 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002337
Douglas Gregord3c68542009-11-19 01:08:35 +00002338 // Code completion after each argument.
2339 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002340 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002341 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002342 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002343 KeyIdents.size(),
2344 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002345 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002346 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002347 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002348 KeyIdents.size(),
2349 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002350 else
John McCall9ae2f072010-08-23 23:25:46 +00002351 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002352 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002353 KeyIdents.size(),
2354 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002355 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002356 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002357 }
2358
Steve Naroff37387c92007-09-17 20:25:27 +00002359 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002360 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002361 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002362 break;
2363 // We have a selector or a colon, continue parsing.
2364 }
2365 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002366 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002367 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002368 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002369 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002370 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002371 // We must manually skip to a ']', otherwise the expression skipper will
2372 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2373 // the enclosing expression.
2374 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002375 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002376 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002377
Steve Naroff49f109c2007-11-15 13:05:42 +00002378 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002379 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002380 }
2381 } else if (!selIdent) {
2382 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002383
Chris Lattner4fef81d2008-08-05 06:19:09 +00002384 // We must manually skip to a ']', otherwise the expression skipper will
2385 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2386 // the enclosing expression.
2387 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002388 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002389 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002390
Chris Lattnerdf195262007-10-09 17:51:17 +00002391 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002392 if (Tok.is(tok::identifier))
2393 Diag(Tok, diag::err_expected_colon);
2394 else
2395 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002396 // We must manually skip to a ']', otherwise the expression skipper will
2397 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2398 // the enclosing expression.
2399 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002400 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002401 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002402
Chris Lattner699b6612008-01-25 18:59:06 +00002403 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002404
Steve Naroff29238a02007-10-05 18:42:47 +00002405 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002406 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002407 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002408 KeyLocs.push_back(Loc);
2409 }
Chris Lattnerff384912007-10-07 02:00:24 +00002410 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002411
Douglas Gregor2725ca82010-04-21 19:57:20 +00002412 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002413 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002414 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002415 MultiExprArg(Actions,
2416 KeyExprs.take(),
2417 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002418 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002419 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002420 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002421 MultiExprArg(Actions,
2422 KeyExprs.take(),
2423 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002424 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002425 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002426 MultiExprArg(Actions,
2427 KeyExprs.take(),
2428 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002429}
2430
John McCall60d7b3a2010-08-24 06:29:42 +00002431ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2432 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002433 if (Res.isInvalid()) return move(Res);
2434
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002435 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2436 // expressions. At this point, we know that the only valid thing that starts
2437 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002438 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002439 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002440 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002441 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002442
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002443 while (Tok.is(tok::at)) {
2444 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002445
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002446 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002447 if (!isTokenStringLiteral())
2448 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002449
John McCall60d7b3a2010-08-24 06:29:42 +00002450 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002451 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002452 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002453
Sebastian Redleffa8d12008-12-10 00:02:53 +00002454 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002455 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002456
2457 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2458 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002459}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002460
2461/// objc-encode-expression:
2462/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002463ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002464Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002465 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002466
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002467 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002468
Chris Lattner4fef81d2008-08-05 06:19:09 +00002469 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002470 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2471
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002472 BalancedDelimiterTracker T(*this, tok::l_paren);
2473 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002474
Douglas Gregor809070a2009-02-18 17:45:20 +00002475 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002476
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002477 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002478
Douglas Gregor809070a2009-02-18 17:45:20 +00002479 if (Ty.isInvalid())
2480 return ExprError();
2481
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002482 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2483 T.getOpenLocation(), Ty.get(),
2484 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002485}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002486
2487/// objc-protocol-expression
2488/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002489ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002490Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002491 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002492
Chris Lattner4fef81d2008-08-05 06:19:09 +00002493 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002494 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2495
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002496 BalancedDelimiterTracker T(*this, tok::l_paren);
2497 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002498
Chris Lattner4fef81d2008-08-05 06:19:09 +00002499 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002500 return ExprError(Diag(Tok, diag::err_expected_ident));
2501
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002502 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002503 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002504
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002505 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002506
Sebastian Redl1d922962008-12-13 15:32:12 +00002507 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002508 T.getOpenLocation(),
2509 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002510}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002511
2512/// objc-selector-expression
2513/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002514ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002515 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002516
Chris Lattner4fef81d2008-08-05 06:19:09 +00002517 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002518 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2519
Chris Lattner5f9e2722011-07-23 10:55:15 +00002520 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002521 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002522
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002523 BalancedDelimiterTracker T(*this, tok::l_paren);
2524 T.consumeOpen();
2525
Douglas Gregor458433d2010-08-26 15:07:07 +00002526 if (Tok.is(tok::code_completion)) {
2527 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2528 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002529 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002530 return ExprError();
2531 }
2532
Chris Lattner2fc5c242009-04-11 18:13:45 +00002533 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002534 if (!SelIdent && // missing selector name.
2535 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002536 return ExprError(Diag(Tok, diag::err_expected_ident));
2537
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002538 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002539 unsigned nColons = 0;
2540 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002541 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002542 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2543 ++nColons;
2544 KeyIdents.push_back(0);
2545 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002546 return ExprError(Diag(Tok, diag::err_expected_colon));
2547
Chris Lattner5add7542010-08-27 22:32:41 +00002548 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002549 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002550 if (Tok.is(tok::r_paren))
2551 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002552
2553 if (Tok.is(tok::code_completion)) {
2554 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2555 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002556 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002557 return ExprError();
2558 }
2559
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002560 // Check for another keyword selector.
2561 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002562 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002563 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002564 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002565 break;
2566 }
Steve Naroff887407e2007-12-05 22:21:29 +00002567 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002568 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002569 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002570 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002571 T.getOpenLocation(),
2572 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002573 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002574
2575Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002576
2577 // Save the current token position.
2578 SourceLocation OrigLoc = Tok.getLocation();
2579
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002580 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2581 // Append the current token at the end of the new token stream so that it
2582 // doesn't get lost.
2583 LM.Toks.push_back(Tok);
2584 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2585
2586 // MDecl might be null due to error in method prototype, etc.
2587 Decl *MDecl = LM.D;
2588 // Consume the previously pushed token.
2589 ConsumeAnyToken();
2590
2591 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2592 SourceLocation BraceLoc = Tok.getLocation();
2593 // Enter a scope for the method body.
2594 ParseScope BodyScope(this,
2595 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2596
2597 // Tell the actions module that we have entered a method definition with the
2598 // specified Declarator for the method.
2599 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2600
2601 if (PP.isCodeCompletionEnabled()) {
2602 if (trySkippingFunctionBodyForCodeCompletion()) {
2603 BodyScope.Exit();
2604 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2605 }
2606 }
2607
2608 StmtResult FnBody(ParseCompoundStatementBody());
2609
2610 // If the function body could not be parsed, make a bogus compoundstmt.
2611 if (FnBody.isInvalid())
2612 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2613 MultiStmtArg(Actions), false);
2614
2615 // Leave the function body scope.
2616 BodyScope.Exit();
2617
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002618 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2619
2620 if (Tok.getLocation() != OrigLoc) {
2621 // Due to parsing error, we either went over the cached tokens or
2622 // there are still cached tokens left. If it's the latter case skip the
2623 // leftover tokens.
2624 // Since this is an uncommon situation that should be avoided, use the
2625 // expensive isBeforeInTranslationUnit call.
2626 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2627 OrigLoc))
2628 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2629 ConsumeAnyToken();
2630 }
2631
2632 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002633}