blob: 1b48225837342eede43e71d9612cc6b8b5858ece [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
Steve Naroffdac269b2007-08-20 21:31:48 +0000116///
117/// objc-interface:
118/// objc-class-interface-attributes[opt] objc-class-interface
119/// objc-category-interface
120///
121/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000122/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000123/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000124/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000125/// objc-interface-decl-list
126/// @end
127///
128/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000129/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000130/// objc-protocol-refs[opt]
131/// objc-interface-decl-list
132/// @end
133///
134/// objc-superclass:
135/// ':' identifier
136///
137/// objc-class-interface-attributes:
138/// __attribute__((visibility("default")))
139/// __attribute__((visibility("hidden")))
140/// __attribute__((deprecated))
141/// __attribute__((unavailable))
142/// __attribute__((objc_exception)) - used by NSException on 64-bit
143///
John McCall7f040a92010-12-24 02:08:15 +0000144Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
145 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000146 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
148 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000150 // Code completion after '@interface'.
151 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000152 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000153 cutOffParsing();
154 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000155 }
156
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000161
Steve Naroffdac269b2007-08-20 21:31:48 +0000162 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000163 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000165 if (Tok.is(tok::l_paren) &&
166 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000167 SourceLocation LParenLoc = ConsumeParen();
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 SourceLocation categoryLoc, rparenLoc;
169 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000170 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000172 cutOffParsing();
173 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000174 }
175
Steve Naroff527fe232007-08-23 19:56:30 +0000176 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 categoryId = Tok.getIdentifierInfo();
179 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000180 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000181 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000182 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000183 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000185
186 rparenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
187 if (rparenLoc.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000189
190 if (!attrs.empty()) { // categories don't support attributes.
191 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
192 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000193 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000194
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000195 // Next, we need to check for any protocol references.
196 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 SmallVector<Decl *, 8> ProtocolRefs;
198 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000199 if (Tok.is(tok::less) &&
200 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000201 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000202 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
John McCalld226f652010-08-21 09:40:31 +0000204 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000205 Actions.ActOnStartCategoryInterface(atLoc,
206 nameId, nameLoc,
207 categoryId, categoryLoc,
208 ProtocolRefs.data(),
209 ProtocolRefs.size(),
210 ProtocolLocs.data(),
211 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000212
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000213 if (Tok.is(tok::l_brace))
214 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
215
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000216 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000217 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000218 }
219 // Parse a class interface.
220 IdentifierInfo *superClassId = 0;
221 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000222
Chris Lattnerdf195262007-10-09 17:51:17 +0000223 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000224 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000225
226 // Code completion of superclass names.
227 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000228 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000229 cutOffParsing();
230 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000231 }
232
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000234 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000235 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000236 }
237 superClassId = Tok.getIdentifierInfo();
238 superClassLoc = ConsumeToken();
239 }
240 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 SmallVector<Decl *, 8> ProtocolRefs;
242 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000243 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000244 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000245 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
246 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000247 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
John McCalld226f652010-08-21 09:40:31 +0000249 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000250 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000251 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000252 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000253 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000254 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattnerdf195262007-10-09 17:51:17 +0000256 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000257 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000258
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000259 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000260 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000261}
262
John McCalld0014542009-12-03 22:31:13 +0000263/// The Objective-C property callback. This should be defined where
264/// it's used, but instead it's been lifted to here to support VS2005.
265struct Parser::ObjCPropertyCallback : FieldCallback {
266 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000268 ObjCDeclSpec &OCDS;
269 SourceLocation AtLoc;
270 tok::ObjCKeywordKind MethodImplKind;
271
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000272 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000274 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
275 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000276 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000277 MethodImplKind(MethodImplKind) {
278 }
279
John McCalld226f652010-08-21 09:40:31 +0000280 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000281 if (FD.D.getIdentifier() == 0) {
282 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
283 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000284 return 0;
John McCalld0014542009-12-03 22:31:13 +0000285 }
286 if (FD.BitfieldSize) {
287 P.Diag(AtLoc, diag::err_objc_property_bitfield)
288 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000289 return 0;
John McCalld0014542009-12-03 22:31:13 +0000290 }
291
292 // Install the property declarator into interfaceDecl.
293 IdentifierInfo *SelName =
294 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
295
296 Selector GetterSel =
297 P.PP.getSelectorTable().getNullarySelector(SelName);
298 IdentifierInfo *SetterName = OCDS.getSetterName();
299 Selector SetterSel;
300 if (SetterName)
301 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
302 else
303 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
304 P.PP.getSelectorTable(),
305 FD.D.getIdentifier());
306 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000307 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000308 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000309 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000310 &isOverridingProperty,
311 MethodImplKind);
312 if (!isOverridingProperty)
313 Props.push_back(Property);
314
315 return Property;
316 }
317};
318
Steve Naroffdac269b2007-08-20 21:31:48 +0000319/// objc-interface-decl-list:
320/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000321/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000322/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000323/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000324/// objc-interface-decl-list declaration
325/// objc-interface-decl-list ';'
326///
Steve Naroff294494e2007-08-22 16:35:03 +0000327/// objc-method-requirement: [OBJC2]
328/// @required
329/// @optional
330///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000331void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
332 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000333 SmallVector<Decl *, 32> allMethods;
334 SmallVector<Decl *, 16> allProperties;
335 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000336 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek782f2f52010-01-07 01:20:12 +0000338 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000339 Actions.ActOnObjCContainerStartDefinition(CDecl);
340
Steve Naroff294494e2007-08-22 16:35:03 +0000341 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000342 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000343 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000344 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000345 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000346 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000347 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
348 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000349 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
350 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000351 continue;
352 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000353 if (Tok.is(tok::l_paren)) {
354 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000355 ParseObjCMethodDecl(Tok.getLocation(),
356 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000357 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000358 continue;
359 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000360 // Ignore excess semicolons.
361 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000362 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000363 continue;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattnerbc662af2008-10-20 06:10:06 +0000366 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000367 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000368 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000370 // Code completion within an Objective-C interface.
371 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000372 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000373 ObjCImpDecl? Sema::PCC_ObjCImplementation
374 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000375 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000376 }
377
Chris Lattnere82a10f2008-10-20 05:46:22 +0000378 // If we don't have an @ directive, parse it as a function definition.
379 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000380 // The code below does not consume '}'s because it is afraid of eating the
381 // end of a namespace. Because of the way this code is structured, an
382 // erroneous r_brace would cause an infinite loop if not handled here.
383 if (Tok.is(tok::r_brace))
384 break;
John McCall0b7e6782011-03-24 11:26:52 +0000385 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000386 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000387 continue;
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnere82a10f2008-10-20 05:46:22 +0000390 // Otherwise, we have an @ directive, eat the @.
391 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000392 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000393 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000394 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000395 break;
396 }
397
Chris Lattnera2449b22008-10-20 05:57:40 +0000398 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnera2449b22008-10-20 05:57:40 +0000400 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000401 AtEnd.setBegin(AtLoc);
402 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000403 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000404 } else if (DirectiveKind == tok::objc_not_keyword) {
405 Diag(Tok, diag::err_objc_unknown_at);
406 SkipUntil(tok::semi);
407 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnerbc662af2008-10-20 06:10:06 +0000410 // Eat the identifier.
411 ConsumeToken();
412
Chris Lattnera2449b22008-10-20 05:57:40 +0000413 switch (DirectiveKind) {
414 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000415 // FIXME: If someone forgets an @end on a protocol, this loop will
416 // continue to eat up tons of stuff and spew lots of nonsense errors. It
417 // would probably be better to bail out if we saw an @class or @interface
418 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000419 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000420 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000421 SkipUntil(tok::r_brace, tok::at);
422 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000423
424 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000425 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000426 Diag(Tok, diag::err_objc_missing_end);
427 ConsumeToken();
428 break;
429
Chris Lattnera2449b22008-10-20 05:57:40 +0000430 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000431 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000433 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000434 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000435 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000436 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000437 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000438 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattnera2449b22008-10-20 05:57:40 +0000440 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000441 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000442 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000443
Chris Lattnere82a10f2008-10-20 05:46:22 +0000444 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000445 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000446 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000447 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000449 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000450 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000451
Chris Lattnere82a10f2008-10-20 05:46:22 +0000452 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000453 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000454 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
John McCall7da19ea2011-03-26 01:53:26 +0000456 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000457 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000458 }
Steve Naroff294494e2007-08-22 16:35:03 +0000459 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000460
461 // We break out of the big loop in two cases: when we see @end or when we see
462 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000463 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000464 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000465 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000466 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000467 ConsumeToken(); // the "end" identifier
468 else
469 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000472 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000473 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000474 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000475 allProperties.data(), allProperties.size(),
476 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000477}
478
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000479/// Parse property attribute declarations.
480///
481/// property-attr-decl: '(' property-attrlist ')'
482/// property-attrlist:
483/// property-attribute
484/// property-attrlist ',' property-attribute
485/// property-attribute:
486/// getter '=' identifier
487/// setter '=' identifier ':'
488/// readonly
489/// readwrite
490/// assign
491/// retain
492/// copy
493/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000494/// atomic
495/// strong
496/// weak
497/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000498///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000499void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000500 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000501 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000503 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000504 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000505 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000506 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000507 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000508 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000510 // If this is not an identifier at all, bail out early.
511 if (II == 0) {
512 MatchRHSPunctuation(tok::r_paren, LHSLoc);
513 return;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner156b0612008-10-20 07:37:22 +0000516 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner92e62b02008-11-20 04:42:34 +0000518 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000519 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000520 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000521 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000522 else if (II->isStr("unsafe_unretained"))
523 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000524 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000525 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000526 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000527 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000528 else if (II->isStr("strong"))
529 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000530 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000531 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000532 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000533 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000534 else if (II->isStr("atomic"))
535 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000536 else if (II->isStr("weak"))
537 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000538 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000539 bool IsSetter = II->getNameStart()[0] == 's';
540
Chris Lattnere00da7c2008-10-20 07:39:53 +0000541 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000542 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
543 diag::err_objc_expected_equal_for_getter;
544
545 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000546 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor4ad96852009-11-19 07:41:15 +0000548 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000549 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000550 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000551 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000552 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000553 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000554 }
555
Anders Carlsson42499be2010-10-02 17:45:21 +0000556
557 SourceLocation SelLoc;
558 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
559
560 if (!SelIdent) {
561 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
562 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000563 SkipUntil(tok::r_paren);
564 return;
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Anders Carlsson42499be2010-10-02 17:45:21 +0000567 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000568 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000569 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000571 if (ExpectAndConsume(tok::colon,
572 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000573 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000574 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000575 } else {
576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000577 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000578 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000579 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000580 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000581 SkipUntil(tok::r_paren);
582 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner156b0612008-10-20 07:37:22 +0000585 if (Tok.isNot(tok::comma))
586 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner156b0612008-10-20 07:37:22 +0000588 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner156b0612008-10-20 07:37:22 +0000591 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000592}
593
Steve Naroff3536b442007-09-06 21:24:23 +0000594/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000595/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000596/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000597///
598/// objc-instance-method: '-'
599/// objc-class-method: '+'
600///
Steve Naroff4985ace2007-08-22 18:35:33 +0000601/// objc-method-attributes: [OBJC2]
602/// __attribute__((deprecated))
603///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000604Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000605 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000606 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000607
Mike Stump1eb44332009-09-09 15:08:12 +0000608 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000609 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000610 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000611 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000612 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000613 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000614 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000615}
616
617/// objc-selector:
618/// identifier
619/// one of
620/// enum struct union if else while do for switch case default
621/// break continue return goto asm sizeof typeof __alignof
622/// unsigned long const short volatile signed restrict _Complex
623/// in out inout bycopy byref oneway int char float double void _Bool
624///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000625IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000626
Chris Lattnerff384912007-10-07 02:00:24 +0000627 switch (Tok.getKind()) {
628 default:
629 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000630 case tok::ampamp:
631 case tok::ampequal:
632 case tok::amp:
633 case tok::pipe:
634 case tok::tilde:
635 case tok::exclaim:
636 case tok::exclaimequal:
637 case tok::pipepipe:
638 case tok::pipeequal:
639 case tok::caret:
640 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000641 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000642 if (isalpha(ThisTok[0])) {
643 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
644 Tok.setKind(tok::identifier);
645 SelectorLoc = ConsumeToken();
646 return II;
647 }
648 return 0;
649 }
650
Chris Lattnerff384912007-10-07 02:00:24 +0000651 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000652 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000653 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000654 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000655 case tok::kw_break:
656 case tok::kw_case:
657 case tok::kw_catch:
658 case tok::kw_char:
659 case tok::kw_class:
660 case tok::kw_const:
661 case tok::kw_const_cast:
662 case tok::kw_continue:
663 case tok::kw_default:
664 case tok::kw_delete:
665 case tok::kw_do:
666 case tok::kw_double:
667 case tok::kw_dynamic_cast:
668 case tok::kw_else:
669 case tok::kw_enum:
670 case tok::kw_explicit:
671 case tok::kw_export:
672 case tok::kw_extern:
673 case tok::kw_false:
674 case tok::kw_float:
675 case tok::kw_for:
676 case tok::kw_friend:
677 case tok::kw_goto:
678 case tok::kw_if:
679 case tok::kw_inline:
680 case tok::kw_int:
681 case tok::kw_long:
682 case tok::kw_mutable:
683 case tok::kw_namespace:
684 case tok::kw_new:
685 case tok::kw_operator:
686 case tok::kw_private:
687 case tok::kw_protected:
688 case tok::kw_public:
689 case tok::kw_register:
690 case tok::kw_reinterpret_cast:
691 case tok::kw_restrict:
692 case tok::kw_return:
693 case tok::kw_short:
694 case tok::kw_signed:
695 case tok::kw_sizeof:
696 case tok::kw_static:
697 case tok::kw_static_cast:
698 case tok::kw_struct:
699 case tok::kw_switch:
700 case tok::kw_template:
701 case tok::kw_this:
702 case tok::kw_throw:
703 case tok::kw_true:
704 case tok::kw_try:
705 case tok::kw_typedef:
706 case tok::kw_typeid:
707 case tok::kw_typename:
708 case tok::kw_typeof:
709 case tok::kw_union:
710 case tok::kw_unsigned:
711 case tok::kw_using:
712 case tok::kw_virtual:
713 case tok::kw_void:
714 case tok::kw_volatile:
715 case tok::kw_wchar_t:
716 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000717 case tok::kw__Bool:
718 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000719 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000720 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000721 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000722 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000723 }
Steve Naroff294494e2007-08-22 16:35:03 +0000724}
725
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000726/// objc-for-collection-in: 'in'
727///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000728bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000729 // FIXME: May have to do additional look-ahead to only allow for
730 // valid tokens following an 'in'; such as an identifier, unary operators,
731 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000732 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000733 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000734}
735
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000737/// qualifier list and builds their bitmask representation in the input
738/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000739///
740/// objc-type-qualifiers:
741/// objc-type-qualifier
742/// objc-type-qualifiers objc-type-qualifier
743///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000744void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000745 Declarator::TheContext Context) {
746 assert(Context == Declarator::ObjCParameterContext ||
747 Context == Declarator::ObjCResultContext);
748
Chris Lattnere8b724d2007-12-12 06:56:32 +0000749 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000750 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000751 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000752 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000753 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000754 }
755
Chris Lattnercb53b362007-12-27 19:57:00 +0000756 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000757 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattnere8b724d2007-12-12 06:56:32 +0000759 const IdentifierInfo *II = Tok.getIdentifierInfo();
760 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000762 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000765 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000766 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
768 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
769 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
770 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
771 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
772 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000773 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000774 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000775 ConsumeToken();
776 II = 0;
777 break;
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattnere8b724d2007-12-12 06:56:32 +0000780 // If this wasn't a recognized qualifier, bail out.
781 if (II) return;
782 }
783}
784
John McCallcdda47f2011-10-01 09:56:14 +0000785/// Take all the decl attributes out of the given list and add
786/// them to the given attribute set.
787static void takeDeclAttributes(ParsedAttributes &attrs,
788 AttributeList *list) {
789 while (list) {
790 AttributeList *cur = list;
791 list = cur->getNext();
792
793 if (!cur->isUsedAsTypeAttr()) {
794 // Clear out the next pointer. We're really completely
795 // destroying the internal invariants of the declarator here,
796 // but it doesn't matter because we're done with it.
797 cur->setNext(0);
798 attrs.add(cur);
799 }
800 }
801}
802
803/// takeDeclAttributes - Take all the decl attributes from the given
804/// declarator and add them to the given list.
805static void takeDeclAttributes(ParsedAttributes &attrs,
806 Declarator &D) {
807 // First, take ownership of all attributes.
808 attrs.getPool().takeAllFrom(D.getAttributePool());
809 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
810
811 // Now actually move the attributes over.
812 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
813 takeDeclAttributes(attrs, D.getAttributes());
814 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
815 takeDeclAttributes(attrs,
816 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
817}
818
Chris Lattnere8b724d2007-12-12 06:56:32 +0000819/// objc-type-name:
820/// '(' objc-type-qualifiers[opt] type-name ')'
821/// '(' objc-type-qualifiers[opt] ')'
822///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000823ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000824 Declarator::TheContext context,
825 ParsedAttributes *paramAttrs) {
826 assert(context == Declarator::ObjCParameterContext ||
827 context == Declarator::ObjCResultContext);
828 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
829
Chris Lattnerdf195262007-10-09 17:51:17 +0000830 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattner4a76b292008-10-22 03:52:06 +0000832 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000833 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000834 ObjCDeclContextSwitch ObjCDC(*this);
835
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000836 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000837 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000838
John McCallb3d87482010-08-24 05:47:05 +0000839 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000840 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000841 // Parse an abstract declarator.
842 DeclSpec declSpec(AttrFactory);
843 declSpec.setObjCQualifiers(&DS);
844 ParseSpecifierQualifierList(declSpec);
845 Declarator declarator(declSpec, context);
846 ParseDeclarator(declarator);
847
848 // If that's not invalid, extract a type.
849 if (!declarator.isInvalidType()) {
850 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
851 if (!type.isInvalid())
852 Ty = type.get();
853
854 // If we're parsing a parameter, steal all the decl attributes
855 // and add them to the decl spec.
856 if (context == Declarator::ObjCParameterContext)
857 takeDeclAttributes(*paramAttrs, declarator);
858 }
859 } else if (context == Declarator::ObjCResultContext &&
860 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000861 if (!Ident_instancetype)
862 Ident_instancetype = PP.getIdentifierInfo("instancetype");
863
864 if (Tok.getIdentifierInfo() == Ident_instancetype) {
865 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
866 ConsumeToken();
867 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000868 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000869
Steve Naroffd7333c22008-10-21 14:15:04 +0000870 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000871 ConsumeParen();
872 else if (Tok.getLocation() == TypeStartLoc) {
873 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000874 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000875 SkipUntil(tok::r_paren);
876 } else {
877 // Otherwise, we found *something*, but didn't get a ')' in the right
878 // place. Emit an error then return what we have as the type.
879 MatchRHSPunctuation(tok::r_paren, LParenLoc);
880 }
Steve Narofff28b2642007-09-05 23:30:30 +0000881 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000882}
883
884/// objc-method-decl:
885/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000886/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000887/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000888/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000889///
890/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000891/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000892/// objc-keyword-selector objc-keyword-decl
893///
894/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000895/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
896/// objc-selector ':' objc-keyword-attributes[opt] identifier
897/// ':' objc-type-name objc-keyword-attributes[opt] identifier
898/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000899///
Steve Naroff4985ace2007-08-22 18:35:33 +0000900/// objc-parmlist:
901/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000902///
Steve Naroff4985ace2007-08-22 18:35:33 +0000903/// objc-parms:
904/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000905///
Steve Naroff4985ace2007-08-22 18:35:33 +0000906/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000907/// , ...
908///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000909/// objc-keyword-attributes: [OBJC2]
910/// __attribute__((unused))
911///
John McCalld226f652010-08-21 09:40:31 +0000912Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000913 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000914 tok::ObjCKeywordKind MethodImplKind,
915 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000916 ParsingDeclRAIIObject PD(*this);
917
Douglas Gregore8f5a172010-04-07 00:21:17 +0000918 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000919 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000920 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000921 cutOffParsing();
922 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000923 }
924
Chris Lattnere8904e92008-08-23 01:48:03 +0000925 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000926 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000929 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek9e049352010-02-18 23:05:16 +0000931 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000932 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000933 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000934 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000935
Douglas Gregore8f5a172010-04-07 00:21:17 +0000936 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000937 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000938 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000939 cutOffParsing();
940 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000941 }
942
Ted Kremenek9e049352010-02-18 23:05:16 +0000943 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000944 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000945 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000946
Steve Naroff84c43102009-02-11 20:43:13 +0000947 // An unnamed colon is valid.
948 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000949 Diag(Tok, diag::err_expected_selector_for_method)
950 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000951 // Skip until we get a ; or {}.
952 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000953 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattner5f9e2722011-07-23 10:55:15 +0000956 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000957 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000958 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000959 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000960 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattnerff384912007-10-07 02:00:24 +0000962 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000963 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000964 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000965 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000966 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000967 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000968 methodAttrs.getList(), MethodImplKind,
969 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000970 PD.complete(Result);
971 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000972 }
Steve Narofff28b2642007-09-05 23:30:30 +0000973
Chris Lattner5f9e2722011-07-23 10:55:15 +0000974 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +0000975 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000976 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000977 ParseScope PrototypeScope(this,
978 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000979
980 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000981
Chris Lattnerff384912007-10-07 02:00:24 +0000982 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000983 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000984 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattnerff384912007-10-07 02:00:24 +0000986 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000987 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000988 Diag(Tok, diag::err_expected_colon);
989 break;
990 }
991 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000992
John McCallb3d87482010-08-24 05:47:05 +0000993 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000994 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +0000995 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
996 Declarator::ObjCParameterContext,
997 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000998
Chris Lattnerff384912007-10-07 02:00:24 +0000999 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001000 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001001 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001002 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001003 MaybeParseGNUAttributes(paramAttrs);
1004 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001005 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001006
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001007 // Code completion for the next piece of the selector.
1008 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001009 KeyIdents.push_back(SelIdent);
1010 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1011 mType == tok::minus,
1012 /*AtParameterName=*/true,
1013 ReturnType,
1014 KeyIdents.data(),
1015 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001016 cutOffParsing();
1017 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001018 }
1019
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001021 Diag(Tok, diag::err_expected_ident); // missing argument name.
1022 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattnere294d3f2009-04-11 18:57:04 +00001025 ArgInfo.Name = Tok.getIdentifierInfo();
1026 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001027 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattnere294d3f2009-04-11 18:57:04 +00001029 ArgInfos.push_back(ArgInfo);
1030 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001031 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001032
John McCall0b7e6782011-03-24 11:26:52 +00001033 // Make sure the attributes persist.
1034 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1035
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001036 // Code completion for the next piece of the selector.
1037 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001038 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1039 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001040 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001041 ReturnType,
1042 KeyIdents.data(),
1043 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001044 cutOffParsing();
1045 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001046 }
1047
Chris Lattnerff384912007-10-07 02:00:24 +00001048 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001049 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001050 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001051 break;
1052 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Steve Naroff335eafa2007-11-15 12:35:21 +00001055 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattnerff384912007-10-07 02:00:24 +00001057 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001058 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001059 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001060 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001061 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001062 ConsumeToken();
1063 break;
1064 }
John McCall0b7e6782011-03-24 11:26:52 +00001065 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001066 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001067 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001068 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1069 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001070 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001071 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001072 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1073 ParmDecl.getIdentifierLoc(),
1074 Param,
1075 0));
1076
Chris Lattnerff384912007-10-07 02:00:24 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001079 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001080 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001081 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001082 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001083
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001084 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001085 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001086
Chris Lattnerff384912007-10-07 02:00:24 +00001087 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1088 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001089 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001090 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001091 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001092 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001093 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001094 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001095 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001096
John McCall54abf7d2009-11-04 02:18:39 +00001097 PD.complete(Result);
1098 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001099}
1100
Steve Naroffdac269b2007-08-20 21:31:48 +00001101/// objc-protocol-refs:
1102/// '<' identifier-list '>'
1103///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001104bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001105ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1106 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001107 bool WarnOnDeclarations,
1108 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001109 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001111 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Chris Lattner5f9e2722011-07-23 10:55:15 +00001113 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattnere13b9592008-07-26 04:03:38 +00001115 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001116 if (Tok.is(tok::code_completion)) {
1117 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1118 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001119 cutOffParsing();
1120 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001121 }
1122
Chris Lattnere13b9592008-07-26 04:03:38 +00001123 if (Tok.isNot(tok::identifier)) {
1124 Diag(Tok, diag::err_expected_ident);
1125 SkipUntil(tok::greater);
1126 return true;
1127 }
1128 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1129 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001130 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001131 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattnere13b9592008-07-26 04:03:38 +00001133 if (Tok.isNot(tok::comma))
1134 break;
1135 ConsumeToken();
1136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Chris Lattnere13b9592008-07-26 04:03:38 +00001138 // Consume the '>'.
1139 if (Tok.isNot(tok::greater)) {
1140 Diag(Tok, diag::err_expected_greater);
1141 return true;
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Chris Lattnere13b9592008-07-26 04:03:38 +00001144 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Chris Lattnere13b9592008-07-26 04:03:38 +00001146 // Convert the list of protocols identifiers into a list of protocol decls.
1147 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1148 &ProtocolIdents[0], ProtocolIdents.size(),
1149 Protocols);
1150 return false;
1151}
1152
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001153/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1154/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001155bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001156 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1157 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1158 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001159 SmallVector<Decl *, 8> ProtocolDecl;
1160 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001161 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1162 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001163 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1164 ProtocolLocs.data(), LAngleLoc);
1165 if (EndProtoLoc.isValid())
1166 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001167 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001168}
1169
1170
Steve Naroffdac269b2007-08-20 21:31:48 +00001171/// objc-class-instance-variables:
1172/// '{' objc-instance-variable-decl-list[opt] '}'
1173///
1174/// objc-instance-variable-decl-list:
1175/// objc-visibility-spec
1176/// objc-instance-variable-decl ';'
1177/// ';'
1178/// objc-instance-variable-decl-list objc-visibility-spec
1179/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1180/// objc-instance-variable-decl-list ';'
1181///
1182/// objc-visibility-spec:
1183/// @private
1184/// @protected
1185/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001186/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001187///
1188/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001189/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001190///
John McCalld226f652010-08-21 09:40:31 +00001191void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001192 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001193 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001195 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001196
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001197 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001198
Steve Naroffddbff782007-08-21 21:17:12 +00001199 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Steve Naroffddbff782007-08-21 21:17:12 +00001201 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001202 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001203 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Steve Naroffddbff782007-08-21 21:17:12 +00001205 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001206 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001207 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001208 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001209 ConsumeToken();
1210 continue;
1211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Steve Naroffddbff782007-08-21 21:17:12 +00001213 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001214 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001215 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001216
1217 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001218 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001219 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001220 }
1221
Steve Naroff861cf3e2007-08-23 18:16:40 +00001222 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001223 case tok::objc_private:
1224 case tok::objc_public:
1225 case tok::objc_protected:
1226 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001227 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001228 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001229 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001230 default:
1231 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001232 continue;
1233 }
1234 }
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001236 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001237 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001238 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001239 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001240 }
1241
John McCallbdd563e2009-11-03 02:38:08 +00001242 struct ObjCIvarCallback : FieldCallback {
1243 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001244 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001245 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001246 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001247
John McCalld226f652010-08-21 09:40:31 +00001248 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001249 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001250 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1251 }
1252
John McCalld226f652010-08-21 09:40:31 +00001253 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001254 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001255 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001256 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001257 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001258 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001259 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001260 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001261 if (Field)
1262 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001263 return Field;
1264 }
1265 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001266
Chris Lattnere1359422008-04-10 06:46:29 +00001267 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001268 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001269 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Chris Lattnerdf195262007-10-09 17:51:17 +00001271 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001272 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001273 } else {
1274 Diag(Tok, diag::err_expected_semi_decl_list);
1275 // Skip to end of block or statement
1276 SkipUntil(tok::r_brace, true, true);
1277 }
1278 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001279 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001280 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1281 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001282 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001283 // Call ActOnFields() even if we don't have any decls. This is useful
1284 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001285 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001286 AllIvarDecls,
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001287 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001288 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001289}
Steve Naroffdac269b2007-08-20 21:31:48 +00001290
1291/// objc-protocol-declaration:
1292/// objc-protocol-definition
1293/// objc-protocol-forward-reference
1294///
1295/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001296/// @protocol identifier
1297/// objc-protocol-refs[opt]
1298/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001299/// @end
1300///
1301/// objc-protocol-forward-reference:
1302/// @protocol identifier-list ';'
1303///
1304/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001305/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001306/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001307Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001308 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001309 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001310 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1311 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Douglas Gregor083128f2009-11-18 04:49:41 +00001313 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001314 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001315 cutOffParsing();
1316 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001317 }
1318
Chris Lattnerdf195262007-10-09 17:51:17 +00001319 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001320 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001321 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001322 }
1323 // Save the protocol name, then consume it.
1324 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1325 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chris Lattnerdf195262007-10-09 17:51:17 +00001327 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001328 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001329 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001330 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001331 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Chris Lattnerdf195262007-10-09 17:51:17 +00001334 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001335 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001336 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1337
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001338 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001339 while (1) {
1340 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001341 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001342 Diag(Tok, diag::err_expected_ident);
1343 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001344 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001345 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001346 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1347 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001348 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Chris Lattnerdf195262007-10-09 17:51:17 +00001350 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001351 break;
1352 }
1353 // Consume the ';'.
1354 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001355 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Steve Naroffe440eb82007-10-10 17:32:04 +00001357 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001358 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001359 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001360 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001363 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001364 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001365
Chris Lattner5f9e2722011-07-23 10:55:15 +00001366 SmallVector<Decl *, 8> ProtocolRefs;
1367 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001368 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001369 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1370 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001371 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
John McCalld226f652010-08-21 09:40:31 +00001373 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001374 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001375 ProtocolRefs.data(),
1376 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001377 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001378 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001379
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001380 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001381 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001382}
Steve Naroffdac269b2007-08-20 21:31:48 +00001383
1384/// objc-implementation:
1385/// objc-class-implementation-prologue
1386/// objc-category-implementation-prologue
1387///
1388/// objc-class-implementation-prologue:
1389/// @implementation identifier objc-superclass[opt]
1390/// objc-class-instance-variables[opt]
1391///
1392/// objc-category-implementation-prologue:
1393/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001394Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001395 SourceLocation atLoc) {
1396 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1397 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1398 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001400 // Code completion after '@implementation'.
1401 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001402 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001403 cutOffParsing();
1404 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001405 }
1406
Chris Lattnerdf195262007-10-09 17:51:17 +00001407 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001408 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001409 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001410 }
1411 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001412 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001413 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001414
1415 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001417 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001418 SourceLocation categoryLoc, rparenLoc;
1419 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001421 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001422 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001423 cutOffParsing();
1424 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001425 }
1426
Chris Lattnerdf195262007-10-09 17:51:17 +00001427 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001428 categoryId = Tok.getIdentifierInfo();
1429 categoryLoc = ConsumeToken();
1430 } else {
1431 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001432 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001433 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001434 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001435 Diag(Tok, diag::err_expected_rparen);
1436 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001437 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001438 }
1439 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001440 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001441 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001442 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001443
1444 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001445 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001446 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001447 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001448 }
1449 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001450 SourceLocation superClassLoc;
1451 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001452 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001453 // We have a super class
1454 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001455 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001456 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001457 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001458 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001459 superClassId = Tok.getIdentifierInfo();
1460 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001461 }
John McCalld226f652010-08-21 09:40:31 +00001462 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001463 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001464 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Steve Naroff60fccee2007-10-29 21:38:07 +00001466 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001467 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1468
1469 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001470 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001471 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001472 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001473}
Steve Naroff60fccee2007-10-29 21:38:07 +00001474
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001475Parser::DeclGroupPtrTy
1476Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001477 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1478 "ParseObjCAtEndDeclaration(): Expected @end");
1479 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001480 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001481 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001482 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1483 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1484 DeclsInGroup.push_back(D);
1485 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001486 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001487
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001488 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001489 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001490 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001491 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001492 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001493 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001494 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001495
1496 LateParsedObjCMethods.clear();
1497 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001498 return Actions.BuildDeclaratorGroup(
1499 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001500}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001501
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001502Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1503 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001504 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001505 return Actions.ConvertDeclToDeclGroup(0);
1506 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001507 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001508 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1509}
1510
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001511/// compatibility-alias-decl:
1512/// @compatibility_alias alias-name class-name ';'
1513///
John McCalld226f652010-08-21 09:40:31 +00001514Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001515 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1516 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1517 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001518 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001519 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001520 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001521 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001522 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1523 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001524 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001525 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001526 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001527 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001528 IdentifierInfo *classId = Tok.getIdentifierInfo();
1529 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001530 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1531 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001532 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1533 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001534}
1535
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001536/// property-synthesis:
1537/// @synthesize property-ivar-list ';'
1538///
1539/// property-ivar-list:
1540/// property-ivar
1541/// property-ivar-list ',' property-ivar
1542///
1543/// property-ivar:
1544/// identifier
1545/// identifier '=' identifier
1546///
John McCalld226f652010-08-21 09:40:31 +00001547Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001548 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1549 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001550 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregorb328c422009-11-18 19:45:45 +00001552 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001553 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001554 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001555 cutOffParsing();
1556 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001557 }
1558
Douglas Gregorb328c422009-11-18 19:45:45 +00001559 if (Tok.isNot(tok::identifier)) {
1560 Diag(Tok, diag::err_synthesized_property_name);
1561 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001562 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001563 }
1564
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001565 IdentifierInfo *propertyIvar = 0;
1566 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1567 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001568 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001569 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001570 // property '=' ivar-name
1571 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001572
1573 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001574 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001575 cutOffParsing();
1576 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001577 }
1578
Chris Lattnerdf195262007-10-09 17:51:17 +00001579 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001580 Diag(Tok, diag::err_expected_ident);
1581 break;
1582 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001583 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001584 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001585 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001586 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001587 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001588 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001589 break;
1590 ConsumeToken(); // consume ','
1591 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001592 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001593 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001594}
1595
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001596/// property-dynamic:
1597/// @dynamic property-list
1598///
1599/// property-list:
1600/// identifier
1601/// property-list ',' identifier
1602///
John McCalld226f652010-08-21 09:40:31 +00001603Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001604 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1605 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001606 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001607 while (true) {
1608 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 Gregor424b2a52009-11-18 22:56:13 +00001612 }
1613
1614 if (Tok.isNot(tok::identifier)) {
1615 Diag(Tok, diag::err_expected_ident);
1616 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001617 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001618 }
1619
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001620 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1621 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001622 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001623 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001624
Chris Lattnerdf195262007-10-09 17:51:17 +00001625 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001626 break;
1627 ConsumeToken(); // consume ','
1628 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001629 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001630 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001631}
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001633/// objc-throw-statement:
1634/// throw expression[opt];
1635///
John McCall60d7b3a2010-08-24 06:29:42 +00001636StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1637 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001638 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001639 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001640 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001641 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001642 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001643 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001644 }
1645 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001646 // consume ';'
1647 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001648 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001649}
1650
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001651/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001652/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001653///
John McCall60d7b3a2010-08-24 06:29:42 +00001654StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001655Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001656 ConsumeToken(); // consume synchronized
1657 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001658 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001659 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001660 }
John McCall07524032011-07-27 21:50:02 +00001661
1662 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001663 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001664 ExprResult operand(ParseExpression());
1665
1666 if (Tok.is(tok::r_paren)) {
1667 ConsumeParen(); // ')'
1668 } else {
1669 if (!operand.isInvalid())
1670 Diag(Tok, diag::err_expected_rparen);
1671
1672 // Skip forward until we see a left brace, but don't consume it.
1673 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001674 }
John McCall07524032011-07-27 21:50:02 +00001675
1676 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001677 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001678 if (!operand.isInvalid())
1679 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001680 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001681 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001682
John McCall07524032011-07-27 21:50:02 +00001683 // Check the @synchronized operand now.
1684 if (!operand.isInvalid())
1685 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001686
John McCall07524032011-07-27 21:50:02 +00001687 // Parse the compound statement within a new scope.
1688 ParseScope bodyScope(this, Scope::DeclScope);
1689 StmtResult body(ParseCompoundStatementBody());
1690 bodyScope.Exit();
1691
1692 // If there was a semantic or parse error earlier with the
1693 // operand, fail now.
1694 if (operand.isInvalid())
1695 return StmtError();
1696
1697 if (body.isInvalid())
1698 body = Actions.ActOnNullStmt(Tok.getLocation());
1699
1700 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001701}
1702
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001703/// objc-try-catch-statement:
1704/// @try compound-statement objc-catch-list[opt]
1705/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1706///
1707/// objc-catch-list:
1708/// @catch ( parameter-declaration ) compound-statement
1709/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1710/// catch-parameter-declaration:
1711/// parameter-declaration
1712/// '...' [OBJC2]
1713///
John McCall60d7b3a2010-08-24 06:29:42 +00001714StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001715 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001716
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001717 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001718 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001719 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001720 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001721 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001722 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001723 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001724 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001725 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001726 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001727 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001728 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001729
Chris Lattnerdf195262007-10-09 17:51:17 +00001730 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001731 // At this point, we need to lookahead to determine if this @ is the start
1732 // of an @catch or @finally. We don't want to consume the @ token if this
1733 // is an @try or @encode or something else.
1734 Token AfterAt = GetLookAheadToken(1);
1735 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1736 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1737 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001738
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001739 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001740 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001741 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001742 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001743 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001744 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001745 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001746 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001747 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001748 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001749 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001750 ParseDeclarator(ParmDecl);
1751
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001752 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001753 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001754 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001755 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001756 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Steve Naroff93a25952009-04-07 22:56:58 +00001758 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Steve Naroff93a25952009-04-07 22:56:58 +00001760 if (Tok.is(tok::r_paren))
1761 RParenLoc = ConsumeParen();
1762 else // Skip over garbage, until we get to ')'. Eat the ')'.
1763 SkipUntil(tok::r_paren, true, false);
1764
John McCall60d7b3a2010-08-24 06:29:42 +00001765 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001766 if (Tok.is(tok::l_brace))
1767 CatchBody = ParseCompoundStatementBody();
1768 else
1769 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001770 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001771 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001772
John McCall60d7b3a2010-08-24 06:29:42 +00001773 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001774 RParenLoc,
1775 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001776 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001777 if (!Catch.isInvalid())
1778 CatchStmts.push_back(Catch.release());
1779
Steve Naroff64515f32008-02-05 21:27:35 +00001780 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001781 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1782 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001783 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001784 }
1785 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001786 } else {
1787 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001788 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001789 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001790
John McCall60d7b3a2010-08-24 06:29:42 +00001791 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001792 if (Tok.is(tok::l_brace))
1793 FinallyBody = ParseCompoundStatementBody();
1794 else
1795 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001796 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001797 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001798 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001799 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001800 catch_or_finally_seen = true;
1801 break;
1802 }
1803 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001804 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001805 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001806 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001807 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001808
John McCall9ae2f072010-08-23 23:25:46 +00001809 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001810 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001811 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001812}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001813
John McCallf85e1932011-06-15 23:02:42 +00001814/// objc-autoreleasepool-statement:
1815/// @autoreleasepool compound-statement
1816///
1817StmtResult
1818Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1819 ConsumeToken(); // consume autoreleasepool
1820 if (Tok.isNot(tok::l_brace)) {
1821 Diag(Tok, diag::err_expected_lbrace);
1822 return StmtError();
1823 }
1824 // Enter a scope to hold everything within the compound stmt. Compound
1825 // statements can always hold declarations.
1826 ParseScope BodyScope(this, Scope::DeclScope);
1827
1828 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1829
1830 BodyScope.Exit();
1831 if (AutoreleasePoolBody.isInvalid())
1832 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1833 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1834 AutoreleasePoolBody.take());
1835}
1836
Steve Naroff3536b442007-09-06 21:24:23 +00001837/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001838///
John McCalld226f652010-08-21 09:40:31 +00001839Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001840 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001841
John McCallf312b1e2010-08-26 23:41:50 +00001842 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1843 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001845 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001846 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001847 if (ObjCImpDecl) {
1848 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001849 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001850 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001851 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001852 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001853
Steve Naroff409be832007-11-11 19:54:21 +00001854 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001855 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001856 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Steve Naroff409be832007-11-11 19:54:21 +00001858 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1859 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Steve Naroff409be832007-11-11 19:54:21 +00001861 // If we didn't find the '{', bail out.
1862 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001863 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001864 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001865 // Allow the rest of sema to find private method decl implementations.
1866 if (MDecl)
1867 Actions.AddAnyMethodToGlobalPool(MDecl);
1868
1869 // Consume the tokens and store them for later parsing.
1870 LexedMethod* LM = new LexedMethod(this, MDecl);
1871 LateParsedObjCMethods.push_back(LM);
1872 CachedTokens &Toks = LM->Toks;
1873 // Begin by storing the '{' token.
1874 Toks.push_back(Tok);
1875 ConsumeBrace();
1876 // Consume everything up to (and including) the matching right brace.
1877 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001878 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001879}
Anders Carlsson55085182007-08-21 17:43:55 +00001880
John McCall60d7b3a2010-08-24 06:29:42 +00001881StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001882 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001883 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001884 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001885 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001886 }
1887
1888 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001889 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001890
1891 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001892 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001893
1894 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001895 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001896
1897 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1898 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001899
John McCall60d7b3a2010-08-24 06:29:42 +00001900 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001901 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001902 // If the expression is invalid, skip ahead to the next semicolon. Not
1903 // doing this opens us up to the possibility of infinite loops if
1904 // ParseExpression does not consume any tokens.
1905 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001906 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001907 }
Chris Lattner5d803162009-12-07 16:33:19 +00001908
Steve Naroff64515f32008-02-05 21:27:35 +00001909 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001910 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001911 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001912}
1913
John McCall60d7b3a2010-08-24 06:29:42 +00001914ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001915 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001916 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001917 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001918 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001919 return ExprError();
1920
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001921 case tok::string_literal: // primary-expression: string-literal
1922 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001923 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001924 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001925 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001926 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001927
Chris Lattner4fef81d2008-08-05 06:19:09 +00001928 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1929 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001930 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001931 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001932 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001933 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001934 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001935 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001936 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001937 }
Anders Carlsson55085182007-08-21 17:43:55 +00001938 }
Anders Carlsson55085182007-08-21 17:43:55 +00001939}
1940
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001941/// \brirg Parse the receiver of an Objective-C++ message send.
1942///
1943/// This routine parses the receiver of a message send in
1944/// Objective-C++ either as a type or as an expression. Note that this
1945/// routine must not be called to parse a send to 'super', since it
1946/// has no way to return such a result.
1947///
1948/// \param IsExpr Whether the receiver was parsed as an expression.
1949///
1950/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1951/// IsExpr is true), the parsed expression. If the receiver was parsed
1952/// as a type (\c IsExpr is false), the parsed type.
1953///
1954/// \returns True if an error occurred during parsing or semantic
1955/// analysis, in which case the arguments do not have valid
1956/// values. Otherwise, returns false for a successful parse.
1957///
1958/// objc-receiver: [C++]
1959/// 'super' [not parsed here]
1960/// expression
1961/// simple-type-specifier
1962/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001963bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001964 InMessageExpressionRAIIObject InMessage(*this, true);
1965
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001966 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1967 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1968 TryAnnotateTypeOrScopeToken();
1969
1970 if (!isCXXSimpleTypeSpecifier()) {
1971 // objc-receiver:
1972 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001973 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001974 if (Receiver.isInvalid())
1975 return true;
1976
1977 IsExpr = true;
1978 TypeOrExpr = Receiver.take();
1979 return false;
1980 }
1981
1982 // objc-receiver:
1983 // typename-specifier
1984 // simple-type-specifier
1985 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001986 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001987 ParseCXXSimpleTypeSpecifier(DS);
1988
1989 if (Tok.is(tok::l_paren)) {
1990 // If we see an opening parentheses at this point, we are
1991 // actually parsing an expression that starts with a
1992 // function-style cast, e.g.,
1993 //
1994 // postfix-expression:
1995 // simple-type-specifier ( expression-list [opt] )
1996 // typename-specifier ( expression-list [opt] )
1997 //
1998 // Parse the remainder of this case, then the (optional)
1999 // postfix-expression suffix, followed by the (optional)
2000 // right-hand side of the binary expression. We have an
2001 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002002 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002003 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002004 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002005 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002006 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002007 if (Receiver.isInvalid())
2008 return true;
2009
2010 IsExpr = true;
2011 TypeOrExpr = Receiver.take();
2012 return false;
2013 }
2014
2015 // We have a class message. Turn the simple-type-specifier or
2016 // typename-specifier we parsed into a type and parse the
2017 // remainder of the class message.
2018 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002019 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002020 if (Type.isInvalid())
2021 return true;
2022
2023 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002024 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002025 return false;
2026}
2027
Douglas Gregor1b730e82010-05-31 14:40:22 +00002028/// \brief Determine whether the parser is currently referring to a an
2029/// Objective-C message send, using a simplified heuristic to avoid overhead.
2030///
2031/// This routine will only return true for a subset of valid message-send
2032/// expressions.
2033bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002034 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002035 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002036 return GetLookAheadToken(1).is(tok::identifier) &&
2037 GetLookAheadToken(2).is(tok::identifier);
2038}
2039
Douglas Gregor9497a732010-09-16 01:51:54 +00002040bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2041 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2042 InMessageExpression)
2043 return false;
2044
2045
2046 ParsedType Type;
2047
2048 if (Tok.is(tok::annot_typename))
2049 Type = getTypeAnnotation(Tok);
2050 else if (Tok.is(tok::identifier))
2051 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2052 getCurScope());
2053 else
2054 return false;
2055
2056 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2057 const Token &AfterNext = GetLookAheadToken(2);
2058 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2059 if (Tok.is(tok::identifier))
2060 TryAnnotateTypeOrScopeToken();
2061
2062 return Tok.is(tok::annot_typename);
2063 }
2064 }
2065
2066 return false;
2067}
2068
Mike Stump1eb44332009-09-09 15:08:12 +00002069/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002070/// '[' objc-receiver objc-message-args ']'
2071///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002072/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002073/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002074/// expression
2075/// class-name
2076/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002077///
John McCall60d7b3a2010-08-24 06:29:42 +00002078ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002079 assert(Tok.is(tok::l_square) && "'[' expected");
2080 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2081
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002082 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002083 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002084 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002085 return ExprError();
2086 }
2087
Douglas Gregor0fbda682010-09-15 14:51:05 +00002088 InMessageExpressionRAIIObject InMessage(*this, true);
2089
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002090 if (getLang().CPlusPlus) {
2091 // We completely separate the C and C++ cases because C++ requires
2092 // more complicated (read: slower) parsing.
2093
2094 // Handle send to super.
2095 // FIXME: This doesn't benefit from the same typo-correction we
2096 // get in Objective-C.
2097 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002098 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002099 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2100 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002101
2102 // Parse the receiver, which is either a type or an expression.
2103 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002104 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002105 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2106 SkipUntil(tok::r_square);
2107 return ExprError();
2108 }
2109
2110 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002111 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2112 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002113 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002114
2115 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002116 ParsedType::getFromOpaquePtr(TypeOrExpr),
2117 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002118 }
2119
2120 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002121 IdentifierInfo *Name = Tok.getIdentifierInfo();
2122 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002123 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002124 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002125 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002126 NextToken().is(tok::period),
2127 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002128 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002129 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2130 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002131
John McCallf312b1e2010-08-26 23:41:50 +00002132 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002133 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002134 SkipUntil(tok::r_square);
2135 return ExprError();
2136 }
2137
Douglas Gregor1569f952010-04-21 20:38:13 +00002138 ConsumeToken(); // the type name
2139
2140 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002141 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002142
John McCallf312b1e2010-08-26 23:41:50 +00002143 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002144 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002145 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002146 }
Chris Lattner699b6612008-01-25 18:59:06 +00002147 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002148
2149 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002150 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002151 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002152 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002153 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002154 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002155
John McCallb3d87482010-08-24 05:47:05 +00002156 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2157 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002158}
Sebastian Redl1d922962008-12-13 15:32:12 +00002159
Douglas Gregor2725ca82010-04-21 19:57:20 +00002160/// \brief Parse the remainder of an Objective-C message following the
2161/// '[' objc-receiver.
2162///
2163/// This routine handles sends to super, class messages (sent to a
2164/// class name), and instance messages (sent to an object), and the
2165/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2166/// ReceiverExpr, respectively. Only one of these parameters may have
2167/// a valid value.
2168///
2169/// \param LBracLoc The location of the opening '['.
2170///
2171/// \param SuperLoc If this is a send to 'super', the location of the
2172/// 'super' keyword that indicates a send to the superclass.
2173///
2174/// \param ReceiverType If this is a class message, the type of the
2175/// class we are sending a message to.
2176///
2177/// \param ReceiverExpr If this is an instance message, the expression
2178/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002179///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002180/// objc-message-args:
2181/// objc-selector
2182/// objc-keywordarg-list
2183///
2184/// objc-keywordarg-list:
2185/// objc-keywordarg
2186/// objc-keywordarg-list objc-keywordarg
2187///
Mike Stump1eb44332009-09-09 15:08:12 +00002188/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002189/// selector-name[opt] ':' objc-keywordexpr
2190///
2191/// objc-keywordexpr:
2192/// nonempty-expr-list
2193///
2194/// nonempty-expr-list:
2195/// assignment-expression
2196/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002197///
John McCall60d7b3a2010-08-24 06:29:42 +00002198ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002199Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002200 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002201 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002202 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002203 InMessageExpressionRAIIObject InMessage(*this, true);
2204
Steve Naroffc4df6d22009-11-07 02:08:14 +00002205 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002206 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002207 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2208 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002209 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002210 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2211 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002212 else
John McCall9ae2f072010-08-23 23:25:46 +00002213 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002214 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002215 cutOffParsing();
2216 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002217 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002218
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002219 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002220 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002221 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002222
Chris Lattner5f9e2722011-07-23 10:55:15 +00002223 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002224 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002225 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002226
Chris Lattnerdf195262007-10-09 17:51:17 +00002227 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002228 while (1) {
2229 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002230 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002231 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002232
Chris Lattnerdf195262007-10-09 17:51:17 +00002233 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002234 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002235 // We must manually skip to a ']', otherwise the expression skipper will
2236 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2237 // the enclosing expression.
2238 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002239 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002240 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002241
Steve Naroff68d331a2007-09-27 14:38:14 +00002242 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002243 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002244
2245 if (Tok.is(tok::code_completion)) {
2246 if (SuperLoc.isValid())
2247 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2248 KeyIdents.data(),
2249 KeyIdents.size(),
2250 /*AtArgumentEpression=*/true);
2251 else if (ReceiverType)
2252 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2253 KeyIdents.data(),
2254 KeyIdents.size(),
2255 /*AtArgumentEpression=*/true);
2256 else
2257 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2258 KeyIdents.data(),
2259 KeyIdents.size(),
2260 /*AtArgumentEpression=*/true);
2261
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002262 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002263 return ExprError();
2264 }
2265
John McCall60d7b3a2010-08-24 06:29:42 +00002266 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002267 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002268 // We must manually skip to a ']', otherwise the expression skipper will
2269 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2270 // the enclosing expression.
2271 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002272 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002273 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002274
Steve Naroff37387c92007-09-17 20:25:27 +00002275 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002276 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002277
Douglas Gregord3c68542009-11-19 01:08:35 +00002278 // Code completion after each argument.
2279 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002280 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002281 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002282 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002283 KeyIdents.size(),
2284 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002285 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002286 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002287 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002288 KeyIdents.size(),
2289 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002290 else
John McCall9ae2f072010-08-23 23:25:46 +00002291 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002292 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002293 KeyIdents.size(),
2294 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002295 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002296 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002297 }
2298
Steve Naroff37387c92007-09-17 20:25:27 +00002299 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002300 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002301 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002302 break;
2303 // We have a selector or a colon, continue parsing.
2304 }
2305 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002306 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002307 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002308 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002309 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002310 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002311 // We must manually skip to a ']', otherwise the expression skipper will
2312 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2313 // the enclosing expression.
2314 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002315 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002316 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002317
Steve Naroff49f109c2007-11-15 13:05:42 +00002318 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002319 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002320 }
2321 } else if (!selIdent) {
2322 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002323
Chris Lattner4fef81d2008-08-05 06:19:09 +00002324 // We must manually skip to a ']', otherwise the expression skipper will
2325 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2326 // the enclosing expression.
2327 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002328 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002329 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002330
Chris Lattnerdf195262007-10-09 17:51:17 +00002331 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002332 if (Tok.is(tok::identifier))
2333 Diag(Tok, diag::err_expected_colon);
2334 else
2335 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002336 // We must manually skip to a ']', otherwise the expression skipper will
2337 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2338 // the enclosing expression.
2339 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002340 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002341 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002342
Chris Lattner699b6612008-01-25 18:59:06 +00002343 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002344
Steve Naroff29238a02007-10-05 18:42:47 +00002345 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002346 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002347 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002348 KeyLocs.push_back(Loc);
2349 }
Chris Lattnerff384912007-10-07 02:00:24 +00002350 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002351
Douglas Gregor2725ca82010-04-21 19:57:20 +00002352 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002353 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002354 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002355 MultiExprArg(Actions,
2356 KeyExprs.take(),
2357 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002358 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002359 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002360 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002361 MultiExprArg(Actions,
2362 KeyExprs.take(),
2363 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002364 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002365 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002366 MultiExprArg(Actions,
2367 KeyExprs.take(),
2368 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002369}
2370
John McCall60d7b3a2010-08-24 06:29:42 +00002371ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2372 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002373 if (Res.isInvalid()) return move(Res);
2374
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002375 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2376 // expressions. At this point, we know that the only valid thing that starts
2377 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002378 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002379 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002380 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002381 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002382
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002383 while (Tok.is(tok::at)) {
2384 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002385
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002386 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002387 if (!isTokenStringLiteral())
2388 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002389
John McCall60d7b3a2010-08-24 06:29:42 +00002390 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002391 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002392 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002393
Sebastian Redleffa8d12008-12-10 00:02:53 +00002394 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002395 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002396
2397 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2398 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002399}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002400
2401/// objc-encode-expression:
2402/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002403ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002404Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002405 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002406
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002407 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002408
Chris Lattner4fef81d2008-08-05 06:19:09 +00002409 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002410 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2411
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002412 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002413
Douglas Gregor809070a2009-02-18 17:45:20 +00002414 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002415
Anders Carlsson4988ae32007-08-23 15:31:37 +00002416 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002417
Douglas Gregor809070a2009-02-18 17:45:20 +00002418 if (Ty.isInvalid())
2419 return ExprError();
2420
Mike Stump1eb44332009-09-09 15:08:12 +00002421 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002422 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002423}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002424
2425/// objc-protocol-expression
2426/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002427ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002428Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002429 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002430
Chris Lattner4fef81d2008-08-05 06:19:09 +00002431 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002432 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2433
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002434 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002435
Chris Lattner4fef81d2008-08-05 06:19:09 +00002436 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002437 return ExprError(Diag(Tok, diag::err_expected_ident));
2438
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002439 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002440 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002441
Anders Carlsson4988ae32007-08-23 15:31:37 +00002442 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002443
Sebastian Redl1d922962008-12-13 15:32:12 +00002444 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2445 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002446}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002447
2448/// objc-selector-expression
2449/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002450ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002451 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002452
Chris Lattner4fef81d2008-08-05 06:19:09 +00002453 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002454 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2455
Chris Lattner5f9e2722011-07-23 10:55:15 +00002456 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002457 SourceLocation LParenLoc = ConsumeParen();
2458 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002459
2460 if (Tok.is(tok::code_completion)) {
2461 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2462 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002463 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002464 return ExprError();
2465 }
2466
Chris Lattner2fc5c242009-04-11 18:13:45 +00002467 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002468 if (!SelIdent && // missing selector name.
2469 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002470 return ExprError(Diag(Tok, diag::err_expected_ident));
2471
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002472 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002473 unsigned nColons = 0;
2474 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002475 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002476 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2477 ++nColons;
2478 KeyIdents.push_back(0);
2479 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002480 return ExprError(Diag(Tok, diag::err_expected_colon));
2481
Chris Lattner5add7542010-08-27 22:32:41 +00002482 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002483 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002484 if (Tok.is(tok::r_paren))
2485 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002486
2487 if (Tok.is(tok::code_completion)) {
2488 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2489 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002490 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002491 return ExprError();
2492 }
2493
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002494 // Check for another keyword selector.
2495 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002496 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002497 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002498 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002499 break;
2500 }
Steve Naroff887407e2007-12-05 22:21:29 +00002501 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002502 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002503 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002504 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2505 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002506 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002507
2508Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2509
2510 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2511 // Append the current token at the end of the new token stream so that it
2512 // doesn't get lost.
2513 LM.Toks.push_back(Tok);
2514 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2515
2516 // MDecl might be null due to error in method prototype, etc.
2517 Decl *MDecl = LM.D;
2518 // Consume the previously pushed token.
2519 ConsumeAnyToken();
2520
2521 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2522 SourceLocation BraceLoc = Tok.getLocation();
2523 // Enter a scope for the method body.
2524 ParseScope BodyScope(this,
2525 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2526
2527 // Tell the actions module that we have entered a method definition with the
2528 // specified Declarator for the method.
2529 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2530
2531 if (PP.isCodeCompletionEnabled()) {
2532 if (trySkippingFunctionBodyForCodeCompletion()) {
2533 BodyScope.Exit();
2534 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2535 }
2536 }
2537
2538 StmtResult FnBody(ParseCompoundStatementBody());
2539
2540 // If the function body could not be parsed, make a bogus compoundstmt.
2541 if (FnBody.isInvalid())
2542 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2543 MultiStmtArg(Actions), false);
2544
2545 // Leave the function body scope.
2546 BodyScope.Exit();
2547
2548 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2549}