blob: 78c6b87715f95e416c6fc1e4daea8e85c1815df5 [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
Steve Naroff294494e2007-08-22 16:35:03 +0000340 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000341 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000342 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000343 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000344 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000345 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000346 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
347 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000348 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
349 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000350 continue;
351 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000352 if (Tok.is(tok::l_paren)) {
353 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000354 ParseObjCMethodDecl(Tok.getLocation(),
355 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000356 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000357 continue;
358 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000359 // Ignore excess semicolons.
360 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000361 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000362 continue;
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattnerbc662af2008-10-20 06:10:06 +0000365 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000366 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000367 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000369 // Code completion within an Objective-C interface.
370 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000371 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000372 ObjCImpDecl? Sema::PCC_ObjCImplementation
373 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000374 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000375 }
376
Chris Lattnere82a10f2008-10-20 05:46:22 +0000377 // If we don't have an @ directive, parse it as a function definition.
378 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000379 // The code below does not consume '}'s because it is afraid of eating the
380 // end of a namespace. Because of the way this code is structured, an
381 // erroneous r_brace would cause an infinite loop if not handled here.
382 if (Tok.is(tok::r_brace))
383 break;
John McCall0b7e6782011-03-24 11:26:52 +0000384 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000385 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000386 continue;
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattnere82a10f2008-10-20 05:46:22 +0000389 // Otherwise, we have an @ directive, eat the @.
390 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000391 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000392 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000394 break;
395 }
396
Chris Lattnera2449b22008-10-20 05:57:40 +0000397 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnera2449b22008-10-20 05:57:40 +0000399 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000400 AtEnd.setBegin(AtLoc);
401 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000402 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000403 } else if (DirectiveKind == tok::objc_not_keyword) {
404 Diag(Tok, diag::err_objc_unknown_at);
405 SkipUntil(tok::semi);
406 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 // Eat the identifier.
410 ConsumeToken();
411
Chris Lattnera2449b22008-10-20 05:57:40 +0000412 switch (DirectiveKind) {
413 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000414 // FIXME: If someone forgets an @end on a protocol, this loop will
415 // continue to eat up tons of stuff and spew lots of nonsense errors. It
416 // would probably be better to bail out if we saw an @class or @interface
417 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000418 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000419 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 SkipUntil(tok::r_brace, tok::at);
421 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000422
423 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000424 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000425 Diag(Tok, diag::err_objc_missing_end);
426 ConsumeToken();
427 break;
428
Chris Lattnera2449b22008-10-20 05:57:40 +0000429 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000430 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000431 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000432 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000433 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000434 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000435 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000436 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000437 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnera2449b22008-10-20 05:57:40 +0000439 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000440 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000441 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000442
Chris Lattnere82a10f2008-10-20 05:46:22 +0000443 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000444 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000445 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000446 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000448 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000449 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000450
Chris Lattnere82a10f2008-10-20 05:46:22 +0000451 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000452 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000453 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
John McCall7da19ea2011-03-26 01:53:26 +0000455 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000456 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000457 }
Steve Naroff294494e2007-08-22 16:35:03 +0000458 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000459
460 // We break out of the big loop in two cases: when we see @end or when we see
461 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000462 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000463 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000464 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000465 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000466 ConsumeToken(); // the "end" identifier
467 else
468 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattnera2449b22008-10-20 05:57:40 +0000470 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000471 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000472 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000473 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000474 allProperties.data(), allProperties.size(),
475 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000476}
477
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000478/// Parse property attribute declarations.
479///
480/// property-attr-decl: '(' property-attrlist ')'
481/// property-attrlist:
482/// property-attribute
483/// property-attrlist ',' property-attribute
484/// property-attribute:
485/// getter '=' identifier
486/// setter '=' identifier ':'
487/// readonly
488/// readwrite
489/// assign
490/// retain
491/// copy
492/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000493/// atomic
494/// strong
495/// weak
496/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000497///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000498void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000499 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000500 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000502 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000503 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000504 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000505 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000506 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000507 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000509 // If this is not an identifier at all, bail out early.
510 if (II == 0) {
511 MatchRHSPunctuation(tok::r_paren, LHSLoc);
512 return;
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner156b0612008-10-20 07:37:22 +0000515 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner92e62b02008-11-20 04:42:34 +0000517 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000518 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000519 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000521 else if (II->isStr("unsafe_unretained"))
522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000523 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000525 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000527 else if (II->isStr("strong"))
528 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000529 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000530 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000531 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000532 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000533 else if (II->isStr("atomic"))
534 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000535 else if (II->isStr("weak"))
536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000537 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000538 bool IsSetter = II->getNameStart()[0] == 's';
539
Chris Lattnere00da7c2008-10-20 07:39:53 +0000540 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000541 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
542 diag::err_objc_expected_equal_for_getter;
543
544 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000545 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor4ad96852009-11-19 07:41:15 +0000547 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000548 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000549 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000550 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000551 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000552 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000553 }
554
Anders Carlsson42499be2010-10-02 17:45:21 +0000555
556 SourceLocation SelLoc;
557 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
558
559 if (!SelIdent) {
560 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
561 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000562 SkipUntil(tok::r_paren);
563 return;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Anders Carlsson42499be2010-10-02 17:45:21 +0000566 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000568 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000570 if (ExpectAndConsume(tok::colon,
571 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000572 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000573 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000574 } else {
575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000576 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000577 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000578 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000579 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000580 SkipUntil(tok::r_paren);
581 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Chris Lattner156b0612008-10-20 07:37:22 +0000584 if (Tok.isNot(tok::comma))
585 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner156b0612008-10-20 07:37:22 +0000587 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattner156b0612008-10-20 07:37:22 +0000590 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000591}
592
Steve Naroff3536b442007-09-06 21:24:23 +0000593/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000594/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000595/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000596///
597/// objc-instance-method: '-'
598/// objc-class-method: '+'
599///
Steve Naroff4985ace2007-08-22 18:35:33 +0000600/// objc-method-attributes: [OBJC2]
601/// __attribute__((deprecated))
602///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000603Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000604 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000605 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000606
Mike Stump1eb44332009-09-09 15:08:12 +0000607 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000608 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000609 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000610 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000611 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000612 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000613 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000614}
615
616/// objc-selector:
617/// identifier
618/// one of
619/// enum struct union if else while do for switch case default
620/// break continue return goto asm sizeof typeof __alignof
621/// unsigned long const short volatile signed restrict _Complex
622/// in out inout bycopy byref oneway int char float double void _Bool
623///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000624IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000625
Chris Lattnerff384912007-10-07 02:00:24 +0000626 switch (Tok.getKind()) {
627 default:
628 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000629 case tok::ampamp:
630 case tok::ampequal:
631 case tok::amp:
632 case tok::pipe:
633 case tok::tilde:
634 case tok::exclaim:
635 case tok::exclaimequal:
636 case tok::pipepipe:
637 case tok::pipeequal:
638 case tok::caret:
639 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000640 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000641 if (isalpha(ThisTok[0])) {
642 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
643 Tok.setKind(tok::identifier);
644 SelectorLoc = ConsumeToken();
645 return II;
646 }
647 return 0;
648 }
649
Chris Lattnerff384912007-10-07 02:00:24 +0000650 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000651 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000652 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000653 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000654 case tok::kw_break:
655 case tok::kw_case:
656 case tok::kw_catch:
657 case tok::kw_char:
658 case tok::kw_class:
659 case tok::kw_const:
660 case tok::kw_const_cast:
661 case tok::kw_continue:
662 case tok::kw_default:
663 case tok::kw_delete:
664 case tok::kw_do:
665 case tok::kw_double:
666 case tok::kw_dynamic_cast:
667 case tok::kw_else:
668 case tok::kw_enum:
669 case tok::kw_explicit:
670 case tok::kw_export:
671 case tok::kw_extern:
672 case tok::kw_false:
673 case tok::kw_float:
674 case tok::kw_for:
675 case tok::kw_friend:
676 case tok::kw_goto:
677 case tok::kw_if:
678 case tok::kw_inline:
679 case tok::kw_int:
680 case tok::kw_long:
681 case tok::kw_mutable:
682 case tok::kw_namespace:
683 case tok::kw_new:
684 case tok::kw_operator:
685 case tok::kw_private:
686 case tok::kw_protected:
687 case tok::kw_public:
688 case tok::kw_register:
689 case tok::kw_reinterpret_cast:
690 case tok::kw_restrict:
691 case tok::kw_return:
692 case tok::kw_short:
693 case tok::kw_signed:
694 case tok::kw_sizeof:
695 case tok::kw_static:
696 case tok::kw_static_cast:
697 case tok::kw_struct:
698 case tok::kw_switch:
699 case tok::kw_template:
700 case tok::kw_this:
701 case tok::kw_throw:
702 case tok::kw_true:
703 case tok::kw_try:
704 case tok::kw_typedef:
705 case tok::kw_typeid:
706 case tok::kw_typename:
707 case tok::kw_typeof:
708 case tok::kw_union:
709 case tok::kw_unsigned:
710 case tok::kw_using:
711 case tok::kw_virtual:
712 case tok::kw_void:
713 case tok::kw_volatile:
714 case tok::kw_wchar_t:
715 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000716 case tok::kw__Bool:
717 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000718 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000719 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000720 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000721 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000722 }
Steve Naroff294494e2007-08-22 16:35:03 +0000723}
724
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000725/// objc-for-collection-in: 'in'
726///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000727bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000728 // FIXME: May have to do additional look-ahead to only allow for
729 // valid tokens following an 'in'; such as an identifier, unary operators,
730 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000731 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000732 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000733}
734
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000735/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000736/// qualifier list and builds their bitmask representation in the input
737/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000738///
739/// objc-type-qualifiers:
740/// objc-type-qualifier
741/// objc-type-qualifiers objc-type-qualifier
742///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000743void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000744 Declarator::TheContext Context) {
745 assert(Context == Declarator::ObjCParameterContext ||
746 Context == Declarator::ObjCResultContext);
747
Chris Lattnere8b724d2007-12-12 06:56:32 +0000748 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000749 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000750 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000751 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000752 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000753 }
754
Chris Lattnercb53b362007-12-27 19:57:00 +0000755 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000756 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattnere8b724d2007-12-12 06:56:32 +0000758 const IdentifierInfo *II = Tok.getIdentifierInfo();
759 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000761 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000764 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000765 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000766 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
767 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
768 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
769 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
770 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
771 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000772 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000773 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000774 ConsumeToken();
775 II = 0;
776 break;
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnere8b724d2007-12-12 06:56:32 +0000779 // If this wasn't a recognized qualifier, bail out.
780 if (II) return;
781 }
782}
783
John McCallcdda47f2011-10-01 09:56:14 +0000784/// Take all the decl attributes out of the given list and add
785/// them to the given attribute set.
786static void takeDeclAttributes(ParsedAttributes &attrs,
787 AttributeList *list) {
788 while (list) {
789 AttributeList *cur = list;
790 list = cur->getNext();
791
792 if (!cur->isUsedAsTypeAttr()) {
793 // Clear out the next pointer. We're really completely
794 // destroying the internal invariants of the declarator here,
795 // but it doesn't matter because we're done with it.
796 cur->setNext(0);
797 attrs.add(cur);
798 }
799 }
800}
801
802/// takeDeclAttributes - Take all the decl attributes from the given
803/// declarator and add them to the given list.
804static void takeDeclAttributes(ParsedAttributes &attrs,
805 Declarator &D) {
806 // First, take ownership of all attributes.
807 attrs.getPool().takeAllFrom(D.getAttributePool());
808 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
809
810 // Now actually move the attributes over.
811 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
812 takeDeclAttributes(attrs, D.getAttributes());
813 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
814 takeDeclAttributes(attrs,
815 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
816}
817
Chris Lattnere8b724d2007-12-12 06:56:32 +0000818/// objc-type-name:
819/// '(' objc-type-qualifiers[opt] type-name ')'
820/// '(' objc-type-qualifiers[opt] ')'
821///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000822ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000823 Declarator::TheContext context,
824 ParsedAttributes *paramAttrs) {
825 assert(context == Declarator::ObjCParameterContext ||
826 context == Declarator::ObjCResultContext);
827 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
828
Chris Lattnerdf195262007-10-09 17:51:17 +0000829 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattner4a76b292008-10-22 03:52:06 +0000831 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000832 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000833 ObjCDeclContextSwitch ObjCDC(*this);
834
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000835 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000836 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000837
John McCallb3d87482010-08-24 05:47:05 +0000838 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000839 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000840 // Parse an abstract declarator.
841 DeclSpec declSpec(AttrFactory);
842 declSpec.setObjCQualifiers(&DS);
843 ParseSpecifierQualifierList(declSpec);
844 Declarator declarator(declSpec, context);
845 ParseDeclarator(declarator);
846
847 // If that's not invalid, extract a type.
848 if (!declarator.isInvalidType()) {
849 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
850 if (!type.isInvalid())
851 Ty = type.get();
852
853 // If we're parsing a parameter, steal all the decl attributes
854 // and add them to the decl spec.
855 if (context == Declarator::ObjCParameterContext)
856 takeDeclAttributes(*paramAttrs, declarator);
857 }
858 } else if (context == Declarator::ObjCResultContext &&
859 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000860 if (!Ident_instancetype)
861 Ident_instancetype = PP.getIdentifierInfo("instancetype");
862
863 if (Tok.getIdentifierInfo() == Ident_instancetype) {
864 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
865 ConsumeToken();
866 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000867 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000868
Steve Naroffd7333c22008-10-21 14:15:04 +0000869 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000870 ConsumeParen();
871 else if (Tok.getLocation() == TypeStartLoc) {
872 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000873 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000874 SkipUntil(tok::r_paren);
875 } else {
876 // Otherwise, we found *something*, but didn't get a ')' in the right
877 // place. Emit an error then return what we have as the type.
878 MatchRHSPunctuation(tok::r_paren, LParenLoc);
879 }
Steve Narofff28b2642007-09-05 23:30:30 +0000880 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000881}
882
883/// objc-method-decl:
884/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000885/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000886/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000887/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000888///
889/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000890/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000891/// objc-keyword-selector objc-keyword-decl
892///
893/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000894/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
895/// objc-selector ':' objc-keyword-attributes[opt] identifier
896/// ':' objc-type-name objc-keyword-attributes[opt] identifier
897/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000898///
Steve Naroff4985ace2007-08-22 18:35:33 +0000899/// objc-parmlist:
900/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000901///
Steve Naroff4985ace2007-08-22 18:35:33 +0000902/// objc-parms:
903/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000904///
Steve Naroff4985ace2007-08-22 18:35:33 +0000905/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000906/// , ...
907///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000908/// objc-keyword-attributes: [OBJC2]
909/// __attribute__((unused))
910///
John McCalld226f652010-08-21 09:40:31 +0000911Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000912 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000913 tok::ObjCKeywordKind MethodImplKind,
914 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000915 ParsingDeclRAIIObject PD(*this);
916
Douglas Gregore8f5a172010-04-07 00:21:17 +0000917 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000918 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000919 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000920 cutOffParsing();
921 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000922 }
923
Chris Lattnere8904e92008-08-23 01:48:03 +0000924 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000925 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000927 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000928 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek9e049352010-02-18 23:05:16 +0000930 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000931 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000932 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000933 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000934
Douglas Gregore8f5a172010-04-07 00:21:17 +0000935 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000936 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000937 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000938 cutOffParsing();
939 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000940 }
941
Ted Kremenek9e049352010-02-18 23:05:16 +0000942 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000943 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000944 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000945
Steve Naroff84c43102009-02-11 20:43:13 +0000946 // An unnamed colon is valid.
947 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000948 Diag(Tok, diag::err_expected_selector_for_method)
949 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000950 // Skip until we get a ; or {}.
951 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000952 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000953 }
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Chris Lattner5f9e2722011-07-23 10:55:15 +0000955 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000956 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000957 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000958 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000959 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Chris Lattnerff384912007-10-07 02:00:24 +0000961 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000962 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000963 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000964 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000965 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000966 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000967 methodAttrs.getList(), MethodImplKind,
968 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000969 PD.complete(Result);
970 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000971 }
Steve Narofff28b2642007-09-05 23:30:30 +0000972
Chris Lattner5f9e2722011-07-23 10:55:15 +0000973 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +0000974 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000975 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000976 ParseScope PrototypeScope(this,
977 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000978
979 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000980
Chris Lattnerff384912007-10-07 02:00:24 +0000981 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000982 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000983 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattnerff384912007-10-07 02:00:24 +0000985 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000986 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000987 Diag(Tok, diag::err_expected_colon);
988 break;
989 }
990 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000991
John McCallb3d87482010-08-24 05:47:05 +0000992 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000993 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +0000994 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
995 Declarator::ObjCParameterContext,
996 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000997
Chris Lattnerff384912007-10-07 02:00:24 +0000998 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +0000999 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001000 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001001 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001002 MaybeParseGNUAttributes(paramAttrs);
1003 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001004 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001005
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001006 // Code completion for the next piece of the selector.
1007 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001008 KeyIdents.push_back(SelIdent);
1009 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1010 mType == tok::minus,
1011 /*AtParameterName=*/true,
1012 ReturnType,
1013 KeyIdents.data(),
1014 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001015 cutOffParsing();
1016 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001017 }
1018
Chris Lattnerdf195262007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001020 Diag(Tok, diag::err_expected_ident); // missing argument name.
1021 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Chris Lattnere294d3f2009-04-11 18:57:04 +00001024 ArgInfo.Name = Tok.getIdentifierInfo();
1025 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001026 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattnere294d3f2009-04-11 18:57:04 +00001028 ArgInfos.push_back(ArgInfo);
1029 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001030 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001031
John McCall0b7e6782011-03-24 11:26:52 +00001032 // Make sure the attributes persist.
1033 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1034
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001035 // Code completion for the next piece of the selector.
1036 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001037 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1038 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001039 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001040 ReturnType,
1041 KeyIdents.data(),
1042 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001043 cutOffParsing();
1044 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001045 }
1046
Chris Lattnerff384912007-10-07 02:00:24 +00001047 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001048 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001049 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001050 break;
1051 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Steve Naroff335eafa2007-11-15 12:35:21 +00001054 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattnerff384912007-10-07 02:00:24 +00001056 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001058 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001059 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001060 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001061 ConsumeToken();
1062 break;
1063 }
John McCall0b7e6782011-03-24 11:26:52 +00001064 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001065 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001066 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001067 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1068 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001069 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001070 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001071 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1072 ParmDecl.getIdentifierLoc(),
1073 Param,
1074 0));
1075
Chris Lattnerff384912007-10-07 02:00:24 +00001076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001078 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001079 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001080 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001081 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001082
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001083 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001084 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001085
Chris Lattnerff384912007-10-07 02:00:24 +00001086 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1087 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001088 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001089 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001090 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001091 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001092 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001093 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001094 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001095
John McCall54abf7d2009-11-04 02:18:39 +00001096 PD.complete(Result);
1097 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001098}
1099
Steve Naroffdac269b2007-08-20 21:31:48 +00001100/// objc-protocol-refs:
1101/// '<' identifier-list '>'
1102///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001103bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001104ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1105 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001106 bool WarnOnDeclarations,
1107 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001108 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001110 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner5f9e2722011-07-23 10:55:15 +00001112 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Chris Lattnere13b9592008-07-26 04:03:38 +00001114 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001115 if (Tok.is(tok::code_completion)) {
1116 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1117 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001118 cutOffParsing();
1119 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001120 }
1121
Chris Lattnere13b9592008-07-26 04:03:38 +00001122 if (Tok.isNot(tok::identifier)) {
1123 Diag(Tok, diag::err_expected_ident);
1124 SkipUntil(tok::greater);
1125 return true;
1126 }
1127 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1128 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001129 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001130 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattnere13b9592008-07-26 04:03:38 +00001132 if (Tok.isNot(tok::comma))
1133 break;
1134 ConsumeToken();
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Chris Lattnere13b9592008-07-26 04:03:38 +00001137 // Consume the '>'.
1138 if (Tok.isNot(tok::greater)) {
1139 Diag(Tok, diag::err_expected_greater);
1140 return true;
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattnere13b9592008-07-26 04:03:38 +00001143 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattnere13b9592008-07-26 04:03:38 +00001145 // Convert the list of protocols identifiers into a list of protocol decls.
1146 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1147 &ProtocolIdents[0], ProtocolIdents.size(),
1148 Protocols);
1149 return false;
1150}
1151
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001152/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1153/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001154bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001155 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1156 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1157 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158 SmallVector<Decl *, 8> ProtocolDecl;
1159 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001160 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1161 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001162 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1163 ProtocolLocs.data(), LAngleLoc);
1164 if (EndProtoLoc.isValid())
1165 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001166 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001167}
1168
1169
Steve Naroffdac269b2007-08-20 21:31:48 +00001170/// objc-class-instance-variables:
1171/// '{' objc-instance-variable-decl-list[opt] '}'
1172///
1173/// objc-instance-variable-decl-list:
1174/// objc-visibility-spec
1175/// objc-instance-variable-decl ';'
1176/// ';'
1177/// objc-instance-variable-decl-list objc-visibility-spec
1178/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1179/// objc-instance-variable-decl-list ';'
1180///
1181/// objc-visibility-spec:
1182/// @private
1183/// @protected
1184/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001185/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001186///
1187/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001188/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001189///
John McCalld226f652010-08-21 09:40:31 +00001190void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001191 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001192 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001193 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001194 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001195
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001196 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001197 ObjCDeclContextSwitch ObjCDC(*this);
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
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001445 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001446 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001447 }
1448 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001449 SourceLocation superClassLoc;
1450 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001451 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001452 // We have a super class
1453 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001454 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001455 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001456 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001457 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001458 superClassId = Tok.getIdentifierInfo();
1459 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 }
John McCalld226f652010-08-21 09:40:31 +00001461 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001462 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001463 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Steve Naroff60fccee2007-10-29 21:38:07 +00001465 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001466 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1467
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001468 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001469 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001470 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001471}
Steve Naroff60fccee2007-10-29 21:38:07 +00001472
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001473Parser::DeclGroupPtrTy
1474Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001475 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1476 "ParseObjCAtEndDeclaration(): Expected @end");
1477 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001478 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001479 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001480 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1481 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1482 DeclsInGroup.push_back(D);
1483 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001484 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001485
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001486 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001487 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001488 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001489 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001490 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001491 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001492 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001493
1494 LateParsedObjCMethods.clear();
1495 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001496 return Actions.BuildDeclaratorGroup(
1497 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001498}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001499
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001500Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1501 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001502 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001503 return Actions.ConvertDeclToDeclGroup(0);
1504 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001505 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001506 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1507}
1508
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001509/// compatibility-alias-decl:
1510/// @compatibility_alias alias-name class-name ';'
1511///
John McCalld226f652010-08-21 09:40:31 +00001512Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001513 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1514 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1515 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001516 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001517 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001518 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001519 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001520 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1521 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001522 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001523 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001524 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001525 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001526 IdentifierInfo *classId = Tok.getIdentifierInfo();
1527 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001528 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1529 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001530 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1531 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001532}
1533
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001534/// property-synthesis:
1535/// @synthesize property-ivar-list ';'
1536///
1537/// property-ivar-list:
1538/// property-ivar
1539/// property-ivar-list ',' property-ivar
1540///
1541/// property-ivar:
1542/// identifier
1543/// identifier '=' identifier
1544///
John McCalld226f652010-08-21 09:40:31 +00001545Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001546 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1547 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001548 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregorb328c422009-11-18 19:45:45 +00001550 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001551 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001552 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001553 cutOffParsing();
1554 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001555 }
1556
Douglas Gregorb328c422009-11-18 19:45:45 +00001557 if (Tok.isNot(tok::identifier)) {
1558 Diag(Tok, diag::err_synthesized_property_name);
1559 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001560 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001561 }
1562
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001563 IdentifierInfo *propertyIvar = 0;
1564 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1565 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001566 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001567 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001568 // property '=' ivar-name
1569 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001570
1571 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001572 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001573 cutOffParsing();
1574 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001575 }
1576
Chris Lattnerdf195262007-10-09 17:51:17 +00001577 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001578 Diag(Tok, diag::err_expected_ident);
1579 break;
1580 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001581 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001582 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001583 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001584 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001585 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001586 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001587 break;
1588 ConsumeToken(); // consume ','
1589 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001590 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001591 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001592}
1593
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001594/// property-dynamic:
1595/// @dynamic property-list
1596///
1597/// property-list:
1598/// identifier
1599/// property-list ',' identifier
1600///
John McCalld226f652010-08-21 09:40:31 +00001601Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001602 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1603 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001604 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001605 while (true) {
1606 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001607 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001608 cutOffParsing();
1609 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001610 }
1611
1612 if (Tok.isNot(tok::identifier)) {
1613 Diag(Tok, diag::err_expected_ident);
1614 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001615 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001616 }
1617
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001618 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1619 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001620 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001621 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001622
Chris Lattnerdf195262007-10-09 17:51:17 +00001623 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001624 break;
1625 ConsumeToken(); // consume ','
1626 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001627 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001628 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001629}
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001631/// objc-throw-statement:
1632/// throw expression[opt];
1633///
John McCall60d7b3a2010-08-24 06:29:42 +00001634StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1635 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001636 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001637 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001638 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001639 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001640 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001641 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001642 }
1643 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001644 // consume ';'
1645 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001646 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001647}
1648
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001649/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001650/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001651///
John McCall60d7b3a2010-08-24 06:29:42 +00001652StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001653Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001654 ConsumeToken(); // consume synchronized
1655 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001656 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001657 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001658 }
John McCall07524032011-07-27 21:50:02 +00001659
1660 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001661 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001662 ExprResult operand(ParseExpression());
1663
1664 if (Tok.is(tok::r_paren)) {
1665 ConsumeParen(); // ')'
1666 } else {
1667 if (!operand.isInvalid())
1668 Diag(Tok, diag::err_expected_rparen);
1669
1670 // Skip forward until we see a left brace, but don't consume it.
1671 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001672 }
John McCall07524032011-07-27 21:50:02 +00001673
1674 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001675 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001676 if (!operand.isInvalid())
1677 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001678 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001679 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001680
John McCall07524032011-07-27 21:50:02 +00001681 // Check the @synchronized operand now.
1682 if (!operand.isInvalid())
1683 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001684
John McCall07524032011-07-27 21:50:02 +00001685 // Parse the compound statement within a new scope.
1686 ParseScope bodyScope(this, Scope::DeclScope);
1687 StmtResult body(ParseCompoundStatementBody());
1688 bodyScope.Exit();
1689
1690 // If there was a semantic or parse error earlier with the
1691 // operand, fail now.
1692 if (operand.isInvalid())
1693 return StmtError();
1694
1695 if (body.isInvalid())
1696 body = Actions.ActOnNullStmt(Tok.getLocation());
1697
1698 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001699}
1700
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001701/// objc-try-catch-statement:
1702/// @try compound-statement objc-catch-list[opt]
1703/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1704///
1705/// objc-catch-list:
1706/// @catch ( parameter-declaration ) compound-statement
1707/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1708/// catch-parameter-declaration:
1709/// parameter-declaration
1710/// '...' [OBJC2]
1711///
John McCall60d7b3a2010-08-24 06:29:42 +00001712StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001713 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001714
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001715 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001716 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001717 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001718 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001719 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001720 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001721 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001722 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001723 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001724 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001725 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001726 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001727
Chris Lattnerdf195262007-10-09 17:51:17 +00001728 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001729 // At this point, we need to lookahead to determine if this @ is the start
1730 // of an @catch or @finally. We don't want to consume the @ token if this
1731 // is an @try or @encode or something else.
1732 Token AfterAt = GetLookAheadToken(1);
1733 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1734 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1735 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001736
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001737 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001738 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001739 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001740 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001741 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001742 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001743 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001744 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001745 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001746 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001747 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001748 ParseDeclarator(ParmDecl);
1749
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001750 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001751 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001752 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001753 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001754 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Steve Naroff93a25952009-04-07 22:56:58 +00001756 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Steve Naroff93a25952009-04-07 22:56:58 +00001758 if (Tok.is(tok::r_paren))
1759 RParenLoc = ConsumeParen();
1760 else // Skip over garbage, until we get to ')'. Eat the ')'.
1761 SkipUntil(tok::r_paren, true, false);
1762
John McCall60d7b3a2010-08-24 06:29:42 +00001763 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001764 if (Tok.is(tok::l_brace))
1765 CatchBody = ParseCompoundStatementBody();
1766 else
1767 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001768 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001769 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001770
John McCall60d7b3a2010-08-24 06:29:42 +00001771 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001772 RParenLoc,
1773 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001774 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001775 if (!Catch.isInvalid())
1776 CatchStmts.push_back(Catch.release());
1777
Steve Naroff64515f32008-02-05 21:27:35 +00001778 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001779 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1780 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001781 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001782 }
1783 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001784 } else {
1785 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001786 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001787 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001788
John McCall60d7b3a2010-08-24 06:29:42 +00001789 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001790 if (Tok.is(tok::l_brace))
1791 FinallyBody = ParseCompoundStatementBody();
1792 else
1793 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001794 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001795 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001796 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001797 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001798 catch_or_finally_seen = true;
1799 break;
1800 }
1801 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001802 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001803 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001804 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001805 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001806
John McCall9ae2f072010-08-23 23:25:46 +00001807 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001808 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001809 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001810}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001811
John McCallf85e1932011-06-15 23:02:42 +00001812/// objc-autoreleasepool-statement:
1813/// @autoreleasepool compound-statement
1814///
1815StmtResult
1816Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1817 ConsumeToken(); // consume autoreleasepool
1818 if (Tok.isNot(tok::l_brace)) {
1819 Diag(Tok, diag::err_expected_lbrace);
1820 return StmtError();
1821 }
1822 // Enter a scope to hold everything within the compound stmt. Compound
1823 // statements can always hold declarations.
1824 ParseScope BodyScope(this, Scope::DeclScope);
1825
1826 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1827
1828 BodyScope.Exit();
1829 if (AutoreleasePoolBody.isInvalid())
1830 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1831 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1832 AutoreleasePoolBody.take());
1833}
1834
Steve Naroff3536b442007-09-06 21:24:23 +00001835/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001836///
John McCalld226f652010-08-21 09:40:31 +00001837Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001838 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001839
John McCallf312b1e2010-08-26 23:41:50 +00001840 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1841 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001843 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001844 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001845 if (ObjCImpDecl) {
1846 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001847 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001848 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001849 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001850 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001851
Steve Naroff409be832007-11-11 19:54:21 +00001852 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001853 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001854 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Steve Naroff409be832007-11-11 19:54:21 +00001856 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1857 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Steve Naroff409be832007-11-11 19:54:21 +00001859 // If we didn't find the '{', bail out.
1860 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001861 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001862 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001863 // Allow the rest of sema to find private method decl implementations.
1864 if (MDecl)
1865 Actions.AddAnyMethodToGlobalPool(MDecl);
1866
1867 // Consume the tokens and store them for later parsing.
1868 LexedMethod* LM = new LexedMethod(this, MDecl);
1869 LateParsedObjCMethods.push_back(LM);
1870 CachedTokens &Toks = LM->Toks;
1871 // Begin by storing the '{' token.
1872 Toks.push_back(Tok);
1873 ConsumeBrace();
1874 // Consume everything up to (and including) the matching right brace.
1875 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001876 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001877}
Anders Carlsson55085182007-08-21 17:43:55 +00001878
John McCall60d7b3a2010-08-24 06:29:42 +00001879StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001880 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001881 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001882 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001883 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001884 }
1885
1886 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001887 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001888
1889 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001890 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001891
1892 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001893 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001894
1895 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1896 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001897
John McCall60d7b3a2010-08-24 06:29:42 +00001898 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001899 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001900 // If the expression is invalid, skip ahead to the next semicolon. Not
1901 // doing this opens us up to the possibility of infinite loops if
1902 // ParseExpression does not consume any tokens.
1903 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001904 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001905 }
Chris Lattner5d803162009-12-07 16:33:19 +00001906
Steve Naroff64515f32008-02-05 21:27:35 +00001907 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001908 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001909 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001910}
1911
John McCall60d7b3a2010-08-24 06:29:42 +00001912ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001913 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001914 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001915 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001916 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001917 return ExprError();
1918
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001919 case tok::string_literal: // primary-expression: string-literal
1920 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001921 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001922 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001923 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001924 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001925
Chris Lattner4fef81d2008-08-05 06:19:09 +00001926 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1927 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001928 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001929 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001930 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001931 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001932 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001933 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001934 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001935 }
Anders Carlsson55085182007-08-21 17:43:55 +00001936 }
Anders Carlsson55085182007-08-21 17:43:55 +00001937}
1938
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001939/// \brirg Parse the receiver of an Objective-C++ message send.
1940///
1941/// This routine parses the receiver of a message send in
1942/// Objective-C++ either as a type or as an expression. Note that this
1943/// routine must not be called to parse a send to 'super', since it
1944/// has no way to return such a result.
1945///
1946/// \param IsExpr Whether the receiver was parsed as an expression.
1947///
1948/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1949/// IsExpr is true), the parsed expression. If the receiver was parsed
1950/// as a type (\c IsExpr is false), the parsed type.
1951///
1952/// \returns True if an error occurred during parsing or semantic
1953/// analysis, in which case the arguments do not have valid
1954/// values. Otherwise, returns false for a successful parse.
1955///
1956/// objc-receiver: [C++]
1957/// 'super' [not parsed here]
1958/// expression
1959/// simple-type-specifier
1960/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001961bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001962 InMessageExpressionRAIIObject InMessage(*this, true);
1963
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001964 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1965 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1966 TryAnnotateTypeOrScopeToken();
1967
1968 if (!isCXXSimpleTypeSpecifier()) {
1969 // objc-receiver:
1970 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001971 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001972 if (Receiver.isInvalid())
1973 return true;
1974
1975 IsExpr = true;
1976 TypeOrExpr = Receiver.take();
1977 return false;
1978 }
1979
1980 // objc-receiver:
1981 // typename-specifier
1982 // simple-type-specifier
1983 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001984 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001985 ParseCXXSimpleTypeSpecifier(DS);
1986
1987 if (Tok.is(tok::l_paren)) {
1988 // If we see an opening parentheses at this point, we are
1989 // actually parsing an expression that starts with a
1990 // function-style cast, e.g.,
1991 //
1992 // postfix-expression:
1993 // simple-type-specifier ( expression-list [opt] )
1994 // typename-specifier ( expression-list [opt] )
1995 //
1996 // Parse the remainder of this case, then the (optional)
1997 // postfix-expression suffix, followed by the (optional)
1998 // right-hand side of the binary expression. We have an
1999 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002000 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002001 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002002 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002003 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002004 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002005 if (Receiver.isInvalid())
2006 return true;
2007
2008 IsExpr = true;
2009 TypeOrExpr = Receiver.take();
2010 return false;
2011 }
2012
2013 // We have a class message. Turn the simple-type-specifier or
2014 // typename-specifier we parsed into a type and parse the
2015 // remainder of the class message.
2016 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002017 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002018 if (Type.isInvalid())
2019 return true;
2020
2021 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002022 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002023 return false;
2024}
2025
Douglas Gregor1b730e82010-05-31 14:40:22 +00002026/// \brief Determine whether the parser is currently referring to a an
2027/// Objective-C message send, using a simplified heuristic to avoid overhead.
2028///
2029/// This routine will only return true for a subset of valid message-send
2030/// expressions.
2031bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002032 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002033 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002034 return GetLookAheadToken(1).is(tok::identifier) &&
2035 GetLookAheadToken(2).is(tok::identifier);
2036}
2037
Douglas Gregor9497a732010-09-16 01:51:54 +00002038bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2039 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2040 InMessageExpression)
2041 return false;
2042
2043
2044 ParsedType Type;
2045
2046 if (Tok.is(tok::annot_typename))
2047 Type = getTypeAnnotation(Tok);
2048 else if (Tok.is(tok::identifier))
2049 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2050 getCurScope());
2051 else
2052 return false;
2053
2054 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2055 const Token &AfterNext = GetLookAheadToken(2);
2056 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2057 if (Tok.is(tok::identifier))
2058 TryAnnotateTypeOrScopeToken();
2059
2060 return Tok.is(tok::annot_typename);
2061 }
2062 }
2063
2064 return false;
2065}
2066
Mike Stump1eb44332009-09-09 15:08:12 +00002067/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002068/// '[' objc-receiver objc-message-args ']'
2069///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002070/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002071/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002072/// expression
2073/// class-name
2074/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002075///
John McCall60d7b3a2010-08-24 06:29:42 +00002076ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002077 assert(Tok.is(tok::l_square) && "'[' expected");
2078 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2079
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002080 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002081 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002082 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002083 return ExprError();
2084 }
2085
Douglas Gregor0fbda682010-09-15 14:51:05 +00002086 InMessageExpressionRAIIObject InMessage(*this, true);
2087
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002088 if (getLang().CPlusPlus) {
2089 // We completely separate the C and C++ cases because C++ requires
2090 // more complicated (read: slower) parsing.
2091
2092 // Handle send to super.
2093 // FIXME: This doesn't benefit from the same typo-correction we
2094 // get in Objective-C.
2095 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002096 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002097 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2098 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002099
2100 // Parse the receiver, which is either a type or an expression.
2101 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002102 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002103 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2104 SkipUntil(tok::r_square);
2105 return ExprError();
2106 }
2107
2108 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002109 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2110 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002111 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002112
2113 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002114 ParsedType::getFromOpaquePtr(TypeOrExpr),
2115 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002116 }
2117
2118 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002119 IdentifierInfo *Name = Tok.getIdentifierInfo();
2120 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002121 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002122 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002123 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002124 NextToken().is(tok::period),
2125 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002126 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002127 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2128 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002129
John McCallf312b1e2010-08-26 23:41:50 +00002130 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002131 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002132 SkipUntil(tok::r_square);
2133 return ExprError();
2134 }
2135
Douglas Gregor1569f952010-04-21 20:38:13 +00002136 ConsumeToken(); // the type name
2137
2138 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002139 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002140
John McCallf312b1e2010-08-26 23:41:50 +00002141 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002142 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002143 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002144 }
Chris Lattner699b6612008-01-25 18:59:06 +00002145 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002146
2147 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002148 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002149 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002150 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002151 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002152 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002153
John McCallb3d87482010-08-24 05:47:05 +00002154 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2155 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002156}
Sebastian Redl1d922962008-12-13 15:32:12 +00002157
Douglas Gregor2725ca82010-04-21 19:57:20 +00002158/// \brief Parse the remainder of an Objective-C message following the
2159/// '[' objc-receiver.
2160///
2161/// This routine handles sends to super, class messages (sent to a
2162/// class name), and instance messages (sent to an object), and the
2163/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2164/// ReceiverExpr, respectively. Only one of these parameters may have
2165/// a valid value.
2166///
2167/// \param LBracLoc The location of the opening '['.
2168///
2169/// \param SuperLoc If this is a send to 'super', the location of the
2170/// 'super' keyword that indicates a send to the superclass.
2171///
2172/// \param ReceiverType If this is a class message, the type of the
2173/// class we are sending a message to.
2174///
2175/// \param ReceiverExpr If this is an instance message, the expression
2176/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002177///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002178/// objc-message-args:
2179/// objc-selector
2180/// objc-keywordarg-list
2181///
2182/// objc-keywordarg-list:
2183/// objc-keywordarg
2184/// objc-keywordarg-list objc-keywordarg
2185///
Mike Stump1eb44332009-09-09 15:08:12 +00002186/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002187/// selector-name[opt] ':' objc-keywordexpr
2188///
2189/// objc-keywordexpr:
2190/// nonempty-expr-list
2191///
2192/// nonempty-expr-list:
2193/// assignment-expression
2194/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002195///
John McCall60d7b3a2010-08-24 06:29:42 +00002196ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002197Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002198 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002199 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002200 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002201 InMessageExpressionRAIIObject InMessage(*this, true);
2202
Steve Naroffc4df6d22009-11-07 02:08:14 +00002203 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002204 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002205 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2206 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002207 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002208 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2209 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002210 else
John McCall9ae2f072010-08-23 23:25:46 +00002211 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002212 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002213 cutOffParsing();
2214 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002215 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002216
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002217 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002218 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002219 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002220
Chris Lattner5f9e2722011-07-23 10:55:15 +00002221 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002222 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002223 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002224
Chris Lattnerdf195262007-10-09 17:51:17 +00002225 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002226 while (1) {
2227 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002228 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002229 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002230
Chris Lattnerdf195262007-10-09 17:51:17 +00002231 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002232 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002233 // We must manually skip to a ']', otherwise the expression skipper will
2234 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2235 // the enclosing expression.
2236 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002237 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002238 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002239
Steve Naroff68d331a2007-09-27 14:38:14 +00002240 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002241 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002242
2243 if (Tok.is(tok::code_completion)) {
2244 if (SuperLoc.isValid())
2245 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2246 KeyIdents.data(),
2247 KeyIdents.size(),
2248 /*AtArgumentEpression=*/true);
2249 else if (ReceiverType)
2250 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2251 KeyIdents.data(),
2252 KeyIdents.size(),
2253 /*AtArgumentEpression=*/true);
2254 else
2255 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2256 KeyIdents.data(),
2257 KeyIdents.size(),
2258 /*AtArgumentEpression=*/true);
2259
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002260 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002261 return ExprError();
2262 }
2263
John McCall60d7b3a2010-08-24 06:29:42 +00002264 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002265 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002266 // We must manually skip to a ']', otherwise the expression skipper will
2267 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2268 // the enclosing expression.
2269 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002270 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002271 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002272
Steve Naroff37387c92007-09-17 20:25:27 +00002273 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002274 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002275
Douglas Gregord3c68542009-11-19 01:08:35 +00002276 // Code completion after each argument.
2277 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002278 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002279 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002280 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002281 KeyIdents.size(),
2282 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002283 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002284 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002285 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002286 KeyIdents.size(),
2287 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002288 else
John McCall9ae2f072010-08-23 23:25:46 +00002289 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002290 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002291 KeyIdents.size(),
2292 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002293 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002294 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002295 }
2296
Steve Naroff37387c92007-09-17 20:25:27 +00002297 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002298 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002299 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002300 break;
2301 // We have a selector or a colon, continue parsing.
2302 }
2303 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002304 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002305 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002306 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002307 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002308 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002309 // We must manually skip to a ']', otherwise the expression skipper will
2310 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2311 // the enclosing expression.
2312 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002313 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002314 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002315
Steve Naroff49f109c2007-11-15 13:05:42 +00002316 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002317 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002318 }
2319 } else if (!selIdent) {
2320 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002321
Chris Lattner4fef81d2008-08-05 06:19:09 +00002322 // We must manually skip to a ']', otherwise the expression skipper will
2323 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2324 // the enclosing expression.
2325 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002326 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002327 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002328
Chris Lattnerdf195262007-10-09 17:51:17 +00002329 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002330 if (Tok.is(tok::identifier))
2331 Diag(Tok, diag::err_expected_colon);
2332 else
2333 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002334 // We must manually skip to a ']', otherwise the expression skipper will
2335 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2336 // the enclosing expression.
2337 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002338 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002339 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002340
Chris Lattner699b6612008-01-25 18:59:06 +00002341 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002342
Steve Naroff29238a02007-10-05 18:42:47 +00002343 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002344 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002345 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002346 KeyLocs.push_back(Loc);
2347 }
Chris Lattnerff384912007-10-07 02:00:24 +00002348 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002349
Douglas Gregor2725ca82010-04-21 19:57:20 +00002350 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002351 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002352 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002353 MultiExprArg(Actions,
2354 KeyExprs.take(),
2355 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002356 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002357 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002358 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002359 MultiExprArg(Actions,
2360 KeyExprs.take(),
2361 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002362 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002363 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002364 MultiExprArg(Actions,
2365 KeyExprs.take(),
2366 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002367}
2368
John McCall60d7b3a2010-08-24 06:29:42 +00002369ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2370 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002371 if (Res.isInvalid()) return move(Res);
2372
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002373 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2374 // expressions. At this point, we know that the only valid thing that starts
2375 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002376 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002377 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002378 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002379 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002380
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002381 while (Tok.is(tok::at)) {
2382 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002383
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002384 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002385 if (!isTokenStringLiteral())
2386 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002387
John McCall60d7b3a2010-08-24 06:29:42 +00002388 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002389 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002390 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002391
Sebastian Redleffa8d12008-12-10 00:02:53 +00002392 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002393 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002394
2395 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2396 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002397}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002398
2399/// objc-encode-expression:
2400/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002401ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002402Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002403 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002404
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002405 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002406
Chris Lattner4fef81d2008-08-05 06:19:09 +00002407 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002408 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2409
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002410 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002411
Douglas Gregor809070a2009-02-18 17:45:20 +00002412 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002413
Anders Carlsson4988ae32007-08-23 15:31:37 +00002414 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002415
Douglas Gregor809070a2009-02-18 17:45:20 +00002416 if (Ty.isInvalid())
2417 return ExprError();
2418
Mike Stump1eb44332009-09-09 15:08:12 +00002419 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002420 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002421}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002422
2423/// objc-protocol-expression
2424/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002425ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002426Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002427 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002428
Chris Lattner4fef81d2008-08-05 06:19:09 +00002429 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002430 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2431
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002432 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002433
Chris Lattner4fef81d2008-08-05 06:19:09 +00002434 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002435 return ExprError(Diag(Tok, diag::err_expected_ident));
2436
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002437 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002438 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002439
Anders Carlsson4988ae32007-08-23 15:31:37 +00002440 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002441
Sebastian Redl1d922962008-12-13 15:32:12 +00002442 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2443 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002444}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002445
2446/// objc-selector-expression
2447/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002448ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002449 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002450
Chris Lattner4fef81d2008-08-05 06:19:09 +00002451 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002452 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2453
Chris Lattner5f9e2722011-07-23 10:55:15 +00002454 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002455 SourceLocation LParenLoc = ConsumeParen();
2456 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002457
2458 if (Tok.is(tok::code_completion)) {
2459 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2460 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002461 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002462 return ExprError();
2463 }
2464
Chris Lattner2fc5c242009-04-11 18:13:45 +00002465 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002466 if (!SelIdent && // missing selector name.
2467 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002468 return ExprError(Diag(Tok, diag::err_expected_ident));
2469
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002470 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002471 unsigned nColons = 0;
2472 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002473 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002474 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2475 ++nColons;
2476 KeyIdents.push_back(0);
2477 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002478 return ExprError(Diag(Tok, diag::err_expected_colon));
2479
Chris Lattner5add7542010-08-27 22:32:41 +00002480 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002481 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002482 if (Tok.is(tok::r_paren))
2483 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002484
2485 if (Tok.is(tok::code_completion)) {
2486 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2487 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002488 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002489 return ExprError();
2490 }
2491
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002492 // Check for another keyword selector.
2493 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002494 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002495 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002496 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002497 break;
2498 }
Steve Naroff887407e2007-12-05 22:21:29 +00002499 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002500 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002501 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002502 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2503 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002504 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002505
2506Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2507
2508 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2509 // Append the current token at the end of the new token stream so that it
2510 // doesn't get lost.
2511 LM.Toks.push_back(Tok);
2512 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2513
2514 // MDecl might be null due to error in method prototype, etc.
2515 Decl *MDecl = LM.D;
2516 // Consume the previously pushed token.
2517 ConsumeAnyToken();
2518
2519 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2520 SourceLocation BraceLoc = Tok.getLocation();
2521 // Enter a scope for the method body.
2522 ParseScope BodyScope(this,
2523 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2524
2525 // Tell the actions module that we have entered a method definition with the
2526 // specified Declarator for the method.
2527 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2528
2529 if (PP.isCodeCompletionEnabled()) {
2530 if (trySkippingFunctionBodyForCodeCompletion()) {
2531 BodyScope.Exit();
2532 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2533 }
2534 }
2535
2536 StmtResult FnBody(ParseCompoundStatementBody());
2537
2538 // If the function body could not be parsed, make a bogus compoundstmt.
2539 if (FnBody.isInvalid())
2540 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2541 MultiStmtArg(Actions), false);
2542
2543 // Leave the function body scope.
2544 BodyScope.Exit();
2545
2546 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2547}