blob: 1f20924c9b7532a5893070035e714eed647e4a29 [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.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000369 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
370 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
371 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
372 if (Tok.is(tok::semi))
373 ConsumeToken();
374 }
Steve Naroff294494e2007-08-22 16:35:03 +0000375 continue;
376 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000377 if (Tok.is(tok::l_paren)) {
378 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000379 ParseObjCMethodDecl(Tok.getLocation(),
380 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000381 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000382 continue;
383 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000384 // Ignore excess semicolons.
385 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000386 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000387 continue;
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnerbc662af2008-10-20 06:10:06 +0000390 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000391 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000392 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000394 // Code completion within an Objective-C interface.
395 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000396 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000397 ObjCImpDecl? Sema::PCC_ObjCImplementation
398 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000399 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000400 }
401
Chris Lattnere82a10f2008-10-20 05:46:22 +0000402 // If we don't have an @ directive, parse it as a function definition.
403 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000404 // The code below does not consume '}'s because it is afraid of eating the
405 // end of a namespace. Because of the way this code is structured, an
406 // erroneous r_brace would cause an infinite loop if not handled here.
407 if (Tok.is(tok::r_brace))
408 break;
John McCall0b7e6782011-03-24 11:26:52 +0000409 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000410 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000411 continue;
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattnere82a10f2008-10-20 05:46:22 +0000414 // Otherwise, we have an @ directive, eat the @.
415 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000416 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000417 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000418 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000419 break;
420 }
421
Chris Lattnera2449b22008-10-20 05:57:40 +0000422 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnera2449b22008-10-20 05:57:40 +0000424 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000425 AtEnd.setBegin(AtLoc);
426 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000427 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000428 } else if (DirectiveKind == tok::objc_not_keyword) {
429 Diag(Tok, diag::err_objc_unknown_at);
430 SkipUntil(tok::semi);
431 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnerbc662af2008-10-20 06:10:06 +0000434 // Eat the identifier.
435 ConsumeToken();
436
Chris Lattnera2449b22008-10-20 05:57:40 +0000437 switch (DirectiveKind) {
438 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000439 // FIXME: If someone forgets an @end on a protocol, this loop will
440 // continue to eat up tons of stuff and spew lots of nonsense errors. It
441 // would probably be better to bail out if we saw an @class or @interface
442 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000443 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000444 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000445 SkipUntil(tok::r_brace, tok::at);
446 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000447
448 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000449 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000450 Diag(AtLoc, diag::err_objc_missing_end)
451 << FixItHint::CreateInsertion(AtLoc, "@end\n");
452 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
453 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000454 ConsumeToken();
455 break;
456
Chris Lattnera2449b22008-10-20 05:57:40 +0000457 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000458 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000459 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000460 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000461 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000462 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000463 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000464 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000465 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattnera2449b22008-10-20 05:57:40 +0000467 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000468 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000469 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000470
Chris Lattnere82a10f2008-10-20 05:46:22 +0000471 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000472 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000473 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000474 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000476 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000477 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000478
Chris Lattnere82a10f2008-10-20 05:46:22 +0000479 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000480 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000481 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
John McCall7da19ea2011-03-26 01:53:26 +0000483 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000484 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000485 }
Steve Naroff294494e2007-08-22 16:35:03 +0000486 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000487
488 // We break out of the big loop in two cases: when we see @end or when we see
489 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000490 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000491 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000492 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000493 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000494 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000495 } else {
496 Diag(Tok, diag::err_objc_missing_end)
497 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
498 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
499 << (int) Actions.getObjCContainerKind();
500 AtEnd.setBegin(Tok.getLocation());
501 AtEnd.setEnd(Tok.getLocation());
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattnera2449b22008-10-20 05:57:40 +0000504 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000505 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000506 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000507 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000508 allProperties.data(), allProperties.size(),
509 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000510}
511
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000512/// Parse property attribute declarations.
513///
514/// property-attr-decl: '(' property-attrlist ')'
515/// property-attrlist:
516/// property-attribute
517/// property-attrlist ',' property-attribute
518/// property-attribute:
519/// getter '=' identifier
520/// setter '=' identifier ':'
521/// readonly
522/// readwrite
523/// assign
524/// retain
525/// copy
526/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000527/// atomic
528/// strong
529/// weak
530/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000531///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000532void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000533 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000534 BalancedDelimiterTracker T(*this, tok::l_paren);
535 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000537 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000538 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000539 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000540 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000541 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000542 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000544 // If this is not an identifier at all, bail out early.
545 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000546 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000547 return;
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner156b0612008-10-20 07:37:22 +0000550 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner92e62b02008-11-20 04:42:34 +0000552 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000553 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000554 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000555 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000556 else if (II->isStr("unsafe_unretained"))
557 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000558 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000559 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000560 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000561 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000562 else if (II->isStr("strong"))
563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000564 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000566 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000568 else if (II->isStr("atomic"))
569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000570 else if (II->isStr("weak"))
571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000572 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000573 bool IsSetter = II->getNameStart()[0] == 's';
574
Chris Lattnere00da7c2008-10-20 07:39:53 +0000575 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000576 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
577 diag::err_objc_expected_equal_for_getter;
578
579 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000580 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Douglas Gregor4ad96852009-11-19 07:41:15 +0000582 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000583 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000584 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000585 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000586 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000587 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000588 }
589
Anders Carlsson42499be2010-10-02 17:45:21 +0000590
591 SourceLocation SelLoc;
592 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
593
594 if (!SelIdent) {
595 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
596 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000597 SkipUntil(tok::r_paren);
598 return;
599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Anders Carlsson42499be2010-10-02 17:45:21 +0000601 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000602 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000603 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000605 if (ExpectAndConsume(tok::colon,
606 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000607 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000608 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000609 } else {
610 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000611 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000612 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000613 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000614 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000615 SkipUntil(tok::r_paren);
616 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Chris Lattner156b0612008-10-20 07:37:22 +0000619 if (Tok.isNot(tok::comma))
620 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Chris Lattner156b0612008-10-20 07:37:22 +0000622 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000625 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000626}
627
Steve Naroff3536b442007-09-06 21:24:23 +0000628/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000629/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000630/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000631///
632/// objc-instance-method: '-'
633/// objc-class-method: '+'
634///
Steve Naroff4985ace2007-08-22 18:35:33 +0000635/// objc-method-attributes: [OBJC2]
636/// __attribute__((deprecated))
637///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000638Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000639 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000640 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000641
Mike Stump1eb44332009-09-09 15:08:12 +0000642 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000643 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000644 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000645 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000646 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000647 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000648 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000649}
650
651/// objc-selector:
652/// identifier
653/// one of
654/// enum struct union if else while do for switch case default
655/// break continue return goto asm sizeof typeof __alignof
656/// unsigned long const short volatile signed restrict _Complex
657/// in out inout bycopy byref oneway int char float double void _Bool
658///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000659IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000660
Chris Lattnerff384912007-10-07 02:00:24 +0000661 switch (Tok.getKind()) {
662 default:
663 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000664 case tok::ampamp:
665 case tok::ampequal:
666 case tok::amp:
667 case tok::pipe:
668 case tok::tilde:
669 case tok::exclaim:
670 case tok::exclaimequal:
671 case tok::pipepipe:
672 case tok::pipeequal:
673 case tok::caret:
674 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000675 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000676 if (isalpha(ThisTok[0])) {
677 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
678 Tok.setKind(tok::identifier);
679 SelectorLoc = ConsumeToken();
680 return II;
681 }
682 return 0;
683 }
684
Chris Lattnerff384912007-10-07 02:00:24 +0000685 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000686 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000687 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000688 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000689 case tok::kw_break:
690 case tok::kw_case:
691 case tok::kw_catch:
692 case tok::kw_char:
693 case tok::kw_class:
694 case tok::kw_const:
695 case tok::kw_const_cast:
696 case tok::kw_continue:
697 case tok::kw_default:
698 case tok::kw_delete:
699 case tok::kw_do:
700 case tok::kw_double:
701 case tok::kw_dynamic_cast:
702 case tok::kw_else:
703 case tok::kw_enum:
704 case tok::kw_explicit:
705 case tok::kw_export:
706 case tok::kw_extern:
707 case tok::kw_false:
708 case tok::kw_float:
709 case tok::kw_for:
710 case tok::kw_friend:
711 case tok::kw_goto:
712 case tok::kw_if:
713 case tok::kw_inline:
714 case tok::kw_int:
715 case tok::kw_long:
716 case tok::kw_mutable:
717 case tok::kw_namespace:
718 case tok::kw_new:
719 case tok::kw_operator:
720 case tok::kw_private:
721 case tok::kw_protected:
722 case tok::kw_public:
723 case tok::kw_register:
724 case tok::kw_reinterpret_cast:
725 case tok::kw_restrict:
726 case tok::kw_return:
727 case tok::kw_short:
728 case tok::kw_signed:
729 case tok::kw_sizeof:
730 case tok::kw_static:
731 case tok::kw_static_cast:
732 case tok::kw_struct:
733 case tok::kw_switch:
734 case tok::kw_template:
735 case tok::kw_this:
736 case tok::kw_throw:
737 case tok::kw_true:
738 case tok::kw_try:
739 case tok::kw_typedef:
740 case tok::kw_typeid:
741 case tok::kw_typename:
742 case tok::kw_typeof:
743 case tok::kw_union:
744 case tok::kw_unsigned:
745 case tok::kw_using:
746 case tok::kw_virtual:
747 case tok::kw_void:
748 case tok::kw_volatile:
749 case tok::kw_wchar_t:
750 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000751 case tok::kw__Bool:
752 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000753 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000754 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000755 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000756 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000757 }
Steve Naroff294494e2007-08-22 16:35:03 +0000758}
759
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000760/// objc-for-collection-in: 'in'
761///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000762bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000763 // FIXME: May have to do additional look-ahead to only allow for
764 // valid tokens following an 'in'; such as an identifier, unary operators,
765 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000766 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000767 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000768}
769
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000771/// qualifier list and builds their bitmask representation in the input
772/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000773///
774/// objc-type-qualifiers:
775/// objc-type-qualifier
776/// objc-type-qualifiers objc-type-qualifier
777///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000778void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000779 Declarator::TheContext Context) {
780 assert(Context == Declarator::ObjCParameterContext ||
781 Context == Declarator::ObjCResultContext);
782
Chris Lattnere8b724d2007-12-12 06:56:32 +0000783 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000784 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000785 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000786 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000787 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000788 }
789
Chris Lattnercb53b362007-12-27 19:57:00 +0000790 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000791 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattnere8b724d2007-12-12 06:56:32 +0000793 const IdentifierInfo *II = Tok.getIdentifierInfo();
794 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000795 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000796 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000798 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000799 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000800 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
802 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
803 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
804 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
805 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
806 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000807 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000809 ConsumeToken();
810 II = 0;
811 break;
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Chris Lattnere8b724d2007-12-12 06:56:32 +0000814 // If this wasn't a recognized qualifier, bail out.
815 if (II) return;
816 }
817}
818
John McCallcdda47f2011-10-01 09:56:14 +0000819/// Take all the decl attributes out of the given list and add
820/// them to the given attribute set.
821static void takeDeclAttributes(ParsedAttributes &attrs,
822 AttributeList *list) {
823 while (list) {
824 AttributeList *cur = list;
825 list = cur->getNext();
826
827 if (!cur->isUsedAsTypeAttr()) {
828 // Clear out the next pointer. We're really completely
829 // destroying the internal invariants of the declarator here,
830 // but it doesn't matter because we're done with it.
831 cur->setNext(0);
832 attrs.add(cur);
833 }
834 }
835}
836
837/// takeDeclAttributes - Take all the decl attributes from the given
838/// declarator and add them to the given list.
839static void takeDeclAttributes(ParsedAttributes &attrs,
840 Declarator &D) {
841 // First, take ownership of all attributes.
842 attrs.getPool().takeAllFrom(D.getAttributePool());
843 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
844
845 // Now actually move the attributes over.
846 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
847 takeDeclAttributes(attrs, D.getAttributes());
848 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
849 takeDeclAttributes(attrs,
850 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
851}
852
Chris Lattnere8b724d2007-12-12 06:56:32 +0000853/// objc-type-name:
854/// '(' objc-type-qualifiers[opt] type-name ')'
855/// '(' objc-type-qualifiers[opt] ')'
856///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000857ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000858 Declarator::TheContext context,
859 ParsedAttributes *paramAttrs) {
860 assert(context == Declarator::ObjCParameterContext ||
861 context == Declarator::ObjCResultContext);
862 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
863
Chris Lattnerdf195262007-10-09 17:51:17 +0000864 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000866 BalancedDelimiterTracker T(*this, tok::l_paren);
867 T.consumeOpen();
868
Chris Lattnere8904e92008-08-23 01:48:03 +0000869 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000870 ObjCDeclContextSwitch ObjCDC(*this);
871
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000872 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000873 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000874
John McCallb3d87482010-08-24 05:47:05 +0000875 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000876 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000877 // Parse an abstract declarator.
878 DeclSpec declSpec(AttrFactory);
879 declSpec.setObjCQualifiers(&DS);
880 ParseSpecifierQualifierList(declSpec);
881 Declarator declarator(declSpec, context);
882 ParseDeclarator(declarator);
883
884 // If that's not invalid, extract a type.
885 if (!declarator.isInvalidType()) {
886 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
887 if (!type.isInvalid())
888 Ty = type.get();
889
890 // If we're parsing a parameter, steal all the decl attributes
891 // and add them to the decl spec.
892 if (context == Declarator::ObjCParameterContext)
893 takeDeclAttributes(*paramAttrs, declarator);
894 }
895 } else if (context == Declarator::ObjCResultContext &&
896 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000897 if (!Ident_instancetype)
898 Ident_instancetype = PP.getIdentifierInfo("instancetype");
899
900 if (Tok.getIdentifierInfo() == Ident_instancetype) {
901 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
902 ConsumeToken();
903 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000904 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000905
Steve Naroffd7333c22008-10-21 14:15:04 +0000906 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000907 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000908 else if (Tok.getLocation() == TypeStartLoc) {
909 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000910 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000911 SkipUntil(tok::r_paren);
912 } else {
913 // Otherwise, we found *something*, but didn't get a ')' in the right
914 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000915 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000916 }
Steve Narofff28b2642007-09-05 23:30:30 +0000917 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000918}
919
920/// objc-method-decl:
921/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000922/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000923/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000924/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000925///
926/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000927/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000928/// objc-keyword-selector objc-keyword-decl
929///
930/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000931/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
932/// objc-selector ':' objc-keyword-attributes[opt] identifier
933/// ':' objc-type-name objc-keyword-attributes[opt] identifier
934/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000935///
Steve Naroff4985ace2007-08-22 18:35:33 +0000936/// objc-parmlist:
937/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000938///
Steve Naroff4985ace2007-08-22 18:35:33 +0000939/// objc-parms:
940/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000941///
Steve Naroff4985ace2007-08-22 18:35:33 +0000942/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000943/// , ...
944///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000945/// objc-keyword-attributes: [OBJC2]
946/// __attribute__((unused))
947///
John McCalld226f652010-08-21 09:40:31 +0000948Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000949 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000950 tok::ObjCKeywordKind MethodImplKind,
951 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000952 ParsingDeclRAIIObject PD(*this);
953
Douglas Gregore8f5a172010-04-07 00:21:17 +0000954 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000955 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000956 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000957 cutOffParsing();
958 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000959 }
960
Chris Lattnere8904e92008-08-23 01:48:03 +0000961 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000962 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000963 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000964 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000965 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremenek9e049352010-02-18 23:05:16 +0000967 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000968 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000969 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000970 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000971
Douglas Gregore8f5a172010-04-07 00:21:17 +0000972 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000973 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000974 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000975 cutOffParsing();
976 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000977 }
978
Ted Kremenek9e049352010-02-18 23:05:16 +0000979 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000980 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000981 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000982
Steve Naroff84c43102009-02-11 20:43:13 +0000983 // An unnamed colon is valid.
984 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000985 Diag(Tok, diag::err_expected_selector_for_method)
986 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000987 // Skip until we get a ; or {}.
988 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000989 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner5f9e2722011-07-23 10:55:15 +0000992 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000993 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000994 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000995 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000996 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattnerff384912007-10-07 02:00:24 +0000998 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000999 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001000 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001001 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001002 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001003 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001004 methodAttrs.getList(), MethodImplKind,
1005 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001006 PD.complete(Result);
1007 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001008 }
Steve Narofff28b2642007-09-05 23:30:30 +00001009
Chris Lattner5f9e2722011-07-23 10:55:15 +00001010 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001011 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001012 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001013 ParseScope PrototypeScope(this,
1014 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001015
1016 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001017
Chris Lattnerff384912007-10-07 02:00:24 +00001018 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001019 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001020 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattnerff384912007-10-07 02:00:24 +00001022 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001023 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001024 Diag(Tok, diag::err_expected_colon);
1025 break;
1026 }
1027 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001028
John McCallb3d87482010-08-24 05:47:05 +00001029 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001030 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001031 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1032 Declarator::ObjCParameterContext,
1033 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001034
Chris Lattnerff384912007-10-07 02:00:24 +00001035 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001036 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001037 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001038 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001039 MaybeParseGNUAttributes(paramAttrs);
1040 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001041 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001042
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001043 // Code completion for the next piece of the selector.
1044 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001045 KeyIdents.push_back(SelIdent);
1046 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1047 mType == tok::minus,
1048 /*AtParameterName=*/true,
1049 ReturnType,
1050 KeyIdents.data(),
1051 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001052 cutOffParsing();
1053 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001054 }
1055
Chris Lattnerdf195262007-10-09 17:51:17 +00001056 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001057 Diag(Tok, diag::err_expected_ident); // missing argument name.
1058 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001059 }
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnere294d3f2009-04-11 18:57:04 +00001061 ArgInfo.Name = Tok.getIdentifierInfo();
1062 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001063 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattnere294d3f2009-04-11 18:57:04 +00001065 ArgInfos.push_back(ArgInfo);
1066 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001067 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001068
John McCall0b7e6782011-03-24 11:26:52 +00001069 // Make sure the attributes persist.
1070 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1071
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001072 // Code completion for the next piece of the selector.
1073 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001074 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1075 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001076 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001077 ReturnType,
1078 KeyIdents.data(),
1079 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001080 cutOffParsing();
1081 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001082 }
1083
Chris Lattnerff384912007-10-07 02:00:24 +00001084 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001085 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001086 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001087 break;
1088 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001089 }
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Steve Naroff335eafa2007-11-15 12:35:21 +00001091 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattnerff384912007-10-07 02:00:24 +00001093 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001094 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001095 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001097 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001098 ConsumeToken();
1099 break;
1100 }
John McCall0b7e6782011-03-24 11:26:52 +00001101 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001102 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001103 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001104 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1105 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001106 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001107 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001108 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1109 ParmDecl.getIdentifierLoc(),
1110 Param,
1111 0));
1112
Chris Lattnerff384912007-10-07 02:00:24 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001115 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001116 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001117 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001118 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001119
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001120 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001121 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001122
Chris Lattnerff384912007-10-07 02:00:24 +00001123 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1124 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001125 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001126 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001127 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001128 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001129 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001130 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001131 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001132
John McCall54abf7d2009-11-04 02:18:39 +00001133 PD.complete(Result);
1134 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001135}
1136
Steve Naroffdac269b2007-08-20 21:31:48 +00001137/// objc-protocol-refs:
1138/// '<' identifier-list '>'
1139///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001140bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001141ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1142 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001143 bool WarnOnDeclarations,
1144 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001145 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001147 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattner5f9e2722011-07-23 10:55:15 +00001149 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Chris Lattnere13b9592008-07-26 04:03:38 +00001151 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001152 if (Tok.is(tok::code_completion)) {
1153 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1154 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001155 cutOffParsing();
1156 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001157 }
1158
Chris Lattnere13b9592008-07-26 04:03:38 +00001159 if (Tok.isNot(tok::identifier)) {
1160 Diag(Tok, diag::err_expected_ident);
1161 SkipUntil(tok::greater);
1162 return true;
1163 }
1164 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1165 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001166 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001167 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Chris Lattnere13b9592008-07-26 04:03:38 +00001169 if (Tok.isNot(tok::comma))
1170 break;
1171 ConsumeToken();
1172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Chris Lattnere13b9592008-07-26 04:03:38 +00001174 // Consume the '>'.
1175 if (Tok.isNot(tok::greater)) {
1176 Diag(Tok, diag::err_expected_greater);
1177 return true;
1178 }
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattnere13b9592008-07-26 04:03:38 +00001180 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Chris Lattnere13b9592008-07-26 04:03:38 +00001182 // Convert the list of protocols identifiers into a list of protocol decls.
1183 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1184 &ProtocolIdents[0], ProtocolIdents.size(),
1185 Protocols);
1186 return false;
1187}
1188
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001189/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1190/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001191bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001192 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1193 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1194 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001195 SmallVector<Decl *, 8> ProtocolDecl;
1196 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001197 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1198 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001199 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1200 ProtocolLocs.data(), LAngleLoc);
1201 if (EndProtoLoc.isValid())
1202 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001203 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001204}
1205
1206
Steve Naroffdac269b2007-08-20 21:31:48 +00001207/// objc-class-instance-variables:
1208/// '{' objc-instance-variable-decl-list[opt] '}'
1209///
1210/// objc-instance-variable-decl-list:
1211/// objc-visibility-spec
1212/// objc-instance-variable-decl ';'
1213/// ';'
1214/// objc-instance-variable-decl-list objc-visibility-spec
1215/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1216/// objc-instance-variable-decl-list ';'
1217///
1218/// objc-visibility-spec:
1219/// @private
1220/// @protected
1221/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001222/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001223///
1224/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001225/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001226///
John McCalld226f652010-08-21 09:40:31 +00001227void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001228 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001229 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001230 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001231 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001232
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001233 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001234 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001235
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001236 BalancedDelimiterTracker T(*this, tok::l_brace);
1237 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Steve Naroffddbff782007-08-21 21:17:12 +00001239 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001240 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001241 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Steve Naroffddbff782007-08-21 21:17:12 +00001243 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001244 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001245 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001246 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001247 ConsumeToken();
1248 continue;
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Steve Naroffddbff782007-08-21 21:17:12 +00001251 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001252 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001253 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001254
1255 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001256 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001257 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001258 }
1259
Steve Naroff861cf3e2007-08-23 18:16:40 +00001260 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001261 case tok::objc_private:
1262 case tok::objc_public:
1263 case tok::objc_protected:
1264 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001265 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001266 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001267 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001268 default:
1269 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001270 continue;
1271 }
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001274 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001275 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001276 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001277 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001278 }
1279
John McCallbdd563e2009-11-03 02:38:08 +00001280 struct ObjCIvarCallback : FieldCallback {
1281 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001282 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001283 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001284 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001285
John McCalld226f652010-08-21 09:40:31 +00001286 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001287 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001288 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1289 }
1290
John McCalld226f652010-08-21 09:40:31 +00001291 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001292 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001293 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001294 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001295 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001296 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001297 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001298 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001299 if (Field)
1300 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001301 return Field;
1302 }
1303 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001304
Chris Lattnere1359422008-04-10 06:46:29 +00001305 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001306 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001307 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Chris Lattnerdf195262007-10-09 17:51:17 +00001309 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001310 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001311 } else {
1312 Diag(Tok, diag::err_expected_semi_decl_list);
1313 // Skip to end of block or statement
1314 SkipUntil(tok::r_brace, true, true);
1315 }
1316 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001317 T.consumeClose();
1318
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001319 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001320 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001321 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001322 // Call ActOnFields() even if we don't have any decls. This is useful
1323 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001324 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001325 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001326 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001327 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001328}
Steve Naroffdac269b2007-08-20 21:31:48 +00001329
1330/// objc-protocol-declaration:
1331/// objc-protocol-definition
1332/// objc-protocol-forward-reference
1333///
1334/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001335/// @protocol identifier
1336/// objc-protocol-refs[opt]
1337/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001338/// @end
1339///
1340/// objc-protocol-forward-reference:
1341/// @protocol identifier-list ';'
1342///
1343/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001344/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001345/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001346Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001347 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001348 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001349 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1350 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Douglas Gregor083128f2009-11-18 04:49:41 +00001352 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001353 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001354 cutOffParsing();
1355 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001356 }
1357
Chris Lattnerdf195262007-10-09 17:51:17 +00001358 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001359 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001360 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001361 }
1362 // Save the protocol name, then consume it.
1363 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1364 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Chris Lattnerdf195262007-10-09 17:51:17 +00001366 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001367 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001368 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001369 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001370 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001371 }
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001373 CheckNestedObjCContexts(AtLoc);
1374
Chris Lattnerdf195262007-10-09 17:51:17 +00001375 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001376 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001377 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1378
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001379 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001380 while (1) {
1381 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001382 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001383 Diag(Tok, diag::err_expected_ident);
1384 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001385 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001386 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001387 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1388 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001389 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Chris Lattnerdf195262007-10-09 17:51:17 +00001391 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001392 break;
1393 }
1394 // Consume the ';'.
1395 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001396 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Steve Naroffe440eb82007-10-10 17:32:04 +00001398 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001399 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001400 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001401 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001404 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001405 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001406
Chris Lattner5f9e2722011-07-23 10:55:15 +00001407 SmallVector<Decl *, 8> ProtocolRefs;
1408 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001409 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001410 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1411 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001412 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001413
John McCalld226f652010-08-21 09:40:31 +00001414 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001415 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001416 ProtocolRefs.data(),
1417 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001418 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001419 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001420
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001421 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001422 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001423}
Steve Naroffdac269b2007-08-20 21:31:48 +00001424
1425/// objc-implementation:
1426/// objc-class-implementation-prologue
1427/// objc-category-implementation-prologue
1428///
1429/// objc-class-implementation-prologue:
1430/// @implementation identifier objc-superclass[opt]
1431/// objc-class-instance-variables[opt]
1432///
1433/// objc-category-implementation-prologue:
1434/// @implementation identifier ( identifier )
Erik Verbruggend64251f2011-12-06 09:25:23 +00001435Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001436 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1437 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001438 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001439 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001441 // Code completion after '@implementation'.
1442 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001443 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001444 cutOffParsing();
1445 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001446 }
1447
Chris Lattnerdf195262007-10-09 17:51:17 +00001448 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001449 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001450 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001451 }
1452 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001453 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001454 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001455
1456 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001457 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001458 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001459 SourceLocation categoryLoc, rparenLoc;
1460 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001462 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001463 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001464 cutOffParsing();
1465 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001466 }
1467
Chris Lattnerdf195262007-10-09 17:51:17 +00001468 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001469 categoryId = Tok.getIdentifierInfo();
1470 categoryLoc = ConsumeToken();
1471 } else {
1472 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001473 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001474 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001475 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001476 Diag(Tok, diag::err_expected_rparen);
1477 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001478 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001479 }
1480 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001481 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001482 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001483 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001484
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001486 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001487 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001488 }
1489 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001490 SourceLocation superClassLoc;
1491 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001492 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001493 // We have a super class
1494 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001495 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001496 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001497 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001498 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001499 superClassId = Tok.getIdentifierInfo();
1500 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001501 }
John McCalld226f652010-08-21 09:40:31 +00001502 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001503 AtLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001504 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Steve Naroff60fccee2007-10-29 21:38:07 +00001506 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggend64251f2011-12-06 09:25:23 +00001507 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001508
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001509 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001510 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001511 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001512}
Steve Naroff60fccee2007-10-29 21:38:07 +00001513
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001514Parser::DeclGroupPtrTy
1515Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001516 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1517 "ParseObjCAtEndDeclaration(): Expected @end");
1518 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001519 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001520 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001521 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1522 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001523 if (D)
1524 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001525 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001526 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001527
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001528 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001529 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001530 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001531 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001532 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001533 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001534 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001535
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001536 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001537 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001538 return Actions.BuildDeclaratorGroup(
1539 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001540}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001541
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001542Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1543 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001544 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001545 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggend64251f2011-12-06 09:25:23 +00001546
John McCalld226f652010-08-21 09:40:31 +00001547 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggend64251f2011-12-06 09:25:23 +00001548 Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1549 Diag(Tok, diag::err_objc_missing_end)
1550 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1551 if (ImpDecl)
1552 Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1553 << Sema::OCK_Implementation;
1554
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001555 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1556}
1557
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001558void Parser::clearLateParsedObjCMethods() {
1559 for (LateParsedObjCMethodContainer::iterator
1560 I = LateParsedObjCMethods.begin(),
1561 E = LateParsedObjCMethods.end(); I != E; ++I)
1562 delete *I;
1563 LateParsedObjCMethods.clear();
1564}
1565
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001566/// compatibility-alias-decl:
1567/// @compatibility_alias alias-name class-name ';'
1568///
John McCalld226f652010-08-21 09:40:31 +00001569Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001570 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1571 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1572 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001573 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001574 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001575 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001576 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001577 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1578 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001579 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001580 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001581 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001582 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001583 IdentifierInfo *classId = Tok.getIdentifierInfo();
1584 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001585 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1586 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001587 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1588 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001589}
1590
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001591/// property-synthesis:
1592/// @synthesize property-ivar-list ';'
1593///
1594/// property-ivar-list:
1595/// property-ivar
1596/// property-ivar-list ',' property-ivar
1597///
1598/// property-ivar:
1599/// identifier
1600/// identifier '=' identifier
1601///
John McCalld226f652010-08-21 09:40:31 +00001602Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001603 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1604 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001605 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Douglas Gregorb328c422009-11-18 19:45:45 +00001607 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001608 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001609 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001610 cutOffParsing();
1611 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001612 }
1613
Douglas Gregorb328c422009-11-18 19:45:45 +00001614 if (Tok.isNot(tok::identifier)) {
1615 Diag(Tok, diag::err_synthesized_property_name);
1616 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001617 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001618 }
1619
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001620 IdentifierInfo *propertyIvar = 0;
1621 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1622 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001623 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001624 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001625 // property '=' ivar-name
1626 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001627
1628 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001629 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001630 cutOffParsing();
1631 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001632 }
1633
Chris Lattnerdf195262007-10-09 17:51:17 +00001634 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001635 Diag(Tok, diag::err_expected_ident);
1636 break;
1637 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001638 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001639 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001640 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001641 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001642 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001643 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001644 break;
1645 ConsumeToken(); // consume ','
1646 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001647 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001648 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001649}
1650
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001651/// property-dynamic:
1652/// @dynamic property-list
1653///
1654/// property-list:
1655/// identifier
1656/// property-list ',' identifier
1657///
John McCalld226f652010-08-21 09:40:31 +00001658Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001659 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1660 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001661 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001662 while (true) {
1663 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001664 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001665 cutOffParsing();
1666 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001667 }
1668
1669 if (Tok.isNot(tok::identifier)) {
1670 Diag(Tok, diag::err_expected_ident);
1671 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001672 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001673 }
1674
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001675 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1676 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001677 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001678 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001679
Chris Lattnerdf195262007-10-09 17:51:17 +00001680 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001681 break;
1682 ConsumeToken(); // consume ','
1683 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001684 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001685 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001686}
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001688/// objc-throw-statement:
1689/// throw expression[opt];
1690///
John McCall60d7b3a2010-08-24 06:29:42 +00001691StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1692 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001693 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001694 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001695 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001696 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001697 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001698 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001699 }
1700 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001701 // consume ';'
1702 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001703 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001704}
1705
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001706/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001707/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001708///
John McCall60d7b3a2010-08-24 06:29:42 +00001709StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001710Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001711 ConsumeToken(); // consume synchronized
1712 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001713 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001714 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001715 }
John McCall07524032011-07-27 21:50:02 +00001716
1717 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001718 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001719 ExprResult operand(ParseExpression());
1720
1721 if (Tok.is(tok::r_paren)) {
1722 ConsumeParen(); // ')'
1723 } else {
1724 if (!operand.isInvalid())
1725 Diag(Tok, diag::err_expected_rparen);
1726
1727 // Skip forward until we see a left brace, but don't consume it.
1728 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001729 }
John McCall07524032011-07-27 21:50:02 +00001730
1731 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001732 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001733 if (!operand.isInvalid())
1734 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001735 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001736 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001737
John McCall07524032011-07-27 21:50:02 +00001738 // Check the @synchronized operand now.
1739 if (!operand.isInvalid())
1740 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001741
John McCall07524032011-07-27 21:50:02 +00001742 // Parse the compound statement within a new scope.
1743 ParseScope bodyScope(this, Scope::DeclScope);
1744 StmtResult body(ParseCompoundStatementBody());
1745 bodyScope.Exit();
1746
1747 // If there was a semantic or parse error earlier with the
1748 // operand, fail now.
1749 if (operand.isInvalid())
1750 return StmtError();
1751
1752 if (body.isInvalid())
1753 body = Actions.ActOnNullStmt(Tok.getLocation());
1754
1755 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001756}
1757
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001758/// objc-try-catch-statement:
1759/// @try compound-statement objc-catch-list[opt]
1760/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1761///
1762/// objc-catch-list:
1763/// @catch ( parameter-declaration ) compound-statement
1764/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1765/// catch-parameter-declaration:
1766/// parameter-declaration
1767/// '...' [OBJC2]
1768///
John McCall60d7b3a2010-08-24 06:29:42 +00001769StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001770 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001771
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001772 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001773 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001774 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001775 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001776 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001777 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001778 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001779 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001780 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001781 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001782 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001783 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001784
Chris Lattnerdf195262007-10-09 17:51:17 +00001785 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001786 // At this point, we need to lookahead to determine if this @ is the start
1787 // of an @catch or @finally. We don't want to consume the @ token if this
1788 // is an @try or @encode or something else.
1789 Token AfterAt = GetLookAheadToken(1);
1790 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1791 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1792 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001793
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001794 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001795 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001796 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001797 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001798 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001799 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001800 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001801 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001802 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001803 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001804 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001805 ParseDeclarator(ParmDecl);
1806
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001807 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001808 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001809 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001810 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001811 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Steve Naroff93a25952009-04-07 22:56:58 +00001813 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Steve Naroff93a25952009-04-07 22:56:58 +00001815 if (Tok.is(tok::r_paren))
1816 RParenLoc = ConsumeParen();
1817 else // Skip over garbage, until we get to ')'. Eat the ')'.
1818 SkipUntil(tok::r_paren, true, false);
1819
John McCall60d7b3a2010-08-24 06:29:42 +00001820 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001821 if (Tok.is(tok::l_brace))
1822 CatchBody = ParseCompoundStatementBody();
1823 else
1824 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001825 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001826 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001827
John McCall60d7b3a2010-08-24 06:29:42 +00001828 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001829 RParenLoc,
1830 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001831 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001832 if (!Catch.isInvalid())
1833 CatchStmts.push_back(Catch.release());
1834
Steve Naroff64515f32008-02-05 21:27:35 +00001835 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001836 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1837 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001838 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001839 }
1840 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001841 } else {
1842 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001843 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001844 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001845
John McCall60d7b3a2010-08-24 06:29:42 +00001846 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001847 if (Tok.is(tok::l_brace))
1848 FinallyBody = ParseCompoundStatementBody();
1849 else
1850 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001851 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001852 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001853 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001854 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001855 catch_or_finally_seen = true;
1856 break;
1857 }
1858 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001859 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001860 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001861 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001862 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001863
John McCall9ae2f072010-08-23 23:25:46 +00001864 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001865 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001866 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001867}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001868
John McCallf85e1932011-06-15 23:02:42 +00001869/// objc-autoreleasepool-statement:
1870/// @autoreleasepool compound-statement
1871///
1872StmtResult
1873Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1874 ConsumeToken(); // consume autoreleasepool
1875 if (Tok.isNot(tok::l_brace)) {
1876 Diag(Tok, diag::err_expected_lbrace);
1877 return StmtError();
1878 }
1879 // Enter a scope to hold everything within the compound stmt. Compound
1880 // statements can always hold declarations.
1881 ParseScope BodyScope(this, Scope::DeclScope);
1882
1883 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1884
1885 BodyScope.Exit();
1886 if (AutoreleasePoolBody.isInvalid())
1887 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1888 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1889 AutoreleasePoolBody.take());
1890}
1891
Steve Naroff3536b442007-09-06 21:24:23 +00001892/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001893///
John McCalld226f652010-08-21 09:40:31 +00001894Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001895 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001896
John McCallf312b1e2010-08-26 23:41:50 +00001897 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1898 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001900 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001901 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001902 if (ObjCImpDecl) {
1903 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001904 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001905 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001906 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001907 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001908
Steve Naroff409be832007-11-11 19:54:21 +00001909 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001910 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001911 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Steve Naroff409be832007-11-11 19:54:21 +00001913 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1914 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Steve Naroff409be832007-11-11 19:54:21 +00001916 // If we didn't find the '{', bail out.
1917 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001918 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001919 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001920 // Allow the rest of sema to find private method decl implementations.
1921 if (MDecl)
1922 Actions.AddAnyMethodToGlobalPool(MDecl);
1923
1924 // Consume the tokens and store them for later parsing.
1925 LexedMethod* LM = new LexedMethod(this, MDecl);
1926 LateParsedObjCMethods.push_back(LM);
1927 CachedTokens &Toks = LM->Toks;
1928 // Begin by storing the '{' token.
1929 Toks.push_back(Tok);
1930 ConsumeBrace();
1931 // Consume everything up to (and including) the matching right brace.
1932 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001933 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001934}
Anders Carlsson55085182007-08-21 17:43:55 +00001935
John McCall60d7b3a2010-08-24 06:29:42 +00001936StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001937 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001938 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001939 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001940 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001941 }
1942
1943 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001944 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001945
1946 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001947 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001948
1949 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001950 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001951
1952 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1953 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001954
John McCall60d7b3a2010-08-24 06:29:42 +00001955 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001956 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001957 // If the expression is invalid, skip ahead to the next semicolon. Not
1958 // doing this opens us up to the possibility of infinite loops if
1959 // ParseExpression does not consume any tokens.
1960 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001961 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001962 }
Chris Lattner5d803162009-12-07 16:33:19 +00001963
Steve Naroff64515f32008-02-05 21:27:35 +00001964 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001965 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001966 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001967}
1968
John McCall60d7b3a2010-08-24 06:29:42 +00001969ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001970 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001971 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001972 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001973 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001974 return ExprError();
1975
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001976 case tok::string_literal: // primary-expression: string-literal
1977 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001978 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001979 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001980 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001981 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001982
Chris Lattner4fef81d2008-08-05 06:19:09 +00001983 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1984 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001985 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001986 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001987 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001988 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001989 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001990 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001991 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001992 }
Anders Carlsson55085182007-08-21 17:43:55 +00001993 }
Anders Carlsson55085182007-08-21 17:43:55 +00001994}
1995
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001996/// \brirg Parse the receiver of an Objective-C++ message send.
1997///
1998/// This routine parses the receiver of a message send in
1999/// Objective-C++ either as a type or as an expression. Note that this
2000/// routine must not be called to parse a send to 'super', since it
2001/// has no way to return such a result.
2002///
2003/// \param IsExpr Whether the receiver was parsed as an expression.
2004///
2005/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2006/// IsExpr is true), the parsed expression. If the receiver was parsed
2007/// as a type (\c IsExpr is false), the parsed type.
2008///
2009/// \returns True if an error occurred during parsing or semantic
2010/// analysis, in which case the arguments do not have valid
2011/// values. Otherwise, returns false for a successful parse.
2012///
2013/// objc-receiver: [C++]
2014/// 'super' [not parsed here]
2015/// expression
2016/// simple-type-specifier
2017/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002018bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002019 InMessageExpressionRAIIObject InMessage(*this, true);
2020
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002021 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2022 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2023 TryAnnotateTypeOrScopeToken();
2024
2025 if (!isCXXSimpleTypeSpecifier()) {
2026 // objc-receiver:
2027 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002028 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002029 if (Receiver.isInvalid())
2030 return true;
2031
2032 IsExpr = true;
2033 TypeOrExpr = Receiver.take();
2034 return false;
2035 }
2036
2037 // objc-receiver:
2038 // typename-specifier
2039 // simple-type-specifier
2040 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002041 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002042 ParseCXXSimpleTypeSpecifier(DS);
2043
2044 if (Tok.is(tok::l_paren)) {
2045 // If we see an opening parentheses at this point, we are
2046 // actually parsing an expression that starts with a
2047 // function-style cast, e.g.,
2048 //
2049 // postfix-expression:
2050 // simple-type-specifier ( expression-list [opt] )
2051 // typename-specifier ( expression-list [opt] )
2052 //
2053 // Parse the remainder of this case, then the (optional)
2054 // postfix-expression suffix, followed by the (optional)
2055 // right-hand side of the binary expression. We have an
2056 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002057 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002058 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002059 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002060 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002061 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002062 if (Receiver.isInvalid())
2063 return true;
2064
2065 IsExpr = true;
2066 TypeOrExpr = Receiver.take();
2067 return false;
2068 }
2069
2070 // We have a class message. Turn the simple-type-specifier or
2071 // typename-specifier we parsed into a type and parse the
2072 // remainder of the class message.
2073 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002074 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002075 if (Type.isInvalid())
2076 return true;
2077
2078 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002079 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002080 return false;
2081}
2082
Douglas Gregor1b730e82010-05-31 14:40:22 +00002083/// \brief Determine whether the parser is currently referring to a an
2084/// Objective-C message send, using a simplified heuristic to avoid overhead.
2085///
2086/// This routine will only return true for a subset of valid message-send
2087/// expressions.
2088bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002089 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002090 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002091 return GetLookAheadToken(1).is(tok::identifier) &&
2092 GetLookAheadToken(2).is(tok::identifier);
2093}
2094
Douglas Gregor9497a732010-09-16 01:51:54 +00002095bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2096 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2097 InMessageExpression)
2098 return false;
2099
2100
2101 ParsedType Type;
2102
2103 if (Tok.is(tok::annot_typename))
2104 Type = getTypeAnnotation(Tok);
2105 else if (Tok.is(tok::identifier))
2106 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2107 getCurScope());
2108 else
2109 return false;
2110
2111 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2112 const Token &AfterNext = GetLookAheadToken(2);
2113 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2114 if (Tok.is(tok::identifier))
2115 TryAnnotateTypeOrScopeToken();
2116
2117 return Tok.is(tok::annot_typename);
2118 }
2119 }
2120
2121 return false;
2122}
2123
Mike Stump1eb44332009-09-09 15:08:12 +00002124/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002125/// '[' objc-receiver objc-message-args ']'
2126///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002127/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002128/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002129/// expression
2130/// class-name
2131/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002132///
John McCall60d7b3a2010-08-24 06:29:42 +00002133ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002134 assert(Tok.is(tok::l_square) && "'[' expected");
2135 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2136
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002137 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002138 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002139 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002140 return ExprError();
2141 }
2142
Douglas Gregor0fbda682010-09-15 14:51:05 +00002143 InMessageExpressionRAIIObject InMessage(*this, true);
2144
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002145 if (getLang().CPlusPlus) {
2146 // We completely separate the C and C++ cases because C++ requires
2147 // more complicated (read: slower) parsing.
2148
2149 // Handle send to super.
2150 // FIXME: This doesn't benefit from the same typo-correction we
2151 // get in Objective-C.
2152 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002153 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002154 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2155 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002156
2157 // Parse the receiver, which is either a type or an expression.
2158 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002159 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002160 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2161 SkipUntil(tok::r_square);
2162 return ExprError();
2163 }
2164
2165 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002166 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2167 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002168 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002169
2170 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002171 ParsedType::getFromOpaquePtr(TypeOrExpr),
2172 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002173 }
2174
2175 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002176 IdentifierInfo *Name = Tok.getIdentifierInfo();
2177 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002178 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002179 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002180 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002181 NextToken().is(tok::period),
2182 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002183 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002184 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2185 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002186
John McCallf312b1e2010-08-26 23:41:50 +00002187 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002188 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002189 SkipUntil(tok::r_square);
2190 return ExprError();
2191 }
2192
Douglas Gregor1569f952010-04-21 20:38:13 +00002193 ConsumeToken(); // the type name
2194
2195 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002196 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002197
John McCallf312b1e2010-08-26 23:41:50 +00002198 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002199 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002200 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002201 }
Chris Lattner699b6612008-01-25 18:59:06 +00002202 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002203
2204 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002205 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002206 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002207 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002208 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002209 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002210
John McCallb3d87482010-08-24 05:47:05 +00002211 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2212 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002213}
Sebastian Redl1d922962008-12-13 15:32:12 +00002214
Douglas Gregor2725ca82010-04-21 19:57:20 +00002215/// \brief Parse the remainder of an Objective-C message following the
2216/// '[' objc-receiver.
2217///
2218/// This routine handles sends to super, class messages (sent to a
2219/// class name), and instance messages (sent to an object), and the
2220/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2221/// ReceiverExpr, respectively. Only one of these parameters may have
2222/// a valid value.
2223///
2224/// \param LBracLoc The location of the opening '['.
2225///
2226/// \param SuperLoc If this is a send to 'super', the location of the
2227/// 'super' keyword that indicates a send to the superclass.
2228///
2229/// \param ReceiverType If this is a class message, the type of the
2230/// class we are sending a message to.
2231///
2232/// \param ReceiverExpr If this is an instance message, the expression
2233/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002234///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002235/// objc-message-args:
2236/// objc-selector
2237/// objc-keywordarg-list
2238///
2239/// objc-keywordarg-list:
2240/// objc-keywordarg
2241/// objc-keywordarg-list objc-keywordarg
2242///
Mike Stump1eb44332009-09-09 15:08:12 +00002243/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002244/// selector-name[opt] ':' objc-keywordexpr
2245///
2246/// objc-keywordexpr:
2247/// nonempty-expr-list
2248///
2249/// nonempty-expr-list:
2250/// assignment-expression
2251/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002252///
John McCall60d7b3a2010-08-24 06:29:42 +00002253ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002254Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002255 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002256 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002257 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002258 InMessageExpressionRAIIObject InMessage(*this, true);
2259
Steve Naroffc4df6d22009-11-07 02:08:14 +00002260 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002261 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002262 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2263 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002264 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002265 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2266 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002267 else
John McCall9ae2f072010-08-23 23:25:46 +00002268 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002269 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002270 cutOffParsing();
2271 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002272 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002273
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002274 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002275 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002276 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002277
Chris Lattner5f9e2722011-07-23 10:55:15 +00002278 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002279 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002280 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002281
Chris Lattnerdf195262007-10-09 17:51:17 +00002282 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002283 while (1) {
2284 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002285 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002286 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002287
Chris Lattnerdf195262007-10-09 17:51:17 +00002288 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002289 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002290 // We must manually skip to a ']', otherwise the expression skipper will
2291 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2292 // the enclosing expression.
2293 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002294 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002295 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002296
Steve Naroff68d331a2007-09-27 14:38:14 +00002297 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002298 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002299
2300 if (Tok.is(tok::code_completion)) {
2301 if (SuperLoc.isValid())
2302 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2303 KeyIdents.data(),
2304 KeyIdents.size(),
2305 /*AtArgumentEpression=*/true);
2306 else if (ReceiverType)
2307 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2308 KeyIdents.data(),
2309 KeyIdents.size(),
2310 /*AtArgumentEpression=*/true);
2311 else
2312 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2313 KeyIdents.data(),
2314 KeyIdents.size(),
2315 /*AtArgumentEpression=*/true);
2316
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002317 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002318 return ExprError();
2319 }
2320
John McCall60d7b3a2010-08-24 06:29:42 +00002321 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002322 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002323 // We must manually skip to a ']', otherwise the expression skipper will
2324 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2325 // the enclosing expression.
2326 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002327 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002328 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002329
Steve Naroff37387c92007-09-17 20:25:27 +00002330 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002331 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002332
Douglas Gregord3c68542009-11-19 01:08:35 +00002333 // Code completion after each argument.
2334 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002335 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002336 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002337 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002338 KeyIdents.size(),
2339 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002340 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002341 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002342 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002343 KeyIdents.size(),
2344 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002345 else
John McCall9ae2f072010-08-23 23:25:46 +00002346 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002347 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002348 KeyIdents.size(),
2349 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002350 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002351 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002352 }
2353
Steve Naroff37387c92007-09-17 20:25:27 +00002354 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002355 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002356 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002357 break;
2358 // We have a selector or a colon, continue parsing.
2359 }
2360 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002361 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002362 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002363 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002364 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002365 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002366 // We must manually skip to a ']', otherwise the expression skipper will
2367 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2368 // the enclosing expression.
2369 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002370 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002371 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002372
Steve Naroff49f109c2007-11-15 13:05:42 +00002373 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002374 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002375 }
2376 } else if (!selIdent) {
2377 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002378
Chris Lattner4fef81d2008-08-05 06:19:09 +00002379 // We must manually skip to a ']', otherwise the expression skipper will
2380 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2381 // the enclosing expression.
2382 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002383 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002384 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002385
Chris Lattnerdf195262007-10-09 17:51:17 +00002386 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002387 if (Tok.is(tok::identifier))
2388 Diag(Tok, diag::err_expected_colon);
2389 else
2390 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002391 // We must manually skip to a ']', otherwise the expression skipper will
2392 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2393 // the enclosing expression.
2394 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002395 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002396 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002397
Chris Lattner699b6612008-01-25 18:59:06 +00002398 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002399
Steve Naroff29238a02007-10-05 18:42:47 +00002400 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002401 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002402 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002403 KeyLocs.push_back(Loc);
2404 }
Chris Lattnerff384912007-10-07 02:00:24 +00002405 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002406
Douglas Gregor2725ca82010-04-21 19:57:20 +00002407 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002408 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002409 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002410 MultiExprArg(Actions,
2411 KeyExprs.take(),
2412 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002413 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002414 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002415 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002416 MultiExprArg(Actions,
2417 KeyExprs.take(),
2418 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002419 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002420 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002421 MultiExprArg(Actions,
2422 KeyExprs.take(),
2423 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002424}
2425
John McCall60d7b3a2010-08-24 06:29:42 +00002426ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2427 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002428 if (Res.isInvalid()) return move(Res);
2429
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002430 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2431 // expressions. At this point, we know that the only valid thing that starts
2432 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002433 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002434 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002435 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002436 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002437
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002438 while (Tok.is(tok::at)) {
2439 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002440
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002441 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002442 if (!isTokenStringLiteral())
2443 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002444
John McCall60d7b3a2010-08-24 06:29:42 +00002445 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002446 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002447 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002448
Sebastian Redleffa8d12008-12-10 00:02:53 +00002449 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002450 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002451
2452 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2453 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002454}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002455
2456/// objc-encode-expression:
2457/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002458ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002459Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002460 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002461
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002462 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002463
Chris Lattner4fef81d2008-08-05 06:19:09 +00002464 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002465 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2466
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002467 BalancedDelimiterTracker T(*this, tok::l_paren);
2468 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002469
Douglas Gregor809070a2009-02-18 17:45:20 +00002470 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002471
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002472 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002473
Douglas Gregor809070a2009-02-18 17:45:20 +00002474 if (Ty.isInvalid())
2475 return ExprError();
2476
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002477 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2478 T.getOpenLocation(), Ty.get(),
2479 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002480}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002481
2482/// objc-protocol-expression
2483/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002484ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002485Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002486 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002487
Chris Lattner4fef81d2008-08-05 06:19:09 +00002488 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002489 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2490
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002491 BalancedDelimiterTracker T(*this, tok::l_paren);
2492 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002493
Chris Lattner4fef81d2008-08-05 06:19:09 +00002494 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002495 return ExprError(Diag(Tok, diag::err_expected_ident));
2496
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002497 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002498 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002499
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002500 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002501
Sebastian Redl1d922962008-12-13 15:32:12 +00002502 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002503 T.getOpenLocation(),
2504 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002505}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002506
2507/// objc-selector-expression
2508/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002509ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002510 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002511
Chris Lattner4fef81d2008-08-05 06:19:09 +00002512 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002513 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2514
Chris Lattner5f9e2722011-07-23 10:55:15 +00002515 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002516 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002517
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002518 BalancedDelimiterTracker T(*this, tok::l_paren);
2519 T.consumeOpen();
2520
Douglas Gregor458433d2010-08-26 15:07:07 +00002521 if (Tok.is(tok::code_completion)) {
2522 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2523 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002524 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002525 return ExprError();
2526 }
2527
Chris Lattner2fc5c242009-04-11 18:13:45 +00002528 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002529 if (!SelIdent && // missing selector name.
2530 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002531 return ExprError(Diag(Tok, diag::err_expected_ident));
2532
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002533 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002534 unsigned nColons = 0;
2535 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002536 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002537 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2538 ++nColons;
2539 KeyIdents.push_back(0);
2540 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002541 return ExprError(Diag(Tok, diag::err_expected_colon));
2542
Chris Lattner5add7542010-08-27 22:32:41 +00002543 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002544 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002545 if (Tok.is(tok::r_paren))
2546 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002547
2548 if (Tok.is(tok::code_completion)) {
2549 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2550 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002551 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002552 return ExprError();
2553 }
2554
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002555 // Check for another keyword selector.
2556 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002557 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002558 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002559 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002560 break;
2561 }
Steve Naroff887407e2007-12-05 22:21:29 +00002562 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002563 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002564 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002565 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002566 T.getOpenLocation(),
2567 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002568 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002569
2570Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002571
2572 // Save the current token position.
2573 SourceLocation OrigLoc = Tok.getLocation();
2574
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002575 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2576 // Append the current token at the end of the new token stream so that it
2577 // doesn't get lost.
2578 LM.Toks.push_back(Tok);
2579 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2580
2581 // MDecl might be null due to error in method prototype, etc.
2582 Decl *MDecl = LM.D;
2583 // Consume the previously pushed token.
2584 ConsumeAnyToken();
2585
2586 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2587 SourceLocation BraceLoc = Tok.getLocation();
2588 // Enter a scope for the method body.
2589 ParseScope BodyScope(this,
2590 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2591
2592 // Tell the actions module that we have entered a method definition with the
2593 // specified Declarator for the method.
2594 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2595
2596 if (PP.isCodeCompletionEnabled()) {
2597 if (trySkippingFunctionBodyForCodeCompletion()) {
2598 BodyScope.Exit();
2599 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2600 }
2601 }
2602
2603 StmtResult FnBody(ParseCompoundStatementBody());
2604
2605 // If the function body could not be parsed, make a bogus compoundstmt.
2606 if (FnBody.isInvalid())
2607 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2608 MultiStmtArg(Actions), false);
2609
2610 // Leave the function body scope.
2611 BodyScope.Exit();
2612
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002613 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2614
2615 if (Tok.getLocation() != OrigLoc) {
2616 // Due to parsing error, we either went over the cached tokens or
2617 // there are still cached tokens left. If it's the latter case skip the
2618 // leftover tokens.
2619 // Since this is an uncommon situation that should be avoided, use the
2620 // expensive isBeforeInTranslationUnit call.
2621 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2622 OrigLoc))
2623 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2624 ConsumeAnyToken();
2625 }
2626
2627 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002628}