blob: 56d5c57c056d41baf85e4d3dadf581125a629fa2 [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);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000053 SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
54 break;
John McCall7f040a92010-12-24 02:08:15 +000055 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000056 case tok::objc_implementation:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000057 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
58 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000059 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000060 return ParseObjCAtEndDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000062 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
64 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000065 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
67 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000068 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000069 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
70 break;
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 {
287 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000289 ObjCDeclSpec &OCDS;
290 SourceLocation AtLoc;
291 tok::ObjCKeywordKind MethodImplKind;
292
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000293 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000294 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000295 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
296 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000297 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000298 MethodImplKind(MethodImplKind) {
299 }
300
John McCalld226f652010-08-21 09:40:31 +0000301 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000302 if (FD.D.getIdentifier() == 0) {
303 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
304 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000305 return 0;
John McCalld0014542009-12-03 22:31:13 +0000306 }
307 if (FD.BitfieldSize) {
308 P.Diag(AtLoc, diag::err_objc_property_bitfield)
309 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000310 return 0;
John McCalld0014542009-12-03 22:31:13 +0000311 }
312
313 // Install the property declarator into interfaceDecl.
314 IdentifierInfo *SelName =
315 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
316
317 Selector GetterSel =
318 P.PP.getSelectorTable().getNullarySelector(SelName);
319 IdentifierInfo *SetterName = OCDS.getSetterName();
320 Selector SetterSel;
321 if (SetterName)
322 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
323 else
324 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
325 P.PP.getSelectorTable(),
326 FD.D.getIdentifier());
327 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000328 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000329 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000330 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000331 &isOverridingProperty,
332 MethodImplKind);
333 if (!isOverridingProperty)
334 Props.push_back(Property);
335
336 return Property;
337 }
338};
339
Steve Naroffdac269b2007-08-20 21:31:48 +0000340/// objc-interface-decl-list:
341/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000342/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000343/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000344/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000345/// objc-interface-decl-list declaration
346/// objc-interface-decl-list ';'
347///
Steve Naroff294494e2007-08-22 16:35:03 +0000348/// objc-method-requirement: [OBJC2]
349/// @required
350/// @optional
351///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000352void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
353 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354 SmallVector<Decl *, 32> allMethods;
355 SmallVector<Decl *, 16> allProperties;
356 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000357 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek782f2f52010-01-07 01:20:12 +0000359 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000360
Steve Naroff294494e2007-08-22 16:35:03 +0000361 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000362 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000363 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000364 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000365 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000366 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000367 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
368 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000369 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
370 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000371 continue;
372 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000373 if (Tok.is(tok::l_paren)) {
374 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000375 ParseObjCMethodDecl(Tok.getLocation(),
376 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000377 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000378 continue;
379 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000380 // Ignore excess semicolons.
381 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000382 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000383 continue;
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnerbc662af2008-10-20 06:10:06 +0000386 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000387 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000388 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000390 // Code completion within an Objective-C interface.
391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000393 ObjCImpDecl? Sema::PCC_ObjCImplementation
394 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000395 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000396 }
397
Chris Lattnere82a10f2008-10-20 05:46:22 +0000398 // If we don't have an @ directive, parse it as a function definition.
399 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000400 // The code below does not consume '}'s because it is afraid of eating the
401 // end of a namespace. Because of the way this code is structured, an
402 // erroneous r_brace would cause an infinite loop if not handled here.
403 if (Tok.is(tok::r_brace))
404 break;
John McCall0b7e6782011-03-24 11:26:52 +0000405 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000406 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000407 continue;
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnere82a10f2008-10-20 05:46:22 +0000410 // Otherwise, we have an @ directive, eat the @.
411 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000412 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000413 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000414 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000415 break;
416 }
417
Chris Lattnera2449b22008-10-20 05:57:40 +0000418 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000421 AtEnd.setBegin(AtLoc);
422 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000423 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000424 } else if (DirectiveKind == tok::objc_not_keyword) {
425 Diag(Tok, diag::err_objc_unknown_at);
426 SkipUntil(tok::semi);
427 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnerbc662af2008-10-20 06:10:06 +0000430 // Eat the identifier.
431 ConsumeToken();
432
Chris Lattnera2449b22008-10-20 05:57:40 +0000433 switch (DirectiveKind) {
434 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000435 // FIXME: If someone forgets an @end on a protocol, this loop will
436 // continue to eat up tons of stuff and spew lots of nonsense errors. It
437 // would probably be better to bail out if we saw an @class or @interface
438 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000439 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000440 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000441 SkipUntil(tok::r_brace, tok::at);
442 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000443
444 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000445 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000446 Diag(AtLoc, diag::err_objc_missing_end)
447 << FixItHint::CreateInsertion(AtLoc, "@end\n");
448 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
449 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000450 ConsumeToken();
451 break;
452
Chris Lattnera2449b22008-10-20 05:57:40 +0000453 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000454 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000455 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000456 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000457 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000458 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000459 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000460 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000461 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattnera2449b22008-10-20 05:57:40 +0000463 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000464 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000465 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000466
Chris Lattnere82a10f2008-10-20 05:46:22 +0000467 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000468 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000469 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000470 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000472 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000473 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000474
Chris Lattnere82a10f2008-10-20 05:46:22 +0000475 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000476 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000477 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
John McCall7da19ea2011-03-26 01:53:26 +0000479 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000480 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000481 }
Steve Naroff294494e2007-08-22 16:35:03 +0000482 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000483
484 // We break out of the big loop in two cases: when we see @end or when we see
485 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000486 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000487 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000488 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000489 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000490 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000491 } else {
492 Diag(Tok, diag::err_objc_missing_end)
493 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
494 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
495 << (int) Actions.getObjCContainerKind();
496 AtEnd.setBegin(Tok.getLocation());
497 AtEnd.setEnd(Tok.getLocation());
498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattnera2449b22008-10-20 05:57:40 +0000500 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000501 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000502 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000503 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000504 allProperties.data(), allProperties.size(),
505 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000506}
507
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000508/// Parse property attribute declarations.
509///
510/// property-attr-decl: '(' property-attrlist ')'
511/// property-attrlist:
512/// property-attribute
513/// property-attrlist ',' property-attribute
514/// property-attribute:
515/// getter '=' identifier
516/// setter '=' identifier ':'
517/// readonly
518/// readwrite
519/// assign
520/// retain
521/// copy
522/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000523/// atomic
524/// strong
525/// weak
526/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000527///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000528void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000529 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000530 BalancedDelimiterTracker T(*this, tok::l_paren);
531 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000533 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000534 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000535 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000536 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000537 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000538 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000540 // If this is not an identifier at all, bail out early.
541 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000542 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000543 return;
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner156b0612008-10-20 07:37:22 +0000546 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner92e62b02008-11-20 04:42:34 +0000548 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000549 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000550 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000551 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000552 else if (II->isStr("unsafe_unretained"))
553 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000554 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000555 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000556 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000557 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000558 else if (II->isStr("strong"))
559 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000560 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000561 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000562 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000564 else if (II->isStr("atomic"))
565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000566 else if (II->isStr("weak"))
567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000568 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000569 bool IsSetter = II->getNameStart()[0] == 's';
570
Chris Lattnere00da7c2008-10-20 07:39:53 +0000571 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000572 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
573 diag::err_objc_expected_equal_for_getter;
574
575 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000576 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Douglas Gregor4ad96852009-11-19 07:41:15 +0000578 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000579 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000580 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000581 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000582 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000583 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000584 }
585
Anders Carlsson42499be2010-10-02 17:45:21 +0000586
587 SourceLocation SelLoc;
588 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
589
590 if (!SelIdent) {
591 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
592 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000593 SkipUntil(tok::r_paren);
594 return;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Anders Carlsson42499be2010-10-02 17:45:21 +0000597 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000598 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000599 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000601 if (ExpectAndConsume(tok::colon,
602 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000603 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000604 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000605 } else {
606 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000607 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000608 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000609 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000610 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000611 SkipUntil(tok::r_paren);
612 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattner156b0612008-10-20 07:37:22 +0000615 if (Tok.isNot(tok::comma))
616 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner156b0612008-10-20 07:37:22 +0000618 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000621 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000622}
623
Steve Naroff3536b442007-09-06 21:24:23 +0000624/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000625/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000626/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000627///
628/// objc-instance-method: '-'
629/// objc-class-method: '+'
630///
Steve Naroff4985ace2007-08-22 18:35:33 +0000631/// objc-method-attributes: [OBJC2]
632/// __attribute__((deprecated))
633///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000634Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000635 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000636 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000637
Mike Stump1eb44332009-09-09 15:08:12 +0000638 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000639 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000640 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000641 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000642 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000643 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000644 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000645}
646
647/// objc-selector:
648/// identifier
649/// one of
650/// enum struct union if else while do for switch case default
651/// break continue return goto asm sizeof typeof __alignof
652/// unsigned long const short volatile signed restrict _Complex
653/// in out inout bycopy byref oneway int char float double void _Bool
654///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000655IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000656
Chris Lattnerff384912007-10-07 02:00:24 +0000657 switch (Tok.getKind()) {
658 default:
659 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000660 case tok::ampamp:
661 case tok::ampequal:
662 case tok::amp:
663 case tok::pipe:
664 case tok::tilde:
665 case tok::exclaim:
666 case tok::exclaimequal:
667 case tok::pipepipe:
668 case tok::pipeequal:
669 case tok::caret:
670 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000671 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000672 if (isalpha(ThisTok[0])) {
673 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
674 Tok.setKind(tok::identifier);
675 SelectorLoc = ConsumeToken();
676 return II;
677 }
678 return 0;
679 }
680
Chris Lattnerff384912007-10-07 02:00:24 +0000681 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000682 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000683 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000684 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000685 case tok::kw_break:
686 case tok::kw_case:
687 case tok::kw_catch:
688 case tok::kw_char:
689 case tok::kw_class:
690 case tok::kw_const:
691 case tok::kw_const_cast:
692 case tok::kw_continue:
693 case tok::kw_default:
694 case tok::kw_delete:
695 case tok::kw_do:
696 case tok::kw_double:
697 case tok::kw_dynamic_cast:
698 case tok::kw_else:
699 case tok::kw_enum:
700 case tok::kw_explicit:
701 case tok::kw_export:
702 case tok::kw_extern:
703 case tok::kw_false:
704 case tok::kw_float:
705 case tok::kw_for:
706 case tok::kw_friend:
707 case tok::kw_goto:
708 case tok::kw_if:
709 case tok::kw_inline:
710 case tok::kw_int:
711 case tok::kw_long:
712 case tok::kw_mutable:
713 case tok::kw_namespace:
714 case tok::kw_new:
715 case tok::kw_operator:
716 case tok::kw_private:
717 case tok::kw_protected:
718 case tok::kw_public:
719 case tok::kw_register:
720 case tok::kw_reinterpret_cast:
721 case tok::kw_restrict:
722 case tok::kw_return:
723 case tok::kw_short:
724 case tok::kw_signed:
725 case tok::kw_sizeof:
726 case tok::kw_static:
727 case tok::kw_static_cast:
728 case tok::kw_struct:
729 case tok::kw_switch:
730 case tok::kw_template:
731 case tok::kw_this:
732 case tok::kw_throw:
733 case tok::kw_true:
734 case tok::kw_try:
735 case tok::kw_typedef:
736 case tok::kw_typeid:
737 case tok::kw_typename:
738 case tok::kw_typeof:
739 case tok::kw_union:
740 case tok::kw_unsigned:
741 case tok::kw_using:
742 case tok::kw_virtual:
743 case tok::kw_void:
744 case tok::kw_volatile:
745 case tok::kw_wchar_t:
746 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000747 case tok::kw__Bool:
748 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000749 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000750 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000751 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000752 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000753 }
Steve Naroff294494e2007-08-22 16:35:03 +0000754}
755
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000756/// objc-for-collection-in: 'in'
757///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000758bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000759 // FIXME: May have to do additional look-ahead to only allow for
760 // valid tokens following an 'in'; such as an identifier, unary operators,
761 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000762 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000763 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000764}
765
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000766/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000767/// qualifier list and builds their bitmask representation in the input
768/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000769///
770/// objc-type-qualifiers:
771/// objc-type-qualifier
772/// objc-type-qualifiers objc-type-qualifier
773///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000774void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000775 Declarator::TheContext Context) {
776 assert(Context == Declarator::ObjCParameterContext ||
777 Context == Declarator::ObjCResultContext);
778
Chris Lattnere8b724d2007-12-12 06:56:32 +0000779 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000780 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000781 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000782 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000783 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000784 }
785
Chris Lattnercb53b362007-12-27 19:57:00 +0000786 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000787 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattnere8b724d2007-12-12 06:56:32 +0000789 const IdentifierInfo *II = Tok.getIdentifierInfo();
790 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000791 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000792 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000795 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000796 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000797 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
798 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
799 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
800 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
801 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
802 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000803 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000805 ConsumeToken();
806 II = 0;
807 break;
808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattnere8b724d2007-12-12 06:56:32 +0000810 // If this wasn't a recognized qualifier, bail out.
811 if (II) return;
812 }
813}
814
John McCallcdda47f2011-10-01 09:56:14 +0000815/// Take all the decl attributes out of the given list and add
816/// them to the given attribute set.
817static void takeDeclAttributes(ParsedAttributes &attrs,
818 AttributeList *list) {
819 while (list) {
820 AttributeList *cur = list;
821 list = cur->getNext();
822
823 if (!cur->isUsedAsTypeAttr()) {
824 // Clear out the next pointer. We're really completely
825 // destroying the internal invariants of the declarator here,
826 // but it doesn't matter because we're done with it.
827 cur->setNext(0);
828 attrs.add(cur);
829 }
830 }
831}
832
833/// takeDeclAttributes - Take all the decl attributes from the given
834/// declarator and add them to the given list.
835static void takeDeclAttributes(ParsedAttributes &attrs,
836 Declarator &D) {
837 // First, take ownership of all attributes.
838 attrs.getPool().takeAllFrom(D.getAttributePool());
839 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
840
841 // Now actually move the attributes over.
842 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
843 takeDeclAttributes(attrs, D.getAttributes());
844 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
845 takeDeclAttributes(attrs,
846 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
847}
848
Chris Lattnere8b724d2007-12-12 06:56:32 +0000849/// objc-type-name:
850/// '(' objc-type-qualifiers[opt] type-name ')'
851/// '(' objc-type-qualifiers[opt] ')'
852///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000853ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000854 Declarator::TheContext context,
855 ParsedAttributes *paramAttrs) {
856 assert(context == Declarator::ObjCParameterContext ||
857 context == Declarator::ObjCResultContext);
858 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
859
Chris Lattnerdf195262007-10-09 17:51:17 +0000860 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000862 BalancedDelimiterTracker T(*this, tok::l_paren);
863 T.consumeOpen();
864
Chris Lattnere8904e92008-08-23 01:48:03 +0000865 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000866 ObjCDeclContextSwitch ObjCDC(*this);
867
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000868 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000869 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000870
John McCallb3d87482010-08-24 05:47:05 +0000871 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000872 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000873 // Parse an abstract declarator.
874 DeclSpec declSpec(AttrFactory);
875 declSpec.setObjCQualifiers(&DS);
876 ParseSpecifierQualifierList(declSpec);
877 Declarator declarator(declSpec, context);
878 ParseDeclarator(declarator);
879
880 // If that's not invalid, extract a type.
881 if (!declarator.isInvalidType()) {
882 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
883 if (!type.isInvalid())
884 Ty = type.get();
885
886 // If we're parsing a parameter, steal all the decl attributes
887 // and add them to the decl spec.
888 if (context == Declarator::ObjCParameterContext)
889 takeDeclAttributes(*paramAttrs, declarator);
890 }
891 } else if (context == Declarator::ObjCResultContext &&
892 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000893 if (!Ident_instancetype)
894 Ident_instancetype = PP.getIdentifierInfo("instancetype");
895
896 if (Tok.getIdentifierInfo() == Ident_instancetype) {
897 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
898 ConsumeToken();
899 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000900 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000901
Steve Naroffd7333c22008-10-21 14:15:04 +0000902 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000903 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000904 else if (Tok.getLocation() == TypeStartLoc) {
905 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000906 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000907 SkipUntil(tok::r_paren);
908 } else {
909 // Otherwise, we found *something*, but didn't get a ')' in the right
910 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000911 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000912 }
Steve Narofff28b2642007-09-05 23:30:30 +0000913 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000914}
915
916/// objc-method-decl:
917/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000918/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000919/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000920/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000921///
922/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000923/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000924/// objc-keyword-selector objc-keyword-decl
925///
926/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000927/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
928/// objc-selector ':' objc-keyword-attributes[opt] identifier
929/// ':' objc-type-name objc-keyword-attributes[opt] identifier
930/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000931///
Steve Naroff4985ace2007-08-22 18:35:33 +0000932/// objc-parmlist:
933/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000934///
Steve Naroff4985ace2007-08-22 18:35:33 +0000935/// objc-parms:
936/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000937///
Steve Naroff4985ace2007-08-22 18:35:33 +0000938/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000939/// , ...
940///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000941/// objc-keyword-attributes: [OBJC2]
942/// __attribute__((unused))
943///
John McCalld226f652010-08-21 09:40:31 +0000944Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000945 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000946 tok::ObjCKeywordKind MethodImplKind,
947 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000948 ParsingDeclRAIIObject PD(*this);
949
Douglas Gregore8f5a172010-04-07 00:21:17 +0000950 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000951 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000952 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000953 cutOffParsing();
954 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000955 }
956
Chris Lattnere8904e92008-08-23 01:48:03 +0000957 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000958 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000959 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000960 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000961 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Ted Kremenek9e049352010-02-18 23:05:16 +0000963 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000964 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000965 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000966 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000967
Douglas Gregore8f5a172010-04-07 00:21:17 +0000968 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000969 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000970 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000971 cutOffParsing();
972 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000973 }
974
Ted Kremenek9e049352010-02-18 23:05:16 +0000975 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000976 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000977 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000978
Steve Naroff84c43102009-02-11 20:43:13 +0000979 // An unnamed colon is valid.
980 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000981 Diag(Tok, diag::err_expected_selector_for_method)
982 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000983 // Skip until we get a ; or {}.
984 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000985 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Chris Lattner5f9e2722011-07-23 10:55:15 +0000988 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000989 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000990 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000991 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000992 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Chris Lattnerff384912007-10-07 02:00:24 +0000994 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000995 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000996 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000997 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000998 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000999 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001000 methodAttrs.getList(), MethodImplKind,
1001 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001002 PD.complete(Result);
1003 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001004 }
Steve Narofff28b2642007-09-05 23:30:30 +00001005
Chris Lattner5f9e2722011-07-23 10:55:15 +00001006 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001007 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001008 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001009 ParseScope PrototypeScope(this,
1010 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001011
1012 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001013
Chris Lattnerff384912007-10-07 02:00:24 +00001014 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001015 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001016 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Chris Lattnerff384912007-10-07 02:00:24 +00001018 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001020 Diag(Tok, diag::err_expected_colon);
1021 break;
1022 }
1023 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001024
John McCallb3d87482010-08-24 05:47:05 +00001025 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001026 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001027 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1028 Declarator::ObjCParameterContext,
1029 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001030
Chris Lattnerff384912007-10-07 02:00:24 +00001031 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001032 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001033 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001034 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001035 MaybeParseGNUAttributes(paramAttrs);
1036 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001037 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001038
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001039 // Code completion for the next piece of the selector.
1040 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001041 KeyIdents.push_back(SelIdent);
1042 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1043 mType == tok::minus,
1044 /*AtParameterName=*/true,
1045 ReturnType,
1046 KeyIdents.data(),
1047 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001048 cutOffParsing();
1049 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001050 }
1051
Chris Lattnerdf195262007-10-09 17:51:17 +00001052 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001053 Diag(Tok, diag::err_expected_ident); // missing argument name.
1054 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattnere294d3f2009-04-11 18:57:04 +00001057 ArgInfo.Name = Tok.getIdentifierInfo();
1058 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001059 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnere294d3f2009-04-11 18:57:04 +00001061 ArgInfos.push_back(ArgInfo);
1062 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001063 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001064
John McCall0b7e6782011-03-24 11:26:52 +00001065 // Make sure the attributes persist.
1066 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1067
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001068 // Code completion for the next piece of the selector.
1069 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001070 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1071 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001072 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001073 ReturnType,
1074 KeyIdents.data(),
1075 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001076 cutOffParsing();
1077 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001078 }
1079
Chris Lattnerff384912007-10-07 02:00:24 +00001080 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001081 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001082 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001083 break;
1084 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Steve Naroff335eafa2007-11-15 12:35:21 +00001087 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattnerff384912007-10-07 02:00:24 +00001089 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001091 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001092 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001093 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001094 ConsumeToken();
1095 break;
1096 }
John McCall0b7e6782011-03-24 11:26:52 +00001097 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001098 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001099 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001100 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1101 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001102 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001103 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001104 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1105 ParmDecl.getIdentifierLoc(),
1106 Param,
1107 0));
1108
Chris Lattnerff384912007-10-07 02:00:24 +00001109 }
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001111 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001112 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001113 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001114 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001115
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001116 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001117 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001118
Chris Lattnerff384912007-10-07 02:00:24 +00001119 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1120 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001121 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001122 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001123 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001124 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001125 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001126 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001127 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001128
John McCall54abf7d2009-11-04 02:18:39 +00001129 PD.complete(Result);
1130 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001131}
1132
Steve Naroffdac269b2007-08-20 21:31:48 +00001133/// objc-protocol-refs:
1134/// '<' identifier-list '>'
1135///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001136bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001137ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1138 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001139 bool WarnOnDeclarations,
1140 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001141 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001143 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattner5f9e2722011-07-23 10:55:15 +00001145 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Chris Lattnere13b9592008-07-26 04:03:38 +00001147 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001148 if (Tok.is(tok::code_completion)) {
1149 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1150 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001151 cutOffParsing();
1152 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001153 }
1154
Chris Lattnere13b9592008-07-26 04:03:38 +00001155 if (Tok.isNot(tok::identifier)) {
1156 Diag(Tok, diag::err_expected_ident);
1157 SkipUntil(tok::greater);
1158 return true;
1159 }
1160 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1161 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001162 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001163 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Chris Lattnere13b9592008-07-26 04:03:38 +00001165 if (Tok.isNot(tok::comma))
1166 break;
1167 ConsumeToken();
1168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Chris Lattnere13b9592008-07-26 04:03:38 +00001170 // Consume the '>'.
1171 if (Tok.isNot(tok::greater)) {
1172 Diag(Tok, diag::err_expected_greater);
1173 return true;
1174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Chris Lattnere13b9592008-07-26 04:03:38 +00001176 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Chris Lattnere13b9592008-07-26 04:03:38 +00001178 // Convert the list of protocols identifiers into a list of protocol decls.
1179 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1180 &ProtocolIdents[0], ProtocolIdents.size(),
1181 Protocols);
1182 return false;
1183}
1184
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001185/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1186/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001187bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001188 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1189 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1190 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001191 SmallVector<Decl *, 8> ProtocolDecl;
1192 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001193 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1194 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001195 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1196 ProtocolLocs.data(), LAngleLoc);
1197 if (EndProtoLoc.isValid())
1198 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001199 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001200}
1201
1202
Steve Naroffdac269b2007-08-20 21:31:48 +00001203/// objc-class-instance-variables:
1204/// '{' objc-instance-variable-decl-list[opt] '}'
1205///
1206/// objc-instance-variable-decl-list:
1207/// objc-visibility-spec
1208/// objc-instance-variable-decl ';'
1209/// ';'
1210/// objc-instance-variable-decl-list objc-visibility-spec
1211/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1212/// objc-instance-variable-decl-list ';'
1213///
1214/// objc-visibility-spec:
1215/// @private
1216/// @protected
1217/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001218/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001219///
1220/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001221/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001222///
John McCalld226f652010-08-21 09:40:31 +00001223void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001224 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001225 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001226 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001227 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001228
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001229 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001230 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001231
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001232 BalancedDelimiterTracker T(*this, tok::l_brace);
1233 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Steve Naroffddbff782007-08-21 21:17:12 +00001235 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001236 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001237 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Steve Naroffddbff782007-08-21 21:17:12 +00001239 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001240 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001241 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001242 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001243 ConsumeToken();
1244 continue;
1245 }
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Steve Naroffddbff782007-08-21 21:17:12 +00001247 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001248 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001249 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001250
1251 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001252 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001253 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001254 }
1255
Steve Naroff861cf3e2007-08-23 18:16:40 +00001256 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001257 case tok::objc_private:
1258 case tok::objc_public:
1259 case tok::objc_protected:
1260 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001261 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001262 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001263 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001264 default:
1265 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001266 continue;
1267 }
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001270 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001271 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001272 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001273 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001274 }
1275
John McCallbdd563e2009-11-03 02:38:08 +00001276 struct ObjCIvarCallback : FieldCallback {
1277 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001278 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001279 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001280 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001281
John McCalld226f652010-08-21 09:40:31 +00001282 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001283 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001284 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1285 }
1286
John McCalld226f652010-08-21 09:40:31 +00001287 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001288 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001289 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001290 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001291 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001292 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001293 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001294 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001295 if (Field)
1296 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001297 return Field;
1298 }
1299 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001300
Chris Lattnere1359422008-04-10 06:46:29 +00001301 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001302 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001303 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Chris Lattnerdf195262007-10-09 17:51:17 +00001305 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001306 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001307 } else {
1308 Diag(Tok, diag::err_expected_semi_decl_list);
1309 // Skip to end of block or statement
1310 SkipUntil(tok::r_brace, true, true);
1311 }
1312 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001313 T.consumeClose();
1314
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001315 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001316 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001317 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001318 // Call ActOnFields() even if we don't have any decls. This is useful
1319 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001320 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001321 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001322 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001323 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001324}
Steve Naroffdac269b2007-08-20 21:31:48 +00001325
1326/// objc-protocol-declaration:
1327/// objc-protocol-definition
1328/// objc-protocol-forward-reference
1329///
1330/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001331/// @protocol identifier
1332/// objc-protocol-refs[opt]
1333/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001334/// @end
1335///
1336/// objc-protocol-forward-reference:
1337/// @protocol identifier-list ';'
1338///
1339/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001340/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001341/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001342Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001343 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001344 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001345 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1346 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Douglas Gregor083128f2009-11-18 04:49:41 +00001348 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001349 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001350 cutOffParsing();
1351 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001352 }
1353
Chris Lattnerdf195262007-10-09 17:51:17 +00001354 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001355 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001356 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001357 }
1358 // Save the protocol name, then consume it.
1359 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1360 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Chris Lattnerdf195262007-10-09 17:51:17 +00001362 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001363 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001364 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001365 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001366 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001367 }
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001369 CheckNestedObjCContexts(AtLoc);
1370
Chris Lattnerdf195262007-10-09 17:51:17 +00001371 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001372 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001373 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1374
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001375 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001376 while (1) {
1377 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001378 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001379 Diag(Tok, diag::err_expected_ident);
1380 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001381 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001382 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001383 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1384 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001385 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Chris Lattnerdf195262007-10-09 17:51:17 +00001387 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001388 break;
1389 }
1390 // Consume the ';'.
1391 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001392 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Steve Naroffe440eb82007-10-10 17:32:04 +00001394 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001395 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001396 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001397 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001398 }
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001400 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001401 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001402
Chris Lattner5f9e2722011-07-23 10:55:15 +00001403 SmallVector<Decl *, 8> ProtocolRefs;
1404 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001405 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001406 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1407 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001408 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
John McCalld226f652010-08-21 09:40:31 +00001410 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001411 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001412 ProtocolRefs.data(),
1413 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001414 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001415 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001416
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001417 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001418 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001419}
Steve Naroffdac269b2007-08-20 21:31:48 +00001420
1421/// objc-implementation:
1422/// objc-class-implementation-prologue
1423/// objc-category-implementation-prologue
1424///
1425/// objc-class-implementation-prologue:
1426/// @implementation identifier objc-superclass[opt]
1427/// objc-class-instance-variables[opt]
1428///
1429/// objc-category-implementation-prologue:
1430/// @implementation identifier ( identifier )
Erik Verbruggend64251f2011-12-06 09:25:23 +00001431Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001432 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1433 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001434 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001435 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001437 // Code completion after '@implementation'.
1438 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001439 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001440 cutOffParsing();
1441 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001442 }
1443
Chris Lattnerdf195262007-10-09 17:51:17 +00001444 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001445 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001446 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001447 }
1448 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001449 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001450 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001451
1452 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001453 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001454 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001455 SourceLocation categoryLoc, rparenLoc;
1456 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001458 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001459 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001460 cutOffParsing();
1461 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001462 }
1463
Chris Lattnerdf195262007-10-09 17:51:17 +00001464 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001465 categoryId = Tok.getIdentifierInfo();
1466 categoryLoc = ConsumeToken();
1467 } else {
1468 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001469 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001470 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001471 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001472 Diag(Tok, diag::err_expected_rparen);
1473 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001474 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001475 }
1476 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001477 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001478 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001479 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001480
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001481 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001482 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001483 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001484 }
1485 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001486 SourceLocation superClassLoc;
1487 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001488 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001489 // We have a super class
1490 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001491 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001492 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001493 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001494 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001495 superClassId = Tok.getIdentifierInfo();
1496 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001497 }
John McCalld226f652010-08-21 09:40:31 +00001498 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001499 AtLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001500 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Steve Naroff60fccee2007-10-29 21:38:07 +00001502 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggend64251f2011-12-06 09:25:23 +00001503 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001504
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001505 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001506 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001507 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001508}
Steve Naroff60fccee2007-10-29 21:38:07 +00001509
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001510Parser::DeclGroupPtrTy
1511Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001512 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1513 "ParseObjCAtEndDeclaration(): Expected @end");
1514 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001515 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001516 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001517 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1518 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001519 if (D)
1520 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001521 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001522 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001523
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001524 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001525 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001526 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001527 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001528 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001529 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001530 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001531
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001532 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001533 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001534 return Actions.BuildDeclaratorGroup(
1535 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001536}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001537
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001538Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1539 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001540 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001541 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggend64251f2011-12-06 09:25:23 +00001542
John McCalld226f652010-08-21 09:40:31 +00001543 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggend64251f2011-12-06 09:25:23 +00001544 Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1545 Diag(Tok, diag::err_objc_missing_end)
1546 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1547 if (ImpDecl)
1548 Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1549 << Sema::OCK_Implementation;
1550
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001551 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1552}
1553
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001554void Parser::clearLateParsedObjCMethods() {
1555 for (LateParsedObjCMethodContainer::iterator
1556 I = LateParsedObjCMethods.begin(),
1557 E = LateParsedObjCMethods.end(); I != E; ++I)
1558 delete *I;
1559 LateParsedObjCMethods.clear();
1560}
1561
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001562/// compatibility-alias-decl:
1563/// @compatibility_alias alias-name class-name ';'
1564///
John McCalld226f652010-08-21 09:40:31 +00001565Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001566 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1567 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1568 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001569 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001570 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001571 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001572 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001573 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1574 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001575 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001576 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001577 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001578 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001579 IdentifierInfo *classId = Tok.getIdentifierInfo();
1580 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001581 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1582 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001583 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1584 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001585}
1586
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001587/// property-synthesis:
1588/// @synthesize property-ivar-list ';'
1589///
1590/// property-ivar-list:
1591/// property-ivar
1592/// property-ivar-list ',' property-ivar
1593///
1594/// property-ivar:
1595/// identifier
1596/// identifier '=' identifier
1597///
John McCalld226f652010-08-21 09:40:31 +00001598Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001599 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1600 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001601 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregorb328c422009-11-18 19:45:45 +00001603 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001604 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001605 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001606 cutOffParsing();
1607 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001608 }
1609
Douglas Gregorb328c422009-11-18 19:45:45 +00001610 if (Tok.isNot(tok::identifier)) {
1611 Diag(Tok, diag::err_synthesized_property_name);
1612 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001613 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001614 }
1615
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001616 IdentifierInfo *propertyIvar = 0;
1617 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1618 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001619 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001620 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001621 // property '=' ivar-name
1622 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001623
1624 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001625 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001626 cutOffParsing();
1627 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001628 }
1629
Chris Lattnerdf195262007-10-09 17:51:17 +00001630 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001631 Diag(Tok, diag::err_expected_ident);
1632 break;
1633 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001634 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001635 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001636 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001637 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001638 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001639 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001640 break;
1641 ConsumeToken(); // consume ','
1642 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001643 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001644 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001645}
1646
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001647/// property-dynamic:
1648/// @dynamic property-list
1649///
1650/// property-list:
1651/// identifier
1652/// property-list ',' identifier
1653///
John McCalld226f652010-08-21 09:40:31 +00001654Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001655 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1656 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001657 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001658 while (true) {
1659 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001660 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001661 cutOffParsing();
1662 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001663 }
1664
1665 if (Tok.isNot(tok::identifier)) {
1666 Diag(Tok, diag::err_expected_ident);
1667 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001668 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001669 }
1670
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001671 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1672 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001673 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001674 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001675
Chris Lattnerdf195262007-10-09 17:51:17 +00001676 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001677 break;
1678 ConsumeToken(); // consume ','
1679 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001680 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001681 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001682}
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001684/// objc-throw-statement:
1685/// throw expression[opt];
1686///
John McCall60d7b3a2010-08-24 06:29:42 +00001687StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1688 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001689 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001690 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001691 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001692 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001693 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001694 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001695 }
1696 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001697 // consume ';'
1698 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001699 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001700}
1701
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001702/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001703/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001704///
John McCall60d7b3a2010-08-24 06:29:42 +00001705StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001706Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001707 ConsumeToken(); // consume synchronized
1708 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001709 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001710 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001711 }
John McCall07524032011-07-27 21:50:02 +00001712
1713 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001714 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001715 ExprResult operand(ParseExpression());
1716
1717 if (Tok.is(tok::r_paren)) {
1718 ConsumeParen(); // ')'
1719 } else {
1720 if (!operand.isInvalid())
1721 Diag(Tok, diag::err_expected_rparen);
1722
1723 // Skip forward until we see a left brace, but don't consume it.
1724 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001725 }
John McCall07524032011-07-27 21:50:02 +00001726
1727 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001728 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001729 if (!operand.isInvalid())
1730 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001731 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001732 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001733
John McCall07524032011-07-27 21:50:02 +00001734 // Check the @synchronized operand now.
1735 if (!operand.isInvalid())
1736 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001737
John McCall07524032011-07-27 21:50:02 +00001738 // Parse the compound statement within a new scope.
1739 ParseScope bodyScope(this, Scope::DeclScope);
1740 StmtResult body(ParseCompoundStatementBody());
1741 bodyScope.Exit();
1742
1743 // If there was a semantic or parse error earlier with the
1744 // operand, fail now.
1745 if (operand.isInvalid())
1746 return StmtError();
1747
1748 if (body.isInvalid())
1749 body = Actions.ActOnNullStmt(Tok.getLocation());
1750
1751 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001752}
1753
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001754/// objc-try-catch-statement:
1755/// @try compound-statement objc-catch-list[opt]
1756/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1757///
1758/// objc-catch-list:
1759/// @catch ( parameter-declaration ) compound-statement
1760/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1761/// catch-parameter-declaration:
1762/// parameter-declaration
1763/// '...' [OBJC2]
1764///
John McCall60d7b3a2010-08-24 06:29:42 +00001765StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001766 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001767
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001768 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001769 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001770 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001771 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001772 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001773 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001774 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001775 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001776 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001777 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001778 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001779 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001780
Chris Lattnerdf195262007-10-09 17:51:17 +00001781 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001782 // At this point, we need to lookahead to determine if this @ is the start
1783 // of an @catch or @finally. We don't want to consume the @ token if this
1784 // is an @try or @encode or something else.
1785 Token AfterAt = GetLookAheadToken(1);
1786 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1787 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1788 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001789
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001790 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001791 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001792 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001793 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001794 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001795 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001796 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001797 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001798 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001799 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001800 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001801 ParseDeclarator(ParmDecl);
1802
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001803 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001804 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001805 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001806 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001807 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Steve Naroff93a25952009-04-07 22:56:58 +00001809 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Steve Naroff93a25952009-04-07 22:56:58 +00001811 if (Tok.is(tok::r_paren))
1812 RParenLoc = ConsumeParen();
1813 else // Skip over garbage, until we get to ')'. Eat the ')'.
1814 SkipUntil(tok::r_paren, true, false);
1815
John McCall60d7b3a2010-08-24 06:29:42 +00001816 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001817 if (Tok.is(tok::l_brace))
1818 CatchBody = ParseCompoundStatementBody();
1819 else
1820 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001821 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001822 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001823
John McCall60d7b3a2010-08-24 06:29:42 +00001824 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001825 RParenLoc,
1826 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001827 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001828 if (!Catch.isInvalid())
1829 CatchStmts.push_back(Catch.release());
1830
Steve Naroff64515f32008-02-05 21:27:35 +00001831 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001832 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1833 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001834 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001835 }
1836 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001837 } else {
1838 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001839 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001840 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001841
John McCall60d7b3a2010-08-24 06:29:42 +00001842 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001843 if (Tok.is(tok::l_brace))
1844 FinallyBody = ParseCompoundStatementBody();
1845 else
1846 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001847 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001848 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001849 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001850 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001851 catch_or_finally_seen = true;
1852 break;
1853 }
1854 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001855 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001856 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001857 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001858 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001859
John McCall9ae2f072010-08-23 23:25:46 +00001860 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001861 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001862 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001863}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001864
John McCallf85e1932011-06-15 23:02:42 +00001865/// objc-autoreleasepool-statement:
1866/// @autoreleasepool compound-statement
1867///
1868StmtResult
1869Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1870 ConsumeToken(); // consume autoreleasepool
1871 if (Tok.isNot(tok::l_brace)) {
1872 Diag(Tok, diag::err_expected_lbrace);
1873 return StmtError();
1874 }
1875 // Enter a scope to hold everything within the compound stmt. Compound
1876 // statements can always hold declarations.
1877 ParseScope BodyScope(this, Scope::DeclScope);
1878
1879 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1880
1881 BodyScope.Exit();
1882 if (AutoreleasePoolBody.isInvalid())
1883 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1884 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1885 AutoreleasePoolBody.take());
1886}
1887
Steve Naroff3536b442007-09-06 21:24:23 +00001888/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001889///
John McCalld226f652010-08-21 09:40:31 +00001890Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001891 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001892
John McCallf312b1e2010-08-26 23:41:50 +00001893 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1894 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001896 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001897 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001898 if (ObjCImpDecl) {
1899 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001900 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001901 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001902 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001903 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001904
Steve Naroff409be832007-11-11 19:54:21 +00001905 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001906 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001907 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Steve Naroff409be832007-11-11 19:54:21 +00001909 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1910 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Steve Naroff409be832007-11-11 19:54:21 +00001912 // If we didn't find the '{', bail out.
1913 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001914 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001915 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001916 // Allow the rest of sema to find private method decl implementations.
1917 if (MDecl)
1918 Actions.AddAnyMethodToGlobalPool(MDecl);
1919
1920 // Consume the tokens and store them for later parsing.
1921 LexedMethod* LM = new LexedMethod(this, MDecl);
1922 LateParsedObjCMethods.push_back(LM);
1923 CachedTokens &Toks = LM->Toks;
1924 // Begin by storing the '{' token.
1925 Toks.push_back(Tok);
1926 ConsumeBrace();
1927 // Consume everything up to (and including) the matching right brace.
1928 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001929 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001930}
Anders Carlsson55085182007-08-21 17:43:55 +00001931
John McCall60d7b3a2010-08-24 06:29:42 +00001932StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001933 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001934 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001935 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001936 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001937 }
1938
1939 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001940 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001941
1942 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001943 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001944
1945 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001946 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001947
1948 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1949 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001950
John McCall60d7b3a2010-08-24 06:29:42 +00001951 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001952 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001953 // If the expression is invalid, skip ahead to the next semicolon. Not
1954 // doing this opens us up to the possibility of infinite loops if
1955 // ParseExpression does not consume any tokens.
1956 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001957 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001958 }
Chris Lattner5d803162009-12-07 16:33:19 +00001959
Steve Naroff64515f32008-02-05 21:27:35 +00001960 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001961 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001962 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001963}
1964
John McCall60d7b3a2010-08-24 06:29:42 +00001965ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001966 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001967 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001968 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001969 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001970 return ExprError();
1971
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001972 case tok::string_literal: // primary-expression: string-literal
1973 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001974 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001975 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001976 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001977 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001978
Chris Lattner4fef81d2008-08-05 06:19:09 +00001979 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1980 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001981 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001982 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001983 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001984 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001985 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001986 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001987 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001988 }
Anders Carlsson55085182007-08-21 17:43:55 +00001989 }
Anders Carlsson55085182007-08-21 17:43:55 +00001990}
1991
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001992/// \brirg Parse the receiver of an Objective-C++ message send.
1993///
1994/// This routine parses the receiver of a message send in
1995/// Objective-C++ either as a type or as an expression. Note that this
1996/// routine must not be called to parse a send to 'super', since it
1997/// has no way to return such a result.
1998///
1999/// \param IsExpr Whether the receiver was parsed as an expression.
2000///
2001/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2002/// IsExpr is true), the parsed expression. If the receiver was parsed
2003/// as a type (\c IsExpr is false), the parsed type.
2004///
2005/// \returns True if an error occurred during parsing or semantic
2006/// analysis, in which case the arguments do not have valid
2007/// values. Otherwise, returns false for a successful parse.
2008///
2009/// objc-receiver: [C++]
2010/// 'super' [not parsed here]
2011/// expression
2012/// simple-type-specifier
2013/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002014bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002015 InMessageExpressionRAIIObject InMessage(*this, true);
2016
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002017 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2018 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2019 TryAnnotateTypeOrScopeToken();
2020
2021 if (!isCXXSimpleTypeSpecifier()) {
2022 // objc-receiver:
2023 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002024 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002025 if (Receiver.isInvalid())
2026 return true;
2027
2028 IsExpr = true;
2029 TypeOrExpr = Receiver.take();
2030 return false;
2031 }
2032
2033 // objc-receiver:
2034 // typename-specifier
2035 // simple-type-specifier
2036 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002037 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002038 ParseCXXSimpleTypeSpecifier(DS);
2039
2040 if (Tok.is(tok::l_paren)) {
2041 // If we see an opening parentheses at this point, we are
2042 // actually parsing an expression that starts with a
2043 // function-style cast, e.g.,
2044 //
2045 // postfix-expression:
2046 // simple-type-specifier ( expression-list [opt] )
2047 // typename-specifier ( expression-list [opt] )
2048 //
2049 // Parse the remainder of this case, then the (optional)
2050 // postfix-expression suffix, followed by the (optional)
2051 // right-hand side of the binary expression. We have an
2052 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002053 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002054 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002055 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002056 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002057 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002058 if (Receiver.isInvalid())
2059 return true;
2060
2061 IsExpr = true;
2062 TypeOrExpr = Receiver.take();
2063 return false;
2064 }
2065
2066 // We have a class message. Turn the simple-type-specifier or
2067 // typename-specifier we parsed into a type and parse the
2068 // remainder of the class message.
2069 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002070 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002071 if (Type.isInvalid())
2072 return true;
2073
2074 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002075 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002076 return false;
2077}
2078
Douglas Gregor1b730e82010-05-31 14:40:22 +00002079/// \brief Determine whether the parser is currently referring to a an
2080/// Objective-C message send, using a simplified heuristic to avoid overhead.
2081///
2082/// This routine will only return true for a subset of valid message-send
2083/// expressions.
2084bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002085 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002086 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002087 return GetLookAheadToken(1).is(tok::identifier) &&
2088 GetLookAheadToken(2).is(tok::identifier);
2089}
2090
Douglas Gregor9497a732010-09-16 01:51:54 +00002091bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2092 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2093 InMessageExpression)
2094 return false;
2095
2096
2097 ParsedType Type;
2098
2099 if (Tok.is(tok::annot_typename))
2100 Type = getTypeAnnotation(Tok);
2101 else if (Tok.is(tok::identifier))
2102 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2103 getCurScope());
2104 else
2105 return false;
2106
2107 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2108 const Token &AfterNext = GetLookAheadToken(2);
2109 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2110 if (Tok.is(tok::identifier))
2111 TryAnnotateTypeOrScopeToken();
2112
2113 return Tok.is(tok::annot_typename);
2114 }
2115 }
2116
2117 return false;
2118}
2119
Mike Stump1eb44332009-09-09 15:08:12 +00002120/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002121/// '[' objc-receiver objc-message-args ']'
2122///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002123/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002124/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002125/// expression
2126/// class-name
2127/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002128///
John McCall60d7b3a2010-08-24 06:29:42 +00002129ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002130 assert(Tok.is(tok::l_square) && "'[' expected");
2131 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2132
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002133 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002134 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002135 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002136 return ExprError();
2137 }
2138
Douglas Gregor0fbda682010-09-15 14:51:05 +00002139 InMessageExpressionRAIIObject InMessage(*this, true);
2140
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002141 if (getLang().CPlusPlus) {
2142 // We completely separate the C and C++ cases because C++ requires
2143 // more complicated (read: slower) parsing.
2144
2145 // Handle send to super.
2146 // FIXME: This doesn't benefit from the same typo-correction we
2147 // get in Objective-C.
2148 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002149 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002150 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2151 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002152
2153 // Parse the receiver, which is either a type or an expression.
2154 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002155 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002156 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2157 SkipUntil(tok::r_square);
2158 return ExprError();
2159 }
2160
2161 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002162 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2163 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002164 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002165
2166 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002167 ParsedType::getFromOpaquePtr(TypeOrExpr),
2168 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002169 }
2170
2171 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002172 IdentifierInfo *Name = Tok.getIdentifierInfo();
2173 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002174 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002175 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002176 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002177 NextToken().is(tok::period),
2178 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002179 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002180 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2181 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002182
John McCallf312b1e2010-08-26 23:41:50 +00002183 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002184 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002185 SkipUntil(tok::r_square);
2186 return ExprError();
2187 }
2188
Douglas Gregor1569f952010-04-21 20:38:13 +00002189 ConsumeToken(); // the type name
2190
2191 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002192 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002193
John McCallf312b1e2010-08-26 23:41:50 +00002194 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002195 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002196 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002197 }
Chris Lattner699b6612008-01-25 18:59:06 +00002198 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002199
2200 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002201 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002202 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002203 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002204 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002205 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002206
John McCallb3d87482010-08-24 05:47:05 +00002207 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2208 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002209}
Sebastian Redl1d922962008-12-13 15:32:12 +00002210
Douglas Gregor2725ca82010-04-21 19:57:20 +00002211/// \brief Parse the remainder of an Objective-C message following the
2212/// '[' objc-receiver.
2213///
2214/// This routine handles sends to super, class messages (sent to a
2215/// class name), and instance messages (sent to an object), and the
2216/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2217/// ReceiverExpr, respectively. Only one of these parameters may have
2218/// a valid value.
2219///
2220/// \param LBracLoc The location of the opening '['.
2221///
2222/// \param SuperLoc If this is a send to 'super', the location of the
2223/// 'super' keyword that indicates a send to the superclass.
2224///
2225/// \param ReceiverType If this is a class message, the type of the
2226/// class we are sending a message to.
2227///
2228/// \param ReceiverExpr If this is an instance message, the expression
2229/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002230///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002231/// objc-message-args:
2232/// objc-selector
2233/// objc-keywordarg-list
2234///
2235/// objc-keywordarg-list:
2236/// objc-keywordarg
2237/// objc-keywordarg-list objc-keywordarg
2238///
Mike Stump1eb44332009-09-09 15:08:12 +00002239/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002240/// selector-name[opt] ':' objc-keywordexpr
2241///
2242/// objc-keywordexpr:
2243/// nonempty-expr-list
2244///
2245/// nonempty-expr-list:
2246/// assignment-expression
2247/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002248///
John McCall60d7b3a2010-08-24 06:29:42 +00002249ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002250Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002251 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002252 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002253 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002254 InMessageExpressionRAIIObject InMessage(*this, true);
2255
Steve Naroffc4df6d22009-11-07 02:08:14 +00002256 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002257 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002258 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2259 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002260 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002261 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2262 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002263 else
John McCall9ae2f072010-08-23 23:25:46 +00002264 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002265 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002266 cutOffParsing();
2267 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002268 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002269
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002270 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002271 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002272 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002273
Chris Lattner5f9e2722011-07-23 10:55:15 +00002274 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002275 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002276 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002277
Chris Lattnerdf195262007-10-09 17:51:17 +00002278 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002279 while (1) {
2280 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002281 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002282 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002283
Chris Lattnerdf195262007-10-09 17:51:17 +00002284 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002285 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002286 // We must manually skip to a ']', otherwise the expression skipper will
2287 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2288 // the enclosing expression.
2289 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002290 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002291 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002292
Steve Naroff68d331a2007-09-27 14:38:14 +00002293 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002294 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002295
2296 if (Tok.is(tok::code_completion)) {
2297 if (SuperLoc.isValid())
2298 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2299 KeyIdents.data(),
2300 KeyIdents.size(),
2301 /*AtArgumentEpression=*/true);
2302 else if (ReceiverType)
2303 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2304 KeyIdents.data(),
2305 KeyIdents.size(),
2306 /*AtArgumentEpression=*/true);
2307 else
2308 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2309 KeyIdents.data(),
2310 KeyIdents.size(),
2311 /*AtArgumentEpression=*/true);
2312
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002313 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002314 return ExprError();
2315 }
2316
John McCall60d7b3a2010-08-24 06:29:42 +00002317 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002318 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002319 // We must manually skip to a ']', otherwise the expression skipper will
2320 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2321 // the enclosing expression.
2322 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002323 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002324 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002325
Steve Naroff37387c92007-09-17 20:25:27 +00002326 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002327 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002328
Douglas Gregord3c68542009-11-19 01:08:35 +00002329 // Code completion after each argument.
2330 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002331 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002332 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002333 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002334 KeyIdents.size(),
2335 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002336 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002337 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002338 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002339 KeyIdents.size(),
2340 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002341 else
John McCall9ae2f072010-08-23 23:25:46 +00002342 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002343 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002344 KeyIdents.size(),
2345 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002346 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002347 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002348 }
2349
Steve Naroff37387c92007-09-17 20:25:27 +00002350 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002351 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002352 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002353 break;
2354 // We have a selector or a colon, continue parsing.
2355 }
2356 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002357 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002358 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002359 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002360 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002361 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002362 // We must manually skip to a ']', otherwise the expression skipper will
2363 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2364 // the enclosing expression.
2365 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002366 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002367 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002368
Steve Naroff49f109c2007-11-15 13:05:42 +00002369 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002370 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002371 }
2372 } else if (!selIdent) {
2373 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002374
Chris Lattner4fef81d2008-08-05 06:19:09 +00002375 // We must manually skip to a ']', otherwise the expression skipper will
2376 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2377 // the enclosing expression.
2378 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002379 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002380 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002381
Chris Lattnerdf195262007-10-09 17:51:17 +00002382 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002383 if (Tok.is(tok::identifier))
2384 Diag(Tok, diag::err_expected_colon);
2385 else
2386 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002387 // We must manually skip to a ']', otherwise the expression skipper will
2388 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2389 // the enclosing expression.
2390 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002391 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002392 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002393
Chris Lattner699b6612008-01-25 18:59:06 +00002394 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002395
Steve Naroff29238a02007-10-05 18:42:47 +00002396 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002397 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002398 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002399 KeyLocs.push_back(Loc);
2400 }
Chris Lattnerff384912007-10-07 02:00:24 +00002401 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002402
Douglas Gregor2725ca82010-04-21 19:57:20 +00002403 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002404 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002405 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002406 MultiExprArg(Actions,
2407 KeyExprs.take(),
2408 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002409 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002410 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002411 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002412 MultiExprArg(Actions,
2413 KeyExprs.take(),
2414 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002415 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, 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()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002420}
2421
John McCall60d7b3a2010-08-24 06:29:42 +00002422ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2423 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002424 if (Res.isInvalid()) return move(Res);
2425
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002426 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2427 // expressions. At this point, we know that the only valid thing that starts
2428 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002429 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002430 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002431 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002432 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002433
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002434 while (Tok.is(tok::at)) {
2435 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002436
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002437 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002438 if (!isTokenStringLiteral())
2439 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002440
John McCall60d7b3a2010-08-24 06:29:42 +00002441 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002442 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002443 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002444
Sebastian Redleffa8d12008-12-10 00:02:53 +00002445 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002446 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002447
2448 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2449 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002450}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002451
2452/// objc-encode-expression:
2453/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002454ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002455Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002456 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002457
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002458 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002459
Chris Lattner4fef81d2008-08-05 06:19:09 +00002460 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002461 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2462
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002463 BalancedDelimiterTracker T(*this, tok::l_paren);
2464 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002465
Douglas Gregor809070a2009-02-18 17:45:20 +00002466 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002467
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002468 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002469
Douglas Gregor809070a2009-02-18 17:45:20 +00002470 if (Ty.isInvalid())
2471 return ExprError();
2472
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002473 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2474 T.getOpenLocation(), Ty.get(),
2475 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002476}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002477
2478/// objc-protocol-expression
2479/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002480ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002481Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002482 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002483
Chris Lattner4fef81d2008-08-05 06:19:09 +00002484 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002485 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2486
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002487 BalancedDelimiterTracker T(*this, tok::l_paren);
2488 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002489
Chris Lattner4fef81d2008-08-05 06:19:09 +00002490 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002491 return ExprError(Diag(Tok, diag::err_expected_ident));
2492
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002493 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002494 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002495
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002496 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002497
Sebastian Redl1d922962008-12-13 15:32:12 +00002498 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002499 T.getOpenLocation(),
2500 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002501}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002502
2503/// objc-selector-expression
2504/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002505ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002506 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002507
Chris Lattner4fef81d2008-08-05 06:19:09 +00002508 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002509 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2510
Chris Lattner5f9e2722011-07-23 10:55:15 +00002511 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002512 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002513
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002514 BalancedDelimiterTracker T(*this, tok::l_paren);
2515 T.consumeOpen();
2516
Douglas Gregor458433d2010-08-26 15:07:07 +00002517 if (Tok.is(tok::code_completion)) {
2518 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2519 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002520 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002521 return ExprError();
2522 }
2523
Chris Lattner2fc5c242009-04-11 18:13:45 +00002524 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002525 if (!SelIdent && // missing selector name.
2526 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002527 return ExprError(Diag(Tok, diag::err_expected_ident));
2528
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002529 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002530 unsigned nColons = 0;
2531 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002532 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002533 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2534 ++nColons;
2535 KeyIdents.push_back(0);
2536 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002537 return ExprError(Diag(Tok, diag::err_expected_colon));
2538
Chris Lattner5add7542010-08-27 22:32:41 +00002539 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002540 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002541 if (Tok.is(tok::r_paren))
2542 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002543
2544 if (Tok.is(tok::code_completion)) {
2545 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2546 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002547 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002548 return ExprError();
2549 }
2550
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002551 // Check for another keyword selector.
2552 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002553 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002554 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002555 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002556 break;
2557 }
Steve Naroff887407e2007-12-05 22:21:29 +00002558 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002559 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002560 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002561 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002562 T.getOpenLocation(),
2563 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002564 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002565
2566Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002567
2568 // Save the current token position.
2569 SourceLocation OrigLoc = Tok.getLocation();
2570
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002571 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2572 // Append the current token at the end of the new token stream so that it
2573 // doesn't get lost.
2574 LM.Toks.push_back(Tok);
2575 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2576
2577 // MDecl might be null due to error in method prototype, etc.
2578 Decl *MDecl = LM.D;
2579 // Consume the previously pushed token.
2580 ConsumeAnyToken();
2581
2582 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2583 SourceLocation BraceLoc = Tok.getLocation();
2584 // Enter a scope for the method body.
2585 ParseScope BodyScope(this,
2586 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2587
2588 // Tell the actions module that we have entered a method definition with the
2589 // specified Declarator for the method.
2590 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2591
2592 if (PP.isCodeCompletionEnabled()) {
2593 if (trySkippingFunctionBodyForCodeCompletion()) {
2594 BodyScope.Exit();
2595 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2596 }
2597 }
2598
2599 StmtResult FnBody(ParseCompoundStatementBody());
2600
2601 // If the function body could not be parsed, make a bogus compoundstmt.
2602 if (FnBody.isInvalid())
2603 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2604 MultiStmtArg(Actions), false);
2605
2606 // Leave the function body scope.
2607 BodyScope.Exit();
2608
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002609 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2610
2611 if (Tok.getLocation() != OrigLoc) {
2612 // Due to parsing error, we either went over the cached tokens or
2613 // there are still cached tokens left. If it's the latter case skip the
2614 // leftover tokens.
2615 // Since this is an uncommon situation that should be avoided, use the
2616 // expensive isBeforeInTranslationUnit call.
2617 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2618 OrigLoc))
2619 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2620 ConsumeAnyToken();
2621 }
2622
2623 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002624}