blob: a2994a3312c08d26a13b387f15330221ac635f42 [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;
Douglas Gregor5948ae12012-01-03 18:04:46 +000069 case tok::objc_import:
70 return ParseModuleImport(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000071 default:
72 Diag(AtLoc, diag::err_unexpected_at);
73 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000074 SingleDecl = 0;
75 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000077 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000078}
79
80///
Mike Stump1eb44332009-09-09 15:08:12 +000081/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000084Parser::DeclGroupPtrTy
85Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 SmallVector<IdentifierInfo *, 8> ClassNames;
88 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000089
Mike Stump1eb44332009-09-09 15:08:12 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000092 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000093 Diag(Tok, diag::err_expected_ident);
94 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000098 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattnerdf195262007-10-09 17:51:17 +0000101 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 ConsumeToken();
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // Consume the ';'.
108 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000109 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekc09cba62009-11-17 23:12:20 +0000111 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
112 ClassLocs.data(),
113 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
Erik Verbruggend64251f2011-12-06 09:25:23 +0000116void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
117{
118 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
119 if (ock == Sema::OCK_None)
120 return;
121
122 Decl *Decl = Actions.ActOnAtEnd(getCurScope(), AtLoc);
123 Diag(AtLoc, diag::err_objc_missing_end)
124 << FixItHint::CreateInsertion(AtLoc, "@end\n");
125 if (Decl)
126 Diag(Decl->getLocStart(), diag::note_objc_container_start)
127 << (int) ock;
128 if (!PendingObjCImpDecl.empty())
129 PendingObjCImpDecl.pop_back();
130 ObjCImpDecl = 0;
131}
132
Steve Naroffdac269b2007-08-20 21:31:48 +0000133///
134/// objc-interface:
135/// objc-class-interface-attributes[opt] objc-class-interface
136/// objc-category-interface
137///
138/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000139/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000140/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000141/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000142/// objc-interface-decl-list
143/// @end
144///
145/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000146/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000147/// objc-protocol-refs[opt]
148/// objc-interface-decl-list
149/// @end
150///
151/// objc-superclass:
152/// ':' identifier
153///
154/// objc-class-interface-attributes:
155/// __attribute__((visibility("default")))
156/// __attribute__((visibility("hidden")))
157/// __attribute__((deprecated))
158/// __attribute__((unavailable))
159/// __attribute__((objc_exception)) - used by NSException on 64-bit
160///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000161Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000162 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000163 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000165 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000166 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000168 // Code completion after '@interface'.
169 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000170 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000171 cutOffParsing();
172 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000173 }
174
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000177 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000179
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000181 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000183 if (Tok.is(tok::l_paren) &&
184 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000185
186 BalancedDelimiterTracker T(*this, tok::l_paren);
187 T.consumeOpen();
188
189 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000190 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000191 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000192 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000193 cutOffParsing();
194 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000195 }
196
Steve Naroff527fe232007-08-23 19:56:30 +0000197 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000198 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000199 categoryId = Tok.getIdentifierInfo();
200 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000201 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000202 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000203 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000204 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000205 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000206
207 T.consumeClose();
208 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000209 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000210
211 if (!attrs.empty()) { // categories don't support attributes.
212 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
213 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000214 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000215
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000216 // Next, we need to check for any protocol references.
217 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000218 SmallVector<Decl *, 8> ProtocolRefs;
219 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000220 if (Tok.is(tok::less) &&
221 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000222 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000223 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
John McCalld226f652010-08-21 09:40:31 +0000225 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000226 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000227 nameId, nameLoc,
228 categoryId, categoryLoc,
229 ProtocolRefs.data(),
230 ProtocolRefs.size(),
231 ProtocolLocs.data(),
232 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000233
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000234 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000235 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000236
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000237 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000238 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000239 }
240 // Parse a class interface.
241 IdentifierInfo *superClassId = 0;
242 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000243
Chris Lattnerdf195262007-10-09 17:51:17 +0000244 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000245 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000246
247 // Code completion of superclass names.
248 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000249 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000250 cutOffParsing();
251 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000252 }
253
Chris Lattnerdf195262007-10-09 17:51:17 +0000254 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000255 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000256 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000257 }
258 superClassId = Tok.getIdentifierInfo();
259 superClassLoc = ConsumeToken();
260 }
261 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262 SmallVector<Decl *, 8> ProtocolRefs;
263 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000264 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000265 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000266 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
267 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000268 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
John McCalld226f652010-08-21 09:40:31 +0000270 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000271 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000272 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000273 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000274 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000275 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattnerdf195262007-10-09 17:51:17 +0000277 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000278 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000279
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000280 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000281 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000282}
283
John McCalld0014542009-12-03 22:31:13 +0000284/// The Objective-C property callback. This should be defined where
285/// it's used, but instead it's been lifted to here to support VS2005.
286struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000287private:
288 virtual void anchor();
289public:
John McCalld0014542009-12-03 22:31:13 +0000290 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000291 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000292 ObjCDeclSpec &OCDS;
293 SourceLocation AtLoc;
294 tok::ObjCKeywordKind MethodImplKind;
295
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000296 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000298 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
299 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000300 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000301 MethodImplKind(MethodImplKind) {
302 }
303
John McCalld226f652010-08-21 09:40:31 +0000304 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000305 if (FD.D.getIdentifier() == 0) {
306 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
307 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000308 return 0;
John McCalld0014542009-12-03 22:31:13 +0000309 }
310 if (FD.BitfieldSize) {
311 P.Diag(AtLoc, diag::err_objc_property_bitfield)
312 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000313 return 0;
John McCalld0014542009-12-03 22:31:13 +0000314 }
315
316 // Install the property declarator into interfaceDecl.
317 IdentifierInfo *SelName =
318 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
319
320 Selector GetterSel =
321 P.PP.getSelectorTable().getNullarySelector(SelName);
322 IdentifierInfo *SetterName = OCDS.getSetterName();
323 Selector SetterSel;
324 if (SetterName)
325 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
326 else
327 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
328 P.PP.getSelectorTable(),
329 FD.D.getIdentifier());
330 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000331 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000332 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000333 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000334 &isOverridingProperty,
335 MethodImplKind);
336 if (!isOverridingProperty)
337 Props.push_back(Property);
338
339 return Property;
340 }
341};
342
David Blaikie99ba9e32011-12-20 02:48:34 +0000343void Parser::ObjCPropertyCallback::anchor() {
344}
345
Steve Naroffdac269b2007-08-20 21:31:48 +0000346/// objc-interface-decl-list:
347/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000348/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000349/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000350/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000351/// objc-interface-decl-list declaration
352/// objc-interface-decl-list ';'
353///
Steve Naroff294494e2007-08-22 16:35:03 +0000354/// objc-method-requirement: [OBJC2]
355/// @required
356/// @optional
357///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000358void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
359 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000360 SmallVector<Decl *, 32> allMethods;
361 SmallVector<Decl *, 16> allProperties;
362 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000363 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek782f2f52010-01-07 01:20:12 +0000365 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000366
Steve Naroff294494e2007-08-22 16:35:03 +0000367 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000368 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000369 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000370 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000371 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000372 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000373 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
374 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000375 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
376 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
377 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
378 if (Tok.is(tok::semi))
379 ConsumeToken();
380 }
Steve Naroff294494e2007-08-22 16:35:03 +0000381 continue;
382 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000383 if (Tok.is(tok::l_paren)) {
384 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000385 ParseObjCMethodDecl(Tok.getLocation(),
386 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000387 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000388 continue;
389 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000390 // Ignore excess semicolons.
391 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000392 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000393 continue;
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattnerbc662af2008-10-20 06:10:06 +0000396 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000397 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000398 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000400 // Code completion within an Objective-C interface.
401 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000402 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000403 ObjCImpDecl? Sema::PCC_ObjCImplementation
404 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000405 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000406 }
407
Chris Lattnere82a10f2008-10-20 05:46:22 +0000408 // If we don't have an @ directive, parse it as a function definition.
409 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000410 // The code below does not consume '}'s because it is afraid of eating the
411 // end of a namespace. Because of the way this code is structured, an
412 // erroneous r_brace would cause an infinite loop if not handled here.
413 if (Tok.is(tok::r_brace))
414 break;
John McCall0b7e6782011-03-24 11:26:52 +0000415 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000416 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000417 continue;
418 }
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattnere82a10f2008-10-20 05:46:22 +0000420 // Otherwise, we have an @ directive, eat the @.
421 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000422 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000423 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000424 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000425 break;
426 }
427
Chris Lattnera2449b22008-10-20 05:57:40 +0000428 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnera2449b22008-10-20 05:57:40 +0000430 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000431 AtEnd.setBegin(AtLoc);
432 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000433 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000434 } else if (DirectiveKind == tok::objc_not_keyword) {
435 Diag(Tok, diag::err_objc_unknown_at);
436 SkipUntil(tok::semi);
437 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattnerbc662af2008-10-20 06:10:06 +0000440 // Eat the identifier.
441 ConsumeToken();
442
Chris Lattnera2449b22008-10-20 05:57:40 +0000443 switch (DirectiveKind) {
444 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000445 // FIXME: If someone forgets an @end on a protocol, this loop will
446 // continue to eat up tons of stuff and spew lots of nonsense errors. It
447 // would probably be better to bail out if we saw an @class or @interface
448 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000449 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000450 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000451 SkipUntil(tok::r_brace, tok::at);
452 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000453
454 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000455 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000456 Diag(AtLoc, diag::err_objc_missing_end)
457 << FixItHint::CreateInsertion(AtLoc, "@end\n");
458 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
459 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000460 ConsumeToken();
461 break;
462
Chris Lattnera2449b22008-10-20 05:57:40 +0000463 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000464 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000465 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000466 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000467 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000468 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000469 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000470 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattnera2449b22008-10-20 05:57:40 +0000473 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000474 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000475 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000476
Chris Lattnere82a10f2008-10-20 05:46:22 +0000477 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000478 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000479 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000480 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000482 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000483 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000484
Chris Lattnere82a10f2008-10-20 05:46:22 +0000485 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000486 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000487 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
John McCall7da19ea2011-03-26 01:53:26 +0000489 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000490 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000491 }
Steve Naroff294494e2007-08-22 16:35:03 +0000492 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000493
494 // We break out of the big loop in two cases: when we see @end or when we see
495 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000496 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000497 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000498 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000499 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000500 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000501 } else {
502 Diag(Tok, diag::err_objc_missing_end)
503 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
504 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
505 << (int) Actions.getObjCContainerKind();
506 AtEnd.setBegin(Tok.getLocation());
507 AtEnd.setEnd(Tok.getLocation());
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattnera2449b22008-10-20 05:57:40 +0000510 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000511 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000512 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000513 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000514 allProperties.data(), allProperties.size(),
515 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000516}
517
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000518/// Parse property attribute declarations.
519///
520/// property-attr-decl: '(' property-attrlist ')'
521/// property-attrlist:
522/// property-attribute
523/// property-attrlist ',' property-attribute
524/// property-attribute:
525/// getter '=' identifier
526/// setter '=' identifier ':'
527/// readonly
528/// readwrite
529/// assign
530/// retain
531/// copy
532/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000533/// atomic
534/// strong
535/// weak
536/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000537///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000538void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000539 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000540 BalancedDelimiterTracker T(*this, tok::l_paren);
541 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000543 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000544 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000545 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000546 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000547 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000548 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000550 // If this is not an identifier at all, bail out early.
551 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000552 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000553 return;
554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Chris Lattner156b0612008-10-20 07:37:22 +0000556 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner92e62b02008-11-20 04:42:34 +0000558 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000559 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000560 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000561 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000562 else if (II->isStr("unsafe_unretained"))
563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000564 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000566 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000568 else if (II->isStr("strong"))
569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000570 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000572 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000574 else if (II->isStr("atomic"))
575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000576 else if (II->isStr("weak"))
577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000578 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000579 bool IsSetter = II->getNameStart()[0] == 's';
580
Chris Lattnere00da7c2008-10-20 07:39:53 +0000581 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000582 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
583 diag::err_objc_expected_equal_for_getter;
584
585 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000586 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor4ad96852009-11-19 07:41:15 +0000588 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000589 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000590 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000591 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000592 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000593 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000594 }
595
Anders Carlsson42499be2010-10-02 17:45:21 +0000596
597 SourceLocation SelLoc;
598 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
599
600 if (!SelIdent) {
601 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
602 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000603 SkipUntil(tok::r_paren);
604 return;
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Anders Carlsson42499be2010-10-02 17:45:21 +0000607 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000608 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000609 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000611 if (ExpectAndConsume(tok::colon,
612 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000613 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000614 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000615 } else {
616 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000617 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000618 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000619 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000620 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000621 SkipUntil(tok::r_paren);
622 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattner156b0612008-10-20 07:37:22 +0000625 if (Tok.isNot(tok::comma))
626 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Chris Lattner156b0612008-10-20 07:37:22 +0000628 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000631 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000632}
633
Steve Naroff3536b442007-09-06 21:24:23 +0000634/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000635/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000636/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000637///
638/// objc-instance-method: '-'
639/// objc-class-method: '+'
640///
Steve Naroff4985ace2007-08-22 18:35:33 +0000641/// objc-method-attributes: [OBJC2]
642/// __attribute__((deprecated))
643///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000644Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000645 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000646 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000647
Mike Stump1eb44332009-09-09 15:08:12 +0000648 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000649 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000650 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000651 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000652 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000653 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000654 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000655}
656
657/// objc-selector:
658/// identifier
659/// one of
660/// enum struct union if else while do for switch case default
661/// break continue return goto asm sizeof typeof __alignof
662/// unsigned long const short volatile signed restrict _Complex
663/// in out inout bycopy byref oneway int char float double void _Bool
664///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000665IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000666
Chris Lattnerff384912007-10-07 02:00:24 +0000667 switch (Tok.getKind()) {
668 default:
669 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000670 case tok::ampamp:
671 case tok::ampequal:
672 case tok::amp:
673 case tok::pipe:
674 case tok::tilde:
675 case tok::exclaim:
676 case tok::exclaimequal:
677 case tok::pipepipe:
678 case tok::pipeequal:
679 case tok::caret:
680 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000681 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000682 if (isalpha(ThisTok[0])) {
683 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
684 Tok.setKind(tok::identifier);
685 SelectorLoc = ConsumeToken();
686 return II;
687 }
688 return 0;
689 }
690
Chris Lattnerff384912007-10-07 02:00:24 +0000691 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000692 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000693 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000694 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000695 case tok::kw_break:
696 case tok::kw_case:
697 case tok::kw_catch:
698 case tok::kw_char:
699 case tok::kw_class:
700 case tok::kw_const:
701 case tok::kw_const_cast:
702 case tok::kw_continue:
703 case tok::kw_default:
704 case tok::kw_delete:
705 case tok::kw_do:
706 case tok::kw_double:
707 case tok::kw_dynamic_cast:
708 case tok::kw_else:
709 case tok::kw_enum:
710 case tok::kw_explicit:
711 case tok::kw_export:
712 case tok::kw_extern:
713 case tok::kw_false:
714 case tok::kw_float:
715 case tok::kw_for:
716 case tok::kw_friend:
717 case tok::kw_goto:
718 case tok::kw_if:
719 case tok::kw_inline:
720 case tok::kw_int:
721 case tok::kw_long:
722 case tok::kw_mutable:
723 case tok::kw_namespace:
724 case tok::kw_new:
725 case tok::kw_operator:
726 case tok::kw_private:
727 case tok::kw_protected:
728 case tok::kw_public:
729 case tok::kw_register:
730 case tok::kw_reinterpret_cast:
731 case tok::kw_restrict:
732 case tok::kw_return:
733 case tok::kw_short:
734 case tok::kw_signed:
735 case tok::kw_sizeof:
736 case tok::kw_static:
737 case tok::kw_static_cast:
738 case tok::kw_struct:
739 case tok::kw_switch:
740 case tok::kw_template:
741 case tok::kw_this:
742 case tok::kw_throw:
743 case tok::kw_true:
744 case tok::kw_try:
745 case tok::kw_typedef:
746 case tok::kw_typeid:
747 case tok::kw_typename:
748 case tok::kw_typeof:
749 case tok::kw_union:
750 case tok::kw_unsigned:
751 case tok::kw_using:
752 case tok::kw_virtual:
753 case tok::kw_void:
754 case tok::kw_volatile:
755 case tok::kw_wchar_t:
756 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000757 case tok::kw__Bool:
758 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000759 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000760 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000761 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000762 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000763 }
Steve Naroff294494e2007-08-22 16:35:03 +0000764}
765
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000766/// objc-for-collection-in: 'in'
767///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000768bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000769 // FIXME: May have to do additional look-ahead to only allow for
770 // valid tokens following an 'in'; such as an identifier, unary operators,
771 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000772 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000773 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000774}
775
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000776/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000777/// qualifier list and builds their bitmask representation in the input
778/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000779///
780/// objc-type-qualifiers:
781/// objc-type-qualifier
782/// objc-type-qualifiers objc-type-qualifier
783///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000784void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000785 Declarator::TheContext Context) {
786 assert(Context == Declarator::ObjCParameterContext ||
787 Context == Declarator::ObjCResultContext);
788
Chris Lattnere8b724d2007-12-12 06:56:32 +0000789 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000790 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000791 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000792 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000793 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000794 }
795
Chris Lattnercb53b362007-12-27 19:57:00 +0000796 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000797 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnere8b724d2007-12-12 06:56:32 +0000799 const IdentifierInfo *II = Tok.getIdentifierInfo();
800 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000802 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000805 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000806 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
808 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
809 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
810 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
811 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
812 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000813 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000815 ConsumeToken();
816 II = 0;
817 break;
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattnere8b724d2007-12-12 06:56:32 +0000820 // If this wasn't a recognized qualifier, bail out.
821 if (II) return;
822 }
823}
824
John McCallcdda47f2011-10-01 09:56:14 +0000825/// Take all the decl attributes out of the given list and add
826/// them to the given attribute set.
827static void takeDeclAttributes(ParsedAttributes &attrs,
828 AttributeList *list) {
829 while (list) {
830 AttributeList *cur = list;
831 list = cur->getNext();
832
833 if (!cur->isUsedAsTypeAttr()) {
834 // Clear out the next pointer. We're really completely
835 // destroying the internal invariants of the declarator here,
836 // but it doesn't matter because we're done with it.
837 cur->setNext(0);
838 attrs.add(cur);
839 }
840 }
841}
842
843/// takeDeclAttributes - Take all the decl attributes from the given
844/// declarator and add them to the given list.
845static void takeDeclAttributes(ParsedAttributes &attrs,
846 Declarator &D) {
847 // First, take ownership of all attributes.
848 attrs.getPool().takeAllFrom(D.getAttributePool());
849 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
850
851 // Now actually move the attributes over.
852 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
853 takeDeclAttributes(attrs, D.getAttributes());
854 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
855 takeDeclAttributes(attrs,
856 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
857}
858
Chris Lattnere8b724d2007-12-12 06:56:32 +0000859/// objc-type-name:
860/// '(' objc-type-qualifiers[opt] type-name ')'
861/// '(' objc-type-qualifiers[opt] ')'
862///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000863ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000864 Declarator::TheContext context,
865 ParsedAttributes *paramAttrs) {
866 assert(context == Declarator::ObjCParameterContext ||
867 context == Declarator::ObjCResultContext);
868 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
869
Chris Lattnerdf195262007-10-09 17:51:17 +0000870 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000872 BalancedDelimiterTracker T(*this, tok::l_paren);
873 T.consumeOpen();
874
Chris Lattnere8904e92008-08-23 01:48:03 +0000875 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000876 ObjCDeclContextSwitch ObjCDC(*this);
877
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000878 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000879 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000880
John McCallb3d87482010-08-24 05:47:05 +0000881 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000882 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000883 // Parse an abstract declarator.
884 DeclSpec declSpec(AttrFactory);
885 declSpec.setObjCQualifiers(&DS);
886 ParseSpecifierQualifierList(declSpec);
887 Declarator declarator(declSpec, context);
888 ParseDeclarator(declarator);
889
890 // If that's not invalid, extract a type.
891 if (!declarator.isInvalidType()) {
892 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
893 if (!type.isInvalid())
894 Ty = type.get();
895
896 // If we're parsing a parameter, steal all the decl attributes
897 // and add them to the decl spec.
898 if (context == Declarator::ObjCParameterContext)
899 takeDeclAttributes(*paramAttrs, declarator);
900 }
901 } else if (context == Declarator::ObjCResultContext &&
902 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000903 if (!Ident_instancetype)
904 Ident_instancetype = PP.getIdentifierInfo("instancetype");
905
906 if (Tok.getIdentifierInfo() == Ident_instancetype) {
907 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
908 ConsumeToken();
909 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000910 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000911
Steve Naroffd7333c22008-10-21 14:15:04 +0000912 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000913 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000914 else if (Tok.getLocation() == TypeStartLoc) {
915 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000916 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000917 SkipUntil(tok::r_paren);
918 } else {
919 // Otherwise, we found *something*, but didn't get a ')' in the right
920 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000921 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000922 }
Steve Narofff28b2642007-09-05 23:30:30 +0000923 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000924}
925
926/// objc-method-decl:
927/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000928/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000929/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000930/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000931///
932/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000933/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000934/// objc-keyword-selector objc-keyword-decl
935///
936/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000937/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
938/// objc-selector ':' objc-keyword-attributes[opt] identifier
939/// ':' objc-type-name objc-keyword-attributes[opt] identifier
940/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000941///
Steve Naroff4985ace2007-08-22 18:35:33 +0000942/// objc-parmlist:
943/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000944///
Steve Naroff4985ace2007-08-22 18:35:33 +0000945/// objc-parms:
946/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000947///
Steve Naroff4985ace2007-08-22 18:35:33 +0000948/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000949/// , ...
950///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000951/// objc-keyword-attributes: [OBJC2]
952/// __attribute__((unused))
953///
John McCalld226f652010-08-21 09:40:31 +0000954Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000955 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000956 tok::ObjCKeywordKind MethodImplKind,
957 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000958 ParsingDeclRAIIObject PD(*this);
959
Douglas Gregore8f5a172010-04-07 00:21:17 +0000960 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000961 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000962 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000963 cutOffParsing();
964 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000965 }
966
Chris Lattnere8904e92008-08-23 01:48:03 +0000967 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000968 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000969 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000970 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000971 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek9e049352010-02-18 23:05:16 +0000973 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000974 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000975 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000976 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000977
Douglas Gregore8f5a172010-04-07 00:21:17 +0000978 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000979 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000980 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000981 cutOffParsing();
982 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000983 }
984
Ted Kremenek9e049352010-02-18 23:05:16 +0000985 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000986 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000987 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000988
Steve Naroff84c43102009-02-11 20:43:13 +0000989 // An unnamed colon is valid.
990 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000991 Diag(Tok, diag::err_expected_selector_for_method)
992 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000993 // Skip until we get a ; or {}.
994 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000995 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner5f9e2722011-07-23 10:55:15 +0000998 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000999 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001000 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001001 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001002 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Chris Lattnerff384912007-10-07 02:00:24 +00001004 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001005 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001006 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001007 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001008 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001009 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001010 methodAttrs.getList(), MethodImplKind,
1011 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001012 PD.complete(Result);
1013 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001014 }
Steve Narofff28b2642007-09-05 23:30:30 +00001015
Chris Lattner5f9e2722011-07-23 10:55:15 +00001016 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001017 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001018 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001019 ParseScope PrototypeScope(this,
1020 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001021
1022 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001023
Chris Lattnerff384912007-10-07 02:00:24 +00001024 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001025 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001026 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattnerff384912007-10-07 02:00:24 +00001028 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001029 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001030 Diag(Tok, diag::err_expected_colon);
1031 break;
1032 }
1033 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001034
John McCallb3d87482010-08-24 05:47:05 +00001035 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001036 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001037 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1038 Declarator::ObjCParameterContext,
1039 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001040
Chris Lattnerff384912007-10-07 02:00:24 +00001041 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001042 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001043 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001044 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001045 MaybeParseGNUAttributes(paramAttrs);
1046 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001047 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001048
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001049 // Code completion for the next piece of the selector.
1050 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001051 KeyIdents.push_back(SelIdent);
1052 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1053 mType == tok::minus,
1054 /*AtParameterName=*/true,
1055 ReturnType,
1056 KeyIdents.data(),
1057 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001058 cutOffParsing();
1059 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001060 }
1061
Chris Lattnerdf195262007-10-09 17:51:17 +00001062 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001063 Diag(Tok, diag::err_expected_ident); // missing argument name.
1064 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Chris Lattnere294d3f2009-04-11 18:57:04 +00001067 ArgInfo.Name = Tok.getIdentifierInfo();
1068 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001069 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattnere294d3f2009-04-11 18:57:04 +00001071 ArgInfos.push_back(ArgInfo);
1072 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001073 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001074
John McCall0b7e6782011-03-24 11:26:52 +00001075 // Make sure the attributes persist.
1076 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1077
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001078 // Code completion for the next piece of the selector.
1079 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001080 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1081 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001082 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001083 ReturnType,
1084 KeyIdents.data(),
1085 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001086 cutOffParsing();
1087 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001088 }
1089
Chris Lattnerff384912007-10-07 02:00:24 +00001090 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001091 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001092 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001093 break;
1094 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Steve Naroff335eafa2007-11-15 12:35:21 +00001097 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Chris Lattnerff384912007-10-07 02:00:24 +00001099 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001100 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001101 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001102 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001103 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001104 ConsumeToken();
1105 break;
1106 }
John McCall0b7e6782011-03-24 11:26:52 +00001107 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001108 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001109 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001110 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1111 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001112 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001113 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001114 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1115 ParmDecl.getIdentifierLoc(),
1116 Param,
1117 0));
1118
Chris Lattnerff384912007-10-07 02:00:24 +00001119 }
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001121 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001122 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001123 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001124 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001125
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001126 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001127 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001128
Chris Lattnerff384912007-10-07 02:00:24 +00001129 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1130 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001131 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001132 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001133 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001134 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001135 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001136 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001137 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001138
John McCall54abf7d2009-11-04 02:18:39 +00001139 PD.complete(Result);
1140 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001141}
1142
Steve Naroffdac269b2007-08-20 21:31:48 +00001143/// objc-protocol-refs:
1144/// '<' identifier-list '>'
1145///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001146bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001147ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1148 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001149 bool WarnOnDeclarations,
1150 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001151 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001153 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Chris Lattner5f9e2722011-07-23 10:55:15 +00001155 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattnere13b9592008-07-26 04:03:38 +00001157 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001158 if (Tok.is(tok::code_completion)) {
1159 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1160 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001161 cutOffParsing();
1162 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001163 }
1164
Chris Lattnere13b9592008-07-26 04:03:38 +00001165 if (Tok.isNot(tok::identifier)) {
1166 Diag(Tok, diag::err_expected_ident);
1167 SkipUntil(tok::greater);
1168 return true;
1169 }
1170 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1171 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001172 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001173 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Chris Lattnere13b9592008-07-26 04:03:38 +00001175 if (Tok.isNot(tok::comma))
1176 break;
1177 ConsumeToken();
1178 }
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattnere13b9592008-07-26 04:03:38 +00001180 // Consume the '>'.
1181 if (Tok.isNot(tok::greater)) {
1182 Diag(Tok, diag::err_expected_greater);
1183 return true;
1184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattnere13b9592008-07-26 04:03:38 +00001186 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Chris Lattnere13b9592008-07-26 04:03:38 +00001188 // Convert the list of protocols identifiers into a list of protocol decls.
1189 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1190 &ProtocolIdents[0], ProtocolIdents.size(),
1191 Protocols);
1192 return false;
1193}
1194
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001195/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1196/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001197bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001198 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1199 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1200 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001201 SmallVector<Decl *, 8> ProtocolDecl;
1202 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001203 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1204 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001205 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1206 ProtocolLocs.data(), LAngleLoc);
1207 if (EndProtoLoc.isValid())
1208 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001209 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001210}
1211
1212
Steve Naroffdac269b2007-08-20 21:31:48 +00001213/// objc-class-instance-variables:
1214/// '{' objc-instance-variable-decl-list[opt] '}'
1215///
1216/// objc-instance-variable-decl-list:
1217/// objc-visibility-spec
1218/// objc-instance-variable-decl ';'
1219/// ';'
1220/// objc-instance-variable-decl-list objc-visibility-spec
1221/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1222/// objc-instance-variable-decl-list ';'
1223///
1224/// objc-visibility-spec:
1225/// @private
1226/// @protected
1227/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001228/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001229///
1230/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001231/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001232///
John McCalld226f652010-08-21 09:40:31 +00001233void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001234 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001235 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001236 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001237 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001238
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001239 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001240 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001241
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001242 BalancedDelimiterTracker T(*this, tok::l_brace);
1243 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Steve Naroffddbff782007-08-21 21:17:12 +00001245 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001246 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001247 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Steve Naroffddbff782007-08-21 21:17:12 +00001249 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001250 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001251 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001252 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001253 ConsumeToken();
1254 continue;
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Steve Naroffddbff782007-08-21 21:17:12 +00001257 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001258 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001259 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001260
1261 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001262 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001263 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001264 }
1265
Steve Naroff861cf3e2007-08-23 18:16:40 +00001266 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001267 case tok::objc_private:
1268 case tok::objc_public:
1269 case tok::objc_protected:
1270 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001271 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001272 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001273 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001274 default:
1275 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001276 continue;
1277 }
1278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001280 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001281 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001282 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001283 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001284 }
1285
John McCallbdd563e2009-11-03 02:38:08 +00001286 struct ObjCIvarCallback : FieldCallback {
1287 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001288 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001289 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001290 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001291
John McCalld226f652010-08-21 09:40:31 +00001292 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001293 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001294 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1295 }
1296
John McCalld226f652010-08-21 09:40:31 +00001297 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001298 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001299 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001300 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001301 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001302 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001303 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001304 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001305 if (Field)
1306 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001307 return Field;
1308 }
1309 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001310
Chris Lattnere1359422008-04-10 06:46:29 +00001311 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001312 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001313 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Chris Lattnerdf195262007-10-09 17:51:17 +00001315 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001316 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001317 } else {
1318 Diag(Tok, diag::err_expected_semi_decl_list);
1319 // Skip to end of block or statement
1320 SkipUntil(tok::r_brace, true, true);
1321 }
1322 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001323 T.consumeClose();
1324
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001325 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001326 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001327 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001328 // Call ActOnFields() even if we don't have any decls. This is useful
1329 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001330 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001331 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001332 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001333 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001334}
Steve Naroffdac269b2007-08-20 21:31:48 +00001335
1336/// objc-protocol-declaration:
1337/// objc-protocol-definition
1338/// objc-protocol-forward-reference
1339///
1340/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001341/// @protocol identifier
1342/// objc-protocol-refs[opt]
1343/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001344/// @end
1345///
1346/// objc-protocol-forward-reference:
1347/// @protocol identifier-list ';'
1348///
1349/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001350/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001351/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001352Parser::DeclGroupPtrTy
1353Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1354 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001355 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001356 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1357 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregor083128f2009-11-18 04:49:41 +00001359 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001360 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001361 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001362 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001363 }
1364
Chris Lattnerdf195262007-10-09 17:51:17 +00001365 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001366 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001367 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001368 }
1369 // Save the protocol name, then consume it.
1370 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1371 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Chris Lattnerdf195262007-10-09 17:51:17 +00001373 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001374 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001375 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001376 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001377 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001378 }
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001380 CheckNestedObjCContexts(AtLoc);
1381
Chris Lattnerdf195262007-10-09 17:51:17 +00001382 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001383 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001384 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1385
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001386 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001387 while (1) {
1388 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001389 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001390 Diag(Tok, diag::err_expected_ident);
1391 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001392 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001393 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001394 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1395 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001396 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Chris Lattnerdf195262007-10-09 17:51:17 +00001398 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001399 break;
1400 }
1401 // Consume the ';'.
1402 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001403 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Steve Naroffe440eb82007-10-10 17:32:04 +00001405 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001406 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001407 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001408 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001411 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001412 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001413
Chris Lattner5f9e2722011-07-23 10:55:15 +00001414 SmallVector<Decl *, 8> ProtocolRefs;
1415 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001416 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001417 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1418 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001419 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001420
John McCalld226f652010-08-21 09:40:31 +00001421 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001422 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001423 ProtocolRefs.data(),
1424 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001425 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001426 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001427
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001428 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001429 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001430}
Steve Naroffdac269b2007-08-20 21:31:48 +00001431
1432/// objc-implementation:
1433/// objc-class-implementation-prologue
1434/// objc-category-implementation-prologue
1435///
1436/// objc-class-implementation-prologue:
1437/// @implementation identifier objc-superclass[opt]
1438/// objc-class-instance-variables[opt]
1439///
1440/// objc-category-implementation-prologue:
1441/// @implementation identifier ( identifier )
Erik Verbruggend64251f2011-12-06 09:25:23 +00001442Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001443 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1444 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001445 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001446 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001448 // Code completion after '@implementation'.
1449 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001451 cutOffParsing();
1452 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001453 }
1454
Chris Lattnerdf195262007-10-09 17:51:17 +00001455 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001456 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001457 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001458 }
1459 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001460 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001461 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001462
1463 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001464 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001465 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001466 SourceLocation categoryLoc, rparenLoc;
1467 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001469 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001470 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001471 cutOffParsing();
1472 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001473 }
1474
Chris Lattnerdf195262007-10-09 17:51:17 +00001475 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001476 categoryId = Tok.getIdentifierInfo();
1477 categoryLoc = ConsumeToken();
1478 } else {
1479 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001480 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001481 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001482 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001483 Diag(Tok, diag::err_expected_rparen);
1484 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001485 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001486 }
1487 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001488 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001489 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001490 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001491
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001492 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001493 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001494 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001495 }
1496 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001497 SourceLocation superClassLoc;
1498 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001499 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001500 // We have a super class
1501 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001502 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001503 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001504 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001505 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001506 superClassId = Tok.getIdentifierInfo();
1507 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001508 }
John McCalld226f652010-08-21 09:40:31 +00001509 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001510 AtLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001511 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Steve Naroff60fccee2007-10-29 21:38:07 +00001513 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggend64251f2011-12-06 09:25:23 +00001514 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001515
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001517 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001518 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001519}
Steve Naroff60fccee2007-10-29 21:38:07 +00001520
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001521Parser::DeclGroupPtrTy
1522Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001523 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1524 "ParseObjCAtEndDeclaration(): Expected @end");
1525 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001526 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001527 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001528 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1529 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001530 if (D)
1531 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001532 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001533 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001534
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001535 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001536 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001537 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001538 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001539 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001540 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001541 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001542
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001543 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001544 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001545 return Actions.BuildDeclaratorGroup(
1546 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001547}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001548
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001549Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1550 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001551 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001552 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggend64251f2011-12-06 09:25:23 +00001553
John McCalld226f652010-08-21 09:40:31 +00001554 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggend64251f2011-12-06 09:25:23 +00001555 Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1556 Diag(Tok, diag::err_objc_missing_end)
1557 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1558 if (ImpDecl)
1559 Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1560 << Sema::OCK_Implementation;
1561
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001562 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1563}
1564
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001565void Parser::clearLateParsedObjCMethods() {
1566 for (LateParsedObjCMethodContainer::iterator
1567 I = LateParsedObjCMethods.begin(),
1568 E = LateParsedObjCMethods.end(); I != E; ++I)
1569 delete *I;
1570 LateParsedObjCMethods.clear();
1571}
1572
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001573/// compatibility-alias-decl:
1574/// @compatibility_alias alias-name class-name ';'
1575///
John McCalld226f652010-08-21 09:40:31 +00001576Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001577 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1578 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1579 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001580 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001581 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001582 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001583 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001584 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1585 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001586 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001587 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001588 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001589 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001590 IdentifierInfo *classId = Tok.getIdentifierInfo();
1591 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001592 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1593 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001594 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1595 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001596}
1597
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001598/// property-synthesis:
1599/// @synthesize property-ivar-list ';'
1600///
1601/// property-ivar-list:
1602/// property-ivar
1603/// property-ivar-list ',' property-ivar
1604///
1605/// property-ivar:
1606/// identifier
1607/// identifier '=' identifier
1608///
John McCalld226f652010-08-21 09:40:31 +00001609Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001610 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1611 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001612 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregorb328c422009-11-18 19:45:45 +00001614 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001615 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001616 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001617 cutOffParsing();
1618 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001619 }
1620
Douglas Gregorb328c422009-11-18 19:45:45 +00001621 if (Tok.isNot(tok::identifier)) {
1622 Diag(Tok, diag::err_synthesized_property_name);
1623 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001624 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001625 }
1626
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001627 IdentifierInfo *propertyIvar = 0;
1628 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1629 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001630 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001631 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001632 // property '=' ivar-name
1633 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001634
1635 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001636 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001637 cutOffParsing();
1638 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001639 }
1640
Chris Lattnerdf195262007-10-09 17:51:17 +00001641 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001642 Diag(Tok, diag::err_expected_ident);
1643 break;
1644 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001645 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001646 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001647 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001648 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001649 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001650 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001651 break;
1652 ConsumeToken(); // consume ','
1653 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001654 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001655 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001656}
1657
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001658/// property-dynamic:
1659/// @dynamic property-list
1660///
1661/// property-list:
1662/// identifier
1663/// property-list ',' identifier
1664///
John McCalld226f652010-08-21 09:40:31 +00001665Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001666 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1667 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001668 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001669 while (true) {
1670 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001671 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001672 cutOffParsing();
1673 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001674 }
1675
1676 if (Tok.isNot(tok::identifier)) {
1677 Diag(Tok, diag::err_expected_ident);
1678 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001679 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001680 }
1681
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001682 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1683 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001684 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001685 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001686
Chris Lattnerdf195262007-10-09 17:51:17 +00001687 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001688 break;
1689 ConsumeToken(); // consume ','
1690 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001691 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001692 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001693}
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001695/// objc-throw-statement:
1696/// throw expression[opt];
1697///
John McCall60d7b3a2010-08-24 06:29:42 +00001698StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1699 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001700 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001701 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001702 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001703 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001704 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001705 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001706 }
1707 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001708 // consume ';'
1709 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001710 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001711}
1712
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001713/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001714/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001715///
John McCall60d7b3a2010-08-24 06:29:42 +00001716StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001717Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001718 ConsumeToken(); // consume synchronized
1719 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001720 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001721 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001722 }
John McCall07524032011-07-27 21:50:02 +00001723
1724 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001725 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001726 ExprResult operand(ParseExpression());
1727
1728 if (Tok.is(tok::r_paren)) {
1729 ConsumeParen(); // ')'
1730 } else {
1731 if (!operand.isInvalid())
1732 Diag(Tok, diag::err_expected_rparen);
1733
1734 // Skip forward until we see a left brace, but don't consume it.
1735 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001736 }
John McCall07524032011-07-27 21:50:02 +00001737
1738 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001739 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001740 if (!operand.isInvalid())
1741 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001742 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001743 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001744
John McCall07524032011-07-27 21:50:02 +00001745 // Check the @synchronized operand now.
1746 if (!operand.isInvalid())
1747 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001748
John McCall07524032011-07-27 21:50:02 +00001749 // Parse the compound statement within a new scope.
1750 ParseScope bodyScope(this, Scope::DeclScope);
1751 StmtResult body(ParseCompoundStatementBody());
1752 bodyScope.Exit();
1753
1754 // If there was a semantic or parse error earlier with the
1755 // operand, fail now.
1756 if (operand.isInvalid())
1757 return StmtError();
1758
1759 if (body.isInvalid())
1760 body = Actions.ActOnNullStmt(Tok.getLocation());
1761
1762 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001763}
1764
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001765/// objc-try-catch-statement:
1766/// @try compound-statement objc-catch-list[opt]
1767/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1768///
1769/// objc-catch-list:
1770/// @catch ( parameter-declaration ) compound-statement
1771/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1772/// catch-parameter-declaration:
1773/// parameter-declaration
1774/// '...' [OBJC2]
1775///
John McCall60d7b3a2010-08-24 06:29:42 +00001776StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001777 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001778
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001779 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001780 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001781 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001782 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001783 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001784 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001785 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001786 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001787 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001788 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001789 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001790 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001791
Chris Lattnerdf195262007-10-09 17:51:17 +00001792 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001793 // At this point, we need to lookahead to determine if this @ is the start
1794 // of an @catch or @finally. We don't want to consume the @ token if this
1795 // is an @try or @encode or something else.
1796 Token AfterAt = GetLookAheadToken(1);
1797 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1798 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1799 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001800
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001801 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001802 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001803 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001804 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001805 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001806 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001807 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001808 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001809 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001810 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001811 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001812 ParseDeclarator(ParmDecl);
1813
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001814 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001815 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001816 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001817 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001818 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Steve Naroff93a25952009-04-07 22:56:58 +00001820 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Steve Naroff93a25952009-04-07 22:56:58 +00001822 if (Tok.is(tok::r_paren))
1823 RParenLoc = ConsumeParen();
1824 else // Skip over garbage, until we get to ')'. Eat the ')'.
1825 SkipUntil(tok::r_paren, true, false);
1826
John McCall60d7b3a2010-08-24 06:29:42 +00001827 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001828 if (Tok.is(tok::l_brace))
1829 CatchBody = ParseCompoundStatementBody();
1830 else
1831 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001832 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001833 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001834
John McCall60d7b3a2010-08-24 06:29:42 +00001835 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001836 RParenLoc,
1837 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001838 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001839 if (!Catch.isInvalid())
1840 CatchStmts.push_back(Catch.release());
1841
Steve Naroff64515f32008-02-05 21:27:35 +00001842 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001843 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1844 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001845 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001846 }
1847 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001848 } else {
1849 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001850 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001851 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001852
John McCall60d7b3a2010-08-24 06:29:42 +00001853 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001854 if (Tok.is(tok::l_brace))
1855 FinallyBody = ParseCompoundStatementBody();
1856 else
1857 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001858 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001859 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001860 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001861 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001862 catch_or_finally_seen = true;
1863 break;
1864 }
1865 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001866 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001867 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001868 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001869 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001870
John McCall9ae2f072010-08-23 23:25:46 +00001871 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001872 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001873 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001874}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001875
John McCallf85e1932011-06-15 23:02:42 +00001876/// objc-autoreleasepool-statement:
1877/// @autoreleasepool compound-statement
1878///
1879StmtResult
1880Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1881 ConsumeToken(); // consume autoreleasepool
1882 if (Tok.isNot(tok::l_brace)) {
1883 Diag(Tok, diag::err_expected_lbrace);
1884 return StmtError();
1885 }
1886 // Enter a scope to hold everything within the compound stmt. Compound
1887 // statements can always hold declarations.
1888 ParseScope BodyScope(this, Scope::DeclScope);
1889
1890 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1891
1892 BodyScope.Exit();
1893 if (AutoreleasePoolBody.isInvalid())
1894 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1895 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1896 AutoreleasePoolBody.take());
1897}
1898
Steve Naroff3536b442007-09-06 21:24:23 +00001899/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001900///
John McCalld226f652010-08-21 09:40:31 +00001901Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001902 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001903
John McCallf312b1e2010-08-26 23:41:50 +00001904 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1905 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001907 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001908 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001909 if (ObjCImpDecl) {
1910 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001911 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001912 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001913 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001914 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001915
Steve Naroff409be832007-11-11 19:54:21 +00001916 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001917 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001918 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Steve Naroff409be832007-11-11 19:54:21 +00001920 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1921 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Steve Naroff409be832007-11-11 19:54:21 +00001923 // If we didn't find the '{', bail out.
1924 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001925 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001926 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001927 // Allow the rest of sema to find private method decl implementations.
1928 if (MDecl)
1929 Actions.AddAnyMethodToGlobalPool(MDecl);
1930
1931 // Consume the tokens and store them for later parsing.
1932 LexedMethod* LM = new LexedMethod(this, MDecl);
1933 LateParsedObjCMethods.push_back(LM);
1934 CachedTokens &Toks = LM->Toks;
1935 // Begin by storing the '{' token.
1936 Toks.push_back(Tok);
1937 ConsumeBrace();
1938 // Consume everything up to (and including) the matching right brace.
1939 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001940 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001941}
Anders Carlsson55085182007-08-21 17:43:55 +00001942
John McCall60d7b3a2010-08-24 06:29:42 +00001943StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001944 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001945 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001946 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001947 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001948 }
1949
1950 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001951 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001952
1953 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001954 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001955
1956 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001957 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001958
1959 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1960 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001961
John McCall60d7b3a2010-08-24 06:29:42 +00001962 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001963 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001964 // If the expression is invalid, skip ahead to the next semicolon. Not
1965 // doing this opens us up to the possibility of infinite loops if
1966 // ParseExpression does not consume any tokens.
1967 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001968 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001969 }
Chris Lattner5d803162009-12-07 16:33:19 +00001970
Steve Naroff64515f32008-02-05 21:27:35 +00001971 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001972 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001973 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001974}
1975
John McCall60d7b3a2010-08-24 06:29:42 +00001976ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001977 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001978 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001979 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001980 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001981 return ExprError();
1982
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001983 case tok::string_literal: // primary-expression: string-literal
1984 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001985 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001986 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001987 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001988 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001989
Chris Lattner4fef81d2008-08-05 06:19:09 +00001990 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1991 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001992 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001993 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001994 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001995 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001996 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001997 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001998 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001999 }
Anders Carlsson55085182007-08-21 17:43:55 +00002000 }
Anders Carlsson55085182007-08-21 17:43:55 +00002001}
2002
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002003/// \brirg Parse the receiver of an Objective-C++ message send.
2004///
2005/// This routine parses the receiver of a message send in
2006/// Objective-C++ either as a type or as an expression. Note that this
2007/// routine must not be called to parse a send to 'super', since it
2008/// has no way to return such a result.
2009///
2010/// \param IsExpr Whether the receiver was parsed as an expression.
2011///
2012/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2013/// IsExpr is true), the parsed expression. If the receiver was parsed
2014/// as a type (\c IsExpr is false), the parsed type.
2015///
2016/// \returns True if an error occurred during parsing or semantic
2017/// analysis, in which case the arguments do not have valid
2018/// values. Otherwise, returns false for a successful parse.
2019///
2020/// objc-receiver: [C++]
2021/// 'super' [not parsed here]
2022/// expression
2023/// simple-type-specifier
2024/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002025bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002026 InMessageExpressionRAIIObject InMessage(*this, true);
2027
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002028 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2029 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2030 TryAnnotateTypeOrScopeToken();
2031
2032 if (!isCXXSimpleTypeSpecifier()) {
2033 // objc-receiver:
2034 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002035 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002036 if (Receiver.isInvalid())
2037 return true;
2038
2039 IsExpr = true;
2040 TypeOrExpr = Receiver.take();
2041 return false;
2042 }
2043
2044 // objc-receiver:
2045 // typename-specifier
2046 // simple-type-specifier
2047 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002048 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002049 ParseCXXSimpleTypeSpecifier(DS);
2050
2051 if (Tok.is(tok::l_paren)) {
2052 // If we see an opening parentheses at this point, we are
2053 // actually parsing an expression that starts with a
2054 // function-style cast, e.g.,
2055 //
2056 // postfix-expression:
2057 // simple-type-specifier ( expression-list [opt] )
2058 // typename-specifier ( expression-list [opt] )
2059 //
2060 // Parse the remainder of this case, then the (optional)
2061 // postfix-expression suffix, followed by the (optional)
2062 // right-hand side of the binary expression. We have an
2063 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002064 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002065 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002066 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002067 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002068 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002069 if (Receiver.isInvalid())
2070 return true;
2071
2072 IsExpr = true;
2073 TypeOrExpr = Receiver.take();
2074 return false;
2075 }
2076
2077 // We have a class message. Turn the simple-type-specifier or
2078 // typename-specifier we parsed into a type and parse the
2079 // remainder of the class message.
2080 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002081 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002082 if (Type.isInvalid())
2083 return true;
2084
2085 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002086 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002087 return false;
2088}
2089
Douglas Gregor1b730e82010-05-31 14:40:22 +00002090/// \brief Determine whether the parser is currently referring to a an
2091/// Objective-C message send, using a simplified heuristic to avoid overhead.
2092///
2093/// This routine will only return true for a subset of valid message-send
2094/// expressions.
2095bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002096 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002097 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002098 return GetLookAheadToken(1).is(tok::identifier) &&
2099 GetLookAheadToken(2).is(tok::identifier);
2100}
2101
Douglas Gregor9497a732010-09-16 01:51:54 +00002102bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2103 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2104 InMessageExpression)
2105 return false;
2106
2107
2108 ParsedType Type;
2109
2110 if (Tok.is(tok::annot_typename))
2111 Type = getTypeAnnotation(Tok);
2112 else if (Tok.is(tok::identifier))
2113 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2114 getCurScope());
2115 else
2116 return false;
2117
2118 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2119 const Token &AfterNext = GetLookAheadToken(2);
2120 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2121 if (Tok.is(tok::identifier))
2122 TryAnnotateTypeOrScopeToken();
2123
2124 return Tok.is(tok::annot_typename);
2125 }
2126 }
2127
2128 return false;
2129}
2130
Mike Stump1eb44332009-09-09 15:08:12 +00002131/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002132/// '[' objc-receiver objc-message-args ']'
2133///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002134/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002135/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002136/// expression
2137/// class-name
2138/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002139///
John McCall60d7b3a2010-08-24 06:29:42 +00002140ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002141 assert(Tok.is(tok::l_square) && "'[' expected");
2142 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2143
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002144 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002145 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002146 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002147 return ExprError();
2148 }
2149
Douglas Gregor0fbda682010-09-15 14:51:05 +00002150 InMessageExpressionRAIIObject InMessage(*this, true);
2151
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002152 if (getLang().CPlusPlus) {
2153 // We completely separate the C and C++ cases because C++ requires
2154 // more complicated (read: slower) parsing.
2155
2156 // Handle send to super.
2157 // FIXME: This doesn't benefit from the same typo-correction we
2158 // get in Objective-C.
2159 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002160 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002161 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2162 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002163
2164 // Parse the receiver, which is either a type or an expression.
2165 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002166 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002167 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2168 SkipUntil(tok::r_square);
2169 return ExprError();
2170 }
2171
2172 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002173 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2174 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002175 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002176
2177 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002178 ParsedType::getFromOpaquePtr(TypeOrExpr),
2179 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002180 }
2181
2182 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002183 IdentifierInfo *Name = Tok.getIdentifierInfo();
2184 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002185 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002186 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002187 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002188 NextToken().is(tok::period),
2189 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002190 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002191 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2192 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002193
John McCallf312b1e2010-08-26 23:41:50 +00002194 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002195 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002196 SkipUntil(tok::r_square);
2197 return ExprError();
2198 }
2199
Douglas Gregor1569f952010-04-21 20:38:13 +00002200 ConsumeToken(); // the type name
2201
2202 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002203 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002204
John McCallf312b1e2010-08-26 23:41:50 +00002205 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002206 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002207 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002208 }
Chris Lattner699b6612008-01-25 18:59:06 +00002209 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002210
2211 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002212 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002213 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002214 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002215 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002216 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002217
John McCallb3d87482010-08-24 05:47:05 +00002218 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2219 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002220}
Sebastian Redl1d922962008-12-13 15:32:12 +00002221
Douglas Gregor2725ca82010-04-21 19:57:20 +00002222/// \brief Parse the remainder of an Objective-C message following the
2223/// '[' objc-receiver.
2224///
2225/// This routine handles sends to super, class messages (sent to a
2226/// class name), and instance messages (sent to an object), and the
2227/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2228/// ReceiverExpr, respectively. Only one of these parameters may have
2229/// a valid value.
2230///
2231/// \param LBracLoc The location of the opening '['.
2232///
2233/// \param SuperLoc If this is a send to 'super', the location of the
2234/// 'super' keyword that indicates a send to the superclass.
2235///
2236/// \param ReceiverType If this is a class message, the type of the
2237/// class we are sending a message to.
2238///
2239/// \param ReceiverExpr If this is an instance message, the expression
2240/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002241///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002242/// objc-message-args:
2243/// objc-selector
2244/// objc-keywordarg-list
2245///
2246/// objc-keywordarg-list:
2247/// objc-keywordarg
2248/// objc-keywordarg-list objc-keywordarg
2249///
Mike Stump1eb44332009-09-09 15:08:12 +00002250/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002251/// selector-name[opt] ':' objc-keywordexpr
2252///
2253/// objc-keywordexpr:
2254/// nonempty-expr-list
2255///
2256/// nonempty-expr-list:
2257/// assignment-expression
2258/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002259///
John McCall60d7b3a2010-08-24 06:29:42 +00002260ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002261Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002262 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002263 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002264 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002265 InMessageExpressionRAIIObject InMessage(*this, true);
2266
Steve Naroffc4df6d22009-11-07 02:08:14 +00002267 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002268 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002269 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2270 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002271 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002272 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2273 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002274 else
John McCall9ae2f072010-08-23 23:25:46 +00002275 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002276 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002277 cutOffParsing();
2278 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002279 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002280
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002281 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002282 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002283 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002284
Chris Lattner5f9e2722011-07-23 10:55:15 +00002285 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002286 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002287 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002288
Chris Lattnerdf195262007-10-09 17:51:17 +00002289 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002290 while (1) {
2291 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002292 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002293 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002294
Chris Lattnerdf195262007-10-09 17:51:17 +00002295 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002296 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002297 // We must manually skip to a ']', otherwise the expression skipper will
2298 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2299 // the enclosing expression.
2300 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002301 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002302 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002303
Steve Naroff68d331a2007-09-27 14:38:14 +00002304 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002305 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002306
2307 if (Tok.is(tok::code_completion)) {
2308 if (SuperLoc.isValid())
2309 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2310 KeyIdents.data(),
2311 KeyIdents.size(),
2312 /*AtArgumentEpression=*/true);
2313 else if (ReceiverType)
2314 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2315 KeyIdents.data(),
2316 KeyIdents.size(),
2317 /*AtArgumentEpression=*/true);
2318 else
2319 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2320 KeyIdents.data(),
2321 KeyIdents.size(),
2322 /*AtArgumentEpression=*/true);
2323
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002324 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002325 return ExprError();
2326 }
2327
John McCall60d7b3a2010-08-24 06:29:42 +00002328 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002329 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002330 // We must manually skip to a ']', otherwise the expression skipper will
2331 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2332 // the enclosing expression.
2333 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002334 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002335 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002336
Steve Naroff37387c92007-09-17 20:25:27 +00002337 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002338 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002339
Douglas Gregord3c68542009-11-19 01:08:35 +00002340 // Code completion after each argument.
2341 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002342 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002343 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002344 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002345 KeyIdents.size(),
2346 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002347 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002348 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002349 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002350 KeyIdents.size(),
2351 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002352 else
John McCall9ae2f072010-08-23 23:25:46 +00002353 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002354 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002355 KeyIdents.size(),
2356 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002357 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002358 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002359 }
2360
Steve Naroff37387c92007-09-17 20:25:27 +00002361 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002362 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002363 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002364 break;
2365 // We have a selector or a colon, continue parsing.
2366 }
2367 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002368 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002369 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002370 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002371 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002372 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002373 // We must manually skip to a ']', otherwise the expression skipper will
2374 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2375 // the enclosing expression.
2376 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002377 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002378 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002379
Steve Naroff49f109c2007-11-15 13:05:42 +00002380 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002381 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002382 }
2383 } else if (!selIdent) {
2384 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002385
Chris Lattner4fef81d2008-08-05 06:19:09 +00002386 // We must manually skip to a ']', otherwise the expression skipper will
2387 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2388 // the enclosing expression.
2389 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002390 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002391 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002392
Chris Lattnerdf195262007-10-09 17:51:17 +00002393 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002394 if (Tok.is(tok::identifier))
2395 Diag(Tok, diag::err_expected_colon);
2396 else
2397 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002398 // We must manually skip to a ']', otherwise the expression skipper will
2399 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2400 // the enclosing expression.
2401 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002402 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002403 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002404
Chris Lattner699b6612008-01-25 18:59:06 +00002405 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002406
Steve Naroff29238a02007-10-05 18:42:47 +00002407 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002408 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002409 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002410 KeyLocs.push_back(Loc);
2411 }
Chris Lattnerff384912007-10-07 02:00:24 +00002412 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002413
Douglas Gregor2725ca82010-04-21 19:57:20 +00002414 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002415 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002416 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002417 MultiExprArg(Actions,
2418 KeyExprs.take(),
2419 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002420 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002421 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002422 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002423 MultiExprArg(Actions,
2424 KeyExprs.take(),
2425 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002426 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002427 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002428 MultiExprArg(Actions,
2429 KeyExprs.take(),
2430 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002431}
2432
John McCall60d7b3a2010-08-24 06:29:42 +00002433ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2434 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002435 if (Res.isInvalid()) return move(Res);
2436
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002437 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2438 // expressions. At this point, we know that the only valid thing that starts
2439 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002440 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002441 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002442 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002443 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002444
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002445 while (Tok.is(tok::at)) {
2446 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002447
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002448 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002449 if (!isTokenStringLiteral())
2450 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002451
John McCall60d7b3a2010-08-24 06:29:42 +00002452 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002453 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002454 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002455
Sebastian Redleffa8d12008-12-10 00:02:53 +00002456 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002457 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002458
2459 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2460 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002461}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002462
2463/// objc-encode-expression:
2464/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002465ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002466Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002467 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002468
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002469 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002470
Chris Lattner4fef81d2008-08-05 06:19:09 +00002471 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002472 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2473
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002474 BalancedDelimiterTracker T(*this, tok::l_paren);
2475 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002476
Douglas Gregor809070a2009-02-18 17:45:20 +00002477 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002478
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002479 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002480
Douglas Gregor809070a2009-02-18 17:45:20 +00002481 if (Ty.isInvalid())
2482 return ExprError();
2483
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002484 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2485 T.getOpenLocation(), Ty.get(),
2486 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002487}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002488
2489/// objc-protocol-expression
2490/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002491ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002492Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002493 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002494
Chris Lattner4fef81d2008-08-05 06:19:09 +00002495 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002496 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2497
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002498 BalancedDelimiterTracker T(*this, tok::l_paren);
2499 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002500
Chris Lattner4fef81d2008-08-05 06:19:09 +00002501 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002502 return ExprError(Diag(Tok, diag::err_expected_ident));
2503
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002504 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002505 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002506
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002507 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002508
Sebastian Redl1d922962008-12-13 15:32:12 +00002509 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002510 T.getOpenLocation(),
2511 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002512}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002513
2514/// objc-selector-expression
2515/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002516ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002517 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002518
Chris Lattner4fef81d2008-08-05 06:19:09 +00002519 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002520 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2521
Chris Lattner5f9e2722011-07-23 10:55:15 +00002522 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002523 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002524
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002525 BalancedDelimiterTracker T(*this, tok::l_paren);
2526 T.consumeOpen();
2527
Douglas Gregor458433d2010-08-26 15:07:07 +00002528 if (Tok.is(tok::code_completion)) {
2529 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2530 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002531 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002532 return ExprError();
2533 }
2534
Chris Lattner2fc5c242009-04-11 18:13:45 +00002535 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002536 if (!SelIdent && // missing selector name.
2537 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002538 return ExprError(Diag(Tok, diag::err_expected_ident));
2539
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002540 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002541 unsigned nColons = 0;
2542 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002543 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002544 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2545 ++nColons;
2546 KeyIdents.push_back(0);
2547 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002548 return ExprError(Diag(Tok, diag::err_expected_colon));
2549
Chris Lattner5add7542010-08-27 22:32:41 +00002550 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002551 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002552 if (Tok.is(tok::r_paren))
2553 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002554
2555 if (Tok.is(tok::code_completion)) {
2556 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2557 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002558 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002559 return ExprError();
2560 }
2561
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002562 // Check for another keyword selector.
2563 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002564 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002565 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002566 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002567 break;
2568 }
Steve Naroff887407e2007-12-05 22:21:29 +00002569 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002570 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002571 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002572 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002573 T.getOpenLocation(),
2574 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002575 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002576
2577Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002578
2579 // Save the current token position.
2580 SourceLocation OrigLoc = Tok.getLocation();
2581
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002582 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2583 // Append the current token at the end of the new token stream so that it
2584 // doesn't get lost.
2585 LM.Toks.push_back(Tok);
2586 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2587
2588 // MDecl might be null due to error in method prototype, etc.
2589 Decl *MDecl = LM.D;
2590 // Consume the previously pushed token.
2591 ConsumeAnyToken();
2592
2593 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2594 SourceLocation BraceLoc = Tok.getLocation();
2595 // Enter a scope for the method body.
2596 ParseScope BodyScope(this,
2597 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2598
2599 // Tell the actions module that we have entered a method definition with the
2600 // specified Declarator for the method.
2601 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2602
2603 if (PP.isCodeCompletionEnabled()) {
2604 if (trySkippingFunctionBodyForCodeCompletion()) {
2605 BodyScope.Exit();
2606 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2607 }
2608 }
2609
2610 StmtResult FnBody(ParseCompoundStatementBody());
2611
2612 // If the function body could not be parsed, make a bogus compoundstmt.
2613 if (FnBody.isInvalid())
2614 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2615 MultiStmtArg(Actions), false);
2616
2617 // Leave the function body scope.
2618 BodyScope.Exit();
2619
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002620 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2621
2622 if (Tok.getLocation() != OrigLoc) {
2623 // Due to parsing error, we either went over the cached tokens or
2624 // there are still cached tokens left. If it's the latter case skip the
2625 // leftover tokens.
2626 // Since this is an uncommon situation that should be avoided, use the
2627 // expensive isBeforeInTranslationUnit call.
2628 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2629 OrigLoc))
2630 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2631 ConsumeAnyToken();
2632 }
2633
2634 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002635}