blob: 1d6006260262bf07a4b5fd9d240ff288370df738 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000039 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000045 break;
John McCall7f040a92010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall7f040a92010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000053 SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
54 break;
John McCall7f040a92010-12-24 02:08:15 +000055 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000056 case tok::objc_implementation:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000057 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
58 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000059 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000060 return ParseObjCAtEndDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000062 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
64 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000065 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
67 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000068 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000069 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
70 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000071 default:
72 Diag(AtLoc, diag::err_unexpected_at);
73 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000074 SingleDecl = 0;
75 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000077 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000078}
79
80///
Mike Stump1eb44332009-09-09 15:08:12 +000081/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000084Parser::DeclGroupPtrTy
85Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 SmallVector<IdentifierInfo *, 8> ClassNames;
88 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000089
Mike Stump1eb44332009-09-09 15:08:12 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000092 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000093 Diag(Tok, diag::err_expected_ident);
94 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000098 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattnerdf195262007-10-09 17:51:17 +0000101 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 ConsumeToken();
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // Consume the ';'.
108 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000109 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekc09cba62009-11-17 23:12:20 +0000111 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
112 ClassLocs.data(),
113 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
Steve Naroffdac269b2007-08-20 21:31:48 +0000116///
117/// objc-interface:
118/// objc-class-interface-attributes[opt] objc-class-interface
119/// objc-category-interface
120///
121/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000122/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000123/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000124/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000125/// objc-interface-decl-list
126/// @end
127///
128/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000129/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000130/// objc-protocol-refs[opt]
131/// objc-interface-decl-list
132/// @end
133///
134/// objc-superclass:
135/// ':' identifier
136///
137/// objc-class-interface-attributes:
138/// __attribute__((visibility("default")))
139/// __attribute__((visibility("hidden")))
140/// __attribute__((deprecated))
141/// __attribute__((unavailable))
142/// __attribute__((objc_exception)) - used by NSException on 64-bit
143///
John McCall7f040a92010-12-24 02:08:15 +0000144Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
145 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000146 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
148 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000150 // Code completion after '@interface'.
151 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000152 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000153 cutOffParsing();
154 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000155 }
156
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000161
Steve Naroffdac269b2007-08-20 21:31:48 +0000162 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000163 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000165 if (Tok.is(tok::l_paren) &&
166 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000167 SourceLocation LParenLoc = ConsumeParen();
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 SourceLocation categoryLoc, rparenLoc;
169 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000170 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000172 cutOffParsing();
173 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000174 }
175
Steve Naroff527fe232007-08-23 19:56:30 +0000176 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 categoryId = Tok.getIdentifierInfo();
179 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000180 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000181 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000182 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000183 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000185
186 rparenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
187 if (rparenLoc.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000189
190 if (!attrs.empty()) { // categories don't support attributes.
191 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
192 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000193 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000194
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000195 // Next, we need to check for any protocol references.
196 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 SmallVector<Decl *, 8> ProtocolRefs;
198 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000199 if (Tok.is(tok::less) &&
200 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000201 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000202 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
John McCalld226f652010-08-21 09:40:31 +0000204 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000205 Actions.ActOnStartCategoryInterface(atLoc,
206 nameId, nameLoc,
207 categoryId, categoryLoc,
208 ProtocolRefs.data(),
209 ProtocolRefs.size(),
210 ProtocolLocs.data(),
211 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000212
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000213 if (Tok.is(tok::l_brace))
214 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
215
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000216 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000217 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000218 }
219 // Parse a class interface.
220 IdentifierInfo *superClassId = 0;
221 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000222
Chris Lattnerdf195262007-10-09 17:51:17 +0000223 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000224 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000225
226 // Code completion of superclass names.
227 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000228 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000229 cutOffParsing();
230 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000231 }
232
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000234 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000235 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000236 }
237 superClassId = Tok.getIdentifierInfo();
238 superClassLoc = ConsumeToken();
239 }
240 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 SmallVector<Decl *, 8> ProtocolRefs;
242 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000243 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000244 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000245 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
246 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000247 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
John McCalld226f652010-08-21 09:40:31 +0000249 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000250 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000251 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000252 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000253 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000254 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattnerdf195262007-10-09 17:51:17 +0000256 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000257 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000258
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000259 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000260 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000261}
262
John McCalld0014542009-12-03 22:31:13 +0000263/// The Objective-C property callback. This should be defined where
264/// it's used, but instead it's been lifted to here to support VS2005.
265struct Parser::ObjCPropertyCallback : FieldCallback {
266 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000268 ObjCDeclSpec &OCDS;
269 SourceLocation AtLoc;
270 tok::ObjCKeywordKind MethodImplKind;
271
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000272 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000274 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
275 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000276 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000277 MethodImplKind(MethodImplKind) {
278 }
279
John McCalld226f652010-08-21 09:40:31 +0000280 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000281 if (FD.D.getIdentifier() == 0) {
282 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
283 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000284 return 0;
John McCalld0014542009-12-03 22:31:13 +0000285 }
286 if (FD.BitfieldSize) {
287 P.Diag(AtLoc, diag::err_objc_property_bitfield)
288 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000289 return 0;
John McCalld0014542009-12-03 22:31:13 +0000290 }
291
292 // Install the property declarator into interfaceDecl.
293 IdentifierInfo *SelName =
294 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
295
296 Selector GetterSel =
297 P.PP.getSelectorTable().getNullarySelector(SelName);
298 IdentifierInfo *SetterName = OCDS.getSetterName();
299 Selector SetterSel;
300 if (SetterName)
301 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
302 else
303 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
304 P.PP.getSelectorTable(),
305 FD.D.getIdentifier());
306 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000307 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000308 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000309 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000310 &isOverridingProperty,
311 MethodImplKind);
312 if (!isOverridingProperty)
313 Props.push_back(Property);
314
315 return Property;
316 }
317};
318
Steve Naroffdac269b2007-08-20 21:31:48 +0000319/// objc-interface-decl-list:
320/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000321/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000322/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000323/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000324/// objc-interface-decl-list declaration
325/// objc-interface-decl-list ';'
326///
Steve Naroff294494e2007-08-22 16:35:03 +0000327/// objc-method-requirement: [OBJC2]
328/// @required
329/// @optional
330///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000331void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
332 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000333 SmallVector<Decl *, 32> allMethods;
334 SmallVector<Decl *, 16> allProperties;
335 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000336 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek782f2f52010-01-07 01:20:12 +0000338 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000339 Actions.ActOnObjCContainerStartDefinition(CDecl);
340
Steve Naroff294494e2007-08-22 16:35:03 +0000341 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000342 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000343 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000344 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000345 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000346 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000347 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
348 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000349 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
350 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000351 continue;
352 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000353 if (Tok.is(tok::l_paren)) {
354 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000355 ParseObjCMethodDecl(Tok.getLocation(),
356 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000357 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000358 continue;
359 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000360 // Ignore excess semicolons.
361 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000362 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000363 continue;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattnerbc662af2008-10-20 06:10:06 +0000366 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000367 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000368 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000370 // Code completion within an Objective-C interface.
371 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000372 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000373 ObjCImpDecl? Sema::PCC_ObjCImplementation
374 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000375 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000376 }
377
Chris Lattnere82a10f2008-10-20 05:46:22 +0000378 // If we don't have an @ directive, parse it as a function definition.
379 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000380 // The code below does not consume '}'s because it is afraid of eating the
381 // end of a namespace. Because of the way this code is structured, an
382 // erroneous r_brace would cause an infinite loop if not handled here.
383 if (Tok.is(tok::r_brace))
384 break;
John McCall0b7e6782011-03-24 11:26:52 +0000385 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000386 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000387 continue;
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnere82a10f2008-10-20 05:46:22 +0000390 // Otherwise, we have an @ directive, eat the @.
391 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000392 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000393 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000394 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000395 break;
396 }
397
Chris Lattnera2449b22008-10-20 05:57:40 +0000398 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnera2449b22008-10-20 05:57:40 +0000400 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000401 AtEnd.setBegin(AtLoc);
402 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000403 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000404 } else if (DirectiveKind == tok::objc_not_keyword) {
405 Diag(Tok, diag::err_objc_unknown_at);
406 SkipUntil(tok::semi);
407 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnerbc662af2008-10-20 06:10:06 +0000410 // Eat the identifier.
411 ConsumeToken();
412
Chris Lattnera2449b22008-10-20 05:57:40 +0000413 switch (DirectiveKind) {
414 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000415 // FIXME: If someone forgets an @end on a protocol, this loop will
416 // continue to eat up tons of stuff and spew lots of nonsense errors. It
417 // would probably be better to bail out if we saw an @class or @interface
418 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000419 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000420 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000421 SkipUntil(tok::r_brace, tok::at);
422 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000423
424 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000425 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000426 Diag(Tok, diag::err_objc_missing_end);
427 ConsumeToken();
428 break;
429
Chris Lattnera2449b22008-10-20 05:57:40 +0000430 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000431 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000433 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000434 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000435 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000436 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000437 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000438 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattnera2449b22008-10-20 05:57:40 +0000440 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000441 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000442 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000443
Chris Lattnere82a10f2008-10-20 05:46:22 +0000444 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000445 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000446 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000447 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000449 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000450 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000451
Chris Lattnere82a10f2008-10-20 05:46:22 +0000452 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000453 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000454 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
John McCall7da19ea2011-03-26 01:53:26 +0000456 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000457 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000458 }
Steve Naroff294494e2007-08-22 16:35:03 +0000459 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000460
461 // We break out of the big loop in two cases: when we see @end or when we see
462 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000463 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000464 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000465 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000466 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000467 ConsumeToken(); // the "end" identifier
468 else
469 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000472 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000473 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000474 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000475 allProperties.data(), allProperties.size(),
476 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000477}
478
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000479/// Parse property attribute declarations.
480///
481/// property-attr-decl: '(' property-attrlist ')'
482/// property-attrlist:
483/// property-attribute
484/// property-attrlist ',' property-attribute
485/// property-attribute:
486/// getter '=' identifier
487/// setter '=' identifier ':'
488/// readonly
489/// readwrite
490/// assign
491/// retain
492/// copy
493/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000494/// atomic
495/// strong
496/// weak
497/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000498///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000499void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000500 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000501 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000503 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000504 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000505 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000506 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000507 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000508 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000510 // If this is not an identifier at all, bail out early.
511 if (II == 0) {
512 MatchRHSPunctuation(tok::r_paren, LHSLoc);
513 return;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner156b0612008-10-20 07:37:22 +0000516 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner92e62b02008-11-20 04:42:34 +0000518 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000519 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000520 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000521 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000522 else if (II->isStr("unsafe_unretained"))
523 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000524 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000525 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000526 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000527 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000528 else if (II->isStr("strong"))
529 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000530 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000531 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000532 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000533 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000534 else if (II->isStr("atomic"))
535 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000536 else if (II->isStr("weak"))
537 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000538 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000539 bool IsSetter = II->getNameStart()[0] == 's';
540
Chris Lattnere00da7c2008-10-20 07:39:53 +0000541 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000542 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
543 diag::err_objc_expected_equal_for_getter;
544
545 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000546 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor4ad96852009-11-19 07:41:15 +0000548 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000549 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000550 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000551 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000552 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000553 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000554 }
555
Anders Carlsson42499be2010-10-02 17:45:21 +0000556
557 SourceLocation SelLoc;
558 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
559
560 if (!SelIdent) {
561 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
562 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000563 SkipUntil(tok::r_paren);
564 return;
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Anders Carlsson42499be2010-10-02 17:45:21 +0000567 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000568 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000569 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000571 if (ExpectAndConsume(tok::colon,
572 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000573 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000574 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000575 } else {
576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000577 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000578 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000579 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000580 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000581 SkipUntil(tok::r_paren);
582 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner156b0612008-10-20 07:37:22 +0000585 if (Tok.isNot(tok::comma))
586 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner156b0612008-10-20 07:37:22 +0000588 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner156b0612008-10-20 07:37:22 +0000591 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000592}
593
Steve Naroff3536b442007-09-06 21:24:23 +0000594/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000595/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000596/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000597///
598/// objc-instance-method: '-'
599/// objc-class-method: '+'
600///
Steve Naroff4985ace2007-08-22 18:35:33 +0000601/// objc-method-attributes: [OBJC2]
602/// __attribute__((deprecated))
603///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000604Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000605 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000606 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000607
Mike Stump1eb44332009-09-09 15:08:12 +0000608 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000609 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000610 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000611 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000612 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000613 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000614 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000615}
616
617/// objc-selector:
618/// identifier
619/// one of
620/// enum struct union if else while do for switch case default
621/// break continue return goto asm sizeof typeof __alignof
622/// unsigned long const short volatile signed restrict _Complex
623/// in out inout bycopy byref oneway int char float double void _Bool
624///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000625IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000626
Chris Lattnerff384912007-10-07 02:00:24 +0000627 switch (Tok.getKind()) {
628 default:
629 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000630 case tok::ampamp:
631 case tok::ampequal:
632 case tok::amp:
633 case tok::pipe:
634 case tok::tilde:
635 case tok::exclaim:
636 case tok::exclaimequal:
637 case tok::pipepipe:
638 case tok::pipeequal:
639 case tok::caret:
640 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000641 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000642 if (isalpha(ThisTok[0])) {
643 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
644 Tok.setKind(tok::identifier);
645 SelectorLoc = ConsumeToken();
646 return II;
647 }
648 return 0;
649 }
650
Chris Lattnerff384912007-10-07 02:00:24 +0000651 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000652 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000653 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000654 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000655 case tok::kw_break:
656 case tok::kw_case:
657 case tok::kw_catch:
658 case tok::kw_char:
659 case tok::kw_class:
660 case tok::kw_const:
661 case tok::kw_const_cast:
662 case tok::kw_continue:
663 case tok::kw_default:
664 case tok::kw_delete:
665 case tok::kw_do:
666 case tok::kw_double:
667 case tok::kw_dynamic_cast:
668 case tok::kw_else:
669 case tok::kw_enum:
670 case tok::kw_explicit:
671 case tok::kw_export:
672 case tok::kw_extern:
673 case tok::kw_false:
674 case tok::kw_float:
675 case tok::kw_for:
676 case tok::kw_friend:
677 case tok::kw_goto:
678 case tok::kw_if:
679 case tok::kw_inline:
680 case tok::kw_int:
681 case tok::kw_long:
682 case tok::kw_mutable:
683 case tok::kw_namespace:
684 case tok::kw_new:
685 case tok::kw_operator:
686 case tok::kw_private:
687 case tok::kw_protected:
688 case tok::kw_public:
689 case tok::kw_register:
690 case tok::kw_reinterpret_cast:
691 case tok::kw_restrict:
692 case tok::kw_return:
693 case tok::kw_short:
694 case tok::kw_signed:
695 case tok::kw_sizeof:
696 case tok::kw_static:
697 case tok::kw_static_cast:
698 case tok::kw_struct:
699 case tok::kw_switch:
700 case tok::kw_template:
701 case tok::kw_this:
702 case tok::kw_throw:
703 case tok::kw_true:
704 case tok::kw_try:
705 case tok::kw_typedef:
706 case tok::kw_typeid:
707 case tok::kw_typename:
708 case tok::kw_typeof:
709 case tok::kw_union:
710 case tok::kw_unsigned:
711 case tok::kw_using:
712 case tok::kw_virtual:
713 case tok::kw_void:
714 case tok::kw_volatile:
715 case tok::kw_wchar_t:
716 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000717 case tok::kw__Bool:
718 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000719 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000720 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000721 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000722 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000723 }
Steve Naroff294494e2007-08-22 16:35:03 +0000724}
725
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000726/// objc-for-collection-in: 'in'
727///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000728bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000729 // FIXME: May have to do additional look-ahead to only allow for
730 // valid tokens following an 'in'; such as an identifier, unary operators,
731 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000732 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000733 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000734}
735
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000737/// qualifier list and builds their bitmask representation in the input
738/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000739///
740/// objc-type-qualifiers:
741/// objc-type-qualifier
742/// objc-type-qualifiers objc-type-qualifier
743///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000744void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
745 ObjCTypeNameContext Context) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000746 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000747 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000748 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
749 Context == OTN_ParameterType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000750 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000751 }
752
Chris Lattnercb53b362007-12-27 19:57:00 +0000753 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000754 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattnere8b724d2007-12-12 06:56:32 +0000756 const IdentifierInfo *II = Tok.getIdentifierInfo();
757 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000758 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000759 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000762 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000763 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
765 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
766 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
767 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
768 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
769 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000770 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000771 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000772 ConsumeToken();
773 II = 0;
774 break;
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnere8b724d2007-12-12 06:56:32 +0000777 // If this wasn't a recognized qualifier, bail out.
778 if (II) return;
779 }
780}
781
782/// objc-type-name:
783/// '(' objc-type-qualifiers[opt] type-name ')'
784/// '(' objc-type-qualifiers[opt] ')'
785///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000786ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
787 ObjCTypeNameContext Context) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000788 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner4a76b292008-10-22 03:52:06 +0000790 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000791 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000792 ObjCDeclContextSwitch ObjCDC(*this);
793
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000794 // Parse type qualifiers, in, inout, etc.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000795 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000796
John McCallb3d87482010-08-24 05:47:05 +0000797 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000798 if (isTypeSpecifierQualifier()) {
John McCallf85e1932011-06-15 23:02:42 +0000799 TypeResult TypeSpec =
800 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000801 if (!TypeSpec.isInvalid())
802 Ty = TypeSpec.get();
Douglas Gregore97179c2011-09-08 01:46:34 +0000803 } else if (Context == OTN_ResultType && Tok.is(tok::identifier)) {
804 if (!Ident_instancetype)
805 Ident_instancetype = PP.getIdentifierInfo("instancetype");
806
807 if (Tok.getIdentifierInfo() == Ident_instancetype) {
808 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
809 ConsumeToken();
810 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000811 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000812
Steve Naroffd7333c22008-10-21 14:15:04 +0000813 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000814 ConsumeParen();
815 else if (Tok.getLocation() == TypeStartLoc) {
816 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000817 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000818 SkipUntil(tok::r_paren);
819 } else {
820 // Otherwise, we found *something*, but didn't get a ')' in the right
821 // place. Emit an error then return what we have as the type.
822 MatchRHSPunctuation(tok::r_paren, LParenLoc);
823 }
Steve Narofff28b2642007-09-05 23:30:30 +0000824 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000825}
826
827/// objc-method-decl:
828/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000829/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000830/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000831/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000832///
833/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000834/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000835/// objc-keyword-selector objc-keyword-decl
836///
837/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000838/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
839/// objc-selector ':' objc-keyword-attributes[opt] identifier
840/// ':' objc-type-name objc-keyword-attributes[opt] identifier
841/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000842///
Steve Naroff4985ace2007-08-22 18:35:33 +0000843/// objc-parmlist:
844/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000845///
Steve Naroff4985ace2007-08-22 18:35:33 +0000846/// objc-parms:
847/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000848///
Steve Naroff4985ace2007-08-22 18:35:33 +0000849/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000850/// , ...
851///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000852/// objc-keyword-attributes: [OBJC2]
853/// __attribute__((unused))
854///
John McCalld226f652010-08-21 09:40:31 +0000855Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000856 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000857 tok::ObjCKeywordKind MethodImplKind,
858 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000859 ParsingDeclRAIIObject PD(*this);
860
Douglas Gregore8f5a172010-04-07 00:21:17 +0000861 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000862 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000863 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000864 cutOffParsing();
865 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000866 }
867
Chris Lattnere8904e92008-08-23 01:48:03 +0000868 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000869 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000870 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000871 if (Tok.is(tok::l_paren))
Douglas Gregorb77cab92011-03-08 19:17:54 +0000872 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Ted Kremenek9e049352010-02-18 23:05:16 +0000874 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000875 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000876 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000877 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000878
Douglas Gregore8f5a172010-04-07 00:21:17 +0000879 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000880 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000881 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000882 cutOffParsing();
883 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000884 }
885
Ted Kremenek9e049352010-02-18 23:05:16 +0000886 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000887 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000888 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000889
Steve Naroff84c43102009-02-11 20:43:13 +0000890 // An unnamed colon is valid.
891 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000892 Diag(Tok, diag::err_expected_selector_for_method)
893 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000894 // Skip until we get a ; or {}.
895 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000896 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Chris Lattner5f9e2722011-07-23 10:55:15 +0000899 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000900 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000901 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000902 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000903 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattnerff384912007-10-07 02:00:24 +0000905 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000906 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000907 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000908 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000909 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000910 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000911 methodAttrs.getList(), MethodImplKind,
912 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000913 PD.complete(Result);
914 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000915 }
Steve Narofff28b2642007-09-05 23:30:30 +0000916
Chris Lattner5f9e2722011-07-23 10:55:15 +0000917 SmallVector<IdentifierInfo *, 12> KeyIdents;
918 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000919 ParseScope PrototypeScope(this,
920 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000921
922 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000923
Chris Lattnerff384912007-10-07 02:00:24 +0000924 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000925 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000926 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Chris Lattnerff384912007-10-07 02:00:24 +0000928 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000929 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000930 Diag(Tok, diag::err_expected_colon);
931 break;
932 }
933 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000934
John McCallb3d87482010-08-24 05:47:05 +0000935 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000936 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregorb77cab92011-03-08 19:17:54 +0000937 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000938
Chris Lattnerff384912007-10-07 02:00:24 +0000939 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000940 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +0000941 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +0000942 MaybeParseGNUAttributes(paramAttrs);
943 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +0000944 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000945
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000946 // Code completion for the next piece of the selector.
947 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000948 KeyIdents.push_back(SelIdent);
949 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
950 mType == tok::minus,
951 /*AtParameterName=*/true,
952 ReturnType,
953 KeyIdents.data(),
954 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000955 cutOffParsing();
956 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000957 }
958
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000960 Diag(Tok, diag::err_expected_ident); // missing argument name.
961 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Chris Lattnere294d3f2009-04-11 18:57:04 +0000964 ArgInfo.Name = Tok.getIdentifierInfo();
965 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000966 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattnere294d3f2009-04-11 18:57:04 +0000968 ArgInfos.push_back(ArgInfo);
969 KeyIdents.push_back(SelIdent);
970
John McCall0b7e6782011-03-24 11:26:52 +0000971 // Make sure the attributes persist.
972 allParamAttrs.takeAllFrom(paramAttrs.getPool());
973
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000974 // Code completion for the next piece of the selector.
975 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000976 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
977 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000978 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000979 ReturnType,
980 KeyIdents.data(),
981 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000982 cutOffParsing();
983 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000984 }
985
Chris Lattnerff384912007-10-07 02:00:24 +0000986 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000987 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000988 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000989 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000990 break;
991 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Steve Naroff335eafa2007-11-15 12:35:21 +0000994 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattnerff384912007-10-07 02:00:24 +0000996 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000997 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000998 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000999 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001000 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001001 ConsumeToken();
1002 break;
1003 }
John McCall0b7e6782011-03-24 11:26:52 +00001004 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001005 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001006 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001007 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1008 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001009 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001010 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001011 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1012 ParmDecl.getIdentifierLoc(),
1013 Param,
1014 0));
1015
Chris Lattnerff384912007-10-07 02:00:24 +00001016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001018 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001019 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001020 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001021 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001022
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001023 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001024 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001025
Chris Lattnerff384912007-10-07 02:00:24 +00001026 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1027 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001028 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001029 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001030 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001031 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001032 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001033 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001034 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001035
John McCall54abf7d2009-11-04 02:18:39 +00001036 PD.complete(Result);
1037 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001038}
1039
Steve Naroffdac269b2007-08-20 21:31:48 +00001040/// objc-protocol-refs:
1041/// '<' identifier-list '>'
1042///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001043bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001044ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1045 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001046 bool WarnOnDeclarations,
1047 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001048 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001050 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Chris Lattner5f9e2722011-07-23 10:55:15 +00001052 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Chris Lattnere13b9592008-07-26 04:03:38 +00001054 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001055 if (Tok.is(tok::code_completion)) {
1056 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1057 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001058 cutOffParsing();
1059 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001060 }
1061
Chris Lattnere13b9592008-07-26 04:03:38 +00001062 if (Tok.isNot(tok::identifier)) {
1063 Diag(Tok, diag::err_expected_ident);
1064 SkipUntil(tok::greater);
1065 return true;
1066 }
1067 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1068 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001069 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001070 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattnere13b9592008-07-26 04:03:38 +00001072 if (Tok.isNot(tok::comma))
1073 break;
1074 ConsumeToken();
1075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Chris Lattnere13b9592008-07-26 04:03:38 +00001077 // Consume the '>'.
1078 if (Tok.isNot(tok::greater)) {
1079 Diag(Tok, diag::err_expected_greater);
1080 return true;
1081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattnere13b9592008-07-26 04:03:38 +00001083 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattnere13b9592008-07-26 04:03:38 +00001085 // Convert the list of protocols identifiers into a list of protocol decls.
1086 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1087 &ProtocolIdents[0], ProtocolIdents.size(),
1088 Protocols);
1089 return false;
1090}
1091
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001092/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1093/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001094bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001095 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1096 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1097 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001098 SmallVector<Decl *, 8> ProtocolDecl;
1099 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001100 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1101 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001102 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1103 ProtocolLocs.data(), LAngleLoc);
1104 if (EndProtoLoc.isValid())
1105 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001106 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001107}
1108
1109
Steve Naroffdac269b2007-08-20 21:31:48 +00001110/// objc-class-instance-variables:
1111/// '{' objc-instance-variable-decl-list[opt] '}'
1112///
1113/// objc-instance-variable-decl-list:
1114/// objc-visibility-spec
1115/// objc-instance-variable-decl ';'
1116/// ';'
1117/// objc-instance-variable-decl-list objc-visibility-spec
1118/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1119/// objc-instance-variable-decl-list ';'
1120///
1121/// objc-visibility-spec:
1122/// @private
1123/// @protected
1124/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001125/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001126///
1127/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001128/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001129///
John McCalld226f652010-08-21 09:40:31 +00001130void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001131 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001132 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001133 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001134 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001135
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001136 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001137
Steve Naroffddbff782007-08-21 21:17:12 +00001138 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Steve Naroffddbff782007-08-21 21:17:12 +00001140 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001141 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001142 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Steve Naroffddbff782007-08-21 21:17:12 +00001144 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001145 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001146 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001147 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001148 ConsumeToken();
1149 continue;
1150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Steve Naroffddbff782007-08-21 21:17:12 +00001152 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001153 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001154 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001155
1156 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001157 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001158 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001159 }
1160
Steve Naroff861cf3e2007-08-23 18:16:40 +00001161 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001162 case tok::objc_private:
1163 case tok::objc_public:
1164 case tok::objc_protected:
1165 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001166 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001167 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001168 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001169 default:
1170 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001171 continue;
1172 }
1173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001175 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001176 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001177 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001178 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001179 }
1180
John McCallbdd563e2009-11-03 02:38:08 +00001181 struct ObjCIvarCallback : FieldCallback {
1182 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001183 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001184 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001185 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001186
John McCalld226f652010-08-21 09:40:31 +00001187 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001188 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001189 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1190 }
1191
John McCalld226f652010-08-21 09:40:31 +00001192 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001193 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001194 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001195 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001196 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001197 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001198 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001199 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001200 if (Field)
1201 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001202 return Field;
1203 }
1204 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001205
Chris Lattnere1359422008-04-10 06:46:29 +00001206 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001207 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001208 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattnerdf195262007-10-09 17:51:17 +00001210 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001211 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001212 } else {
1213 Diag(Tok, diag::err_expected_semi_decl_list);
1214 // Skip to end of block or statement
1215 SkipUntil(tok::r_brace, true, true);
1216 }
1217 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001218 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001219 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1220 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001221 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001222 // Call ActOnFields() even if we don't have any decls. This is useful
1223 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001224 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001225 AllIvarDecls,
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001226 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001227 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001228}
Steve Naroffdac269b2007-08-20 21:31:48 +00001229
1230/// objc-protocol-declaration:
1231/// objc-protocol-definition
1232/// objc-protocol-forward-reference
1233///
1234/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001235/// @protocol identifier
1236/// objc-protocol-refs[opt]
1237/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001238/// @end
1239///
1240/// objc-protocol-forward-reference:
1241/// @protocol identifier-list ';'
1242///
1243/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001244/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001245/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001246Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001247 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001248 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001249 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1250 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Douglas Gregor083128f2009-11-18 04:49:41 +00001252 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001253 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001254 cutOffParsing();
1255 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001256 }
1257
Chris Lattnerdf195262007-10-09 17:51:17 +00001258 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001259 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001260 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001261 }
1262 // Save the protocol name, then consume it.
1263 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1264 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Chris Lattnerdf195262007-10-09 17:51:17 +00001266 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001267 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001268 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001269 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001270 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Chris Lattnerdf195262007-10-09 17:51:17 +00001273 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001274 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001275 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1276
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001277 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001278 while (1) {
1279 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001280 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001281 Diag(Tok, diag::err_expected_ident);
1282 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001283 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001284 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001285 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1286 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001287 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Chris Lattnerdf195262007-10-09 17:51:17 +00001289 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001290 break;
1291 }
1292 // Consume the ';'.
1293 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001294 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Steve Naroffe440eb82007-10-10 17:32:04 +00001296 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001297 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001298 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001299 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001300 }
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001302 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001303 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001304
Chris Lattner5f9e2722011-07-23 10:55:15 +00001305 SmallVector<Decl *, 8> ProtocolRefs;
1306 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001307 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001308 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1309 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001310 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
John McCalld226f652010-08-21 09:40:31 +00001312 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001313 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001314 ProtocolRefs.data(),
1315 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001316 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001317 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001318
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001319 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001320 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001321}
Steve Naroffdac269b2007-08-20 21:31:48 +00001322
1323/// objc-implementation:
1324/// objc-class-implementation-prologue
1325/// objc-category-implementation-prologue
1326///
1327/// objc-class-implementation-prologue:
1328/// @implementation identifier objc-superclass[opt]
1329/// objc-class-instance-variables[opt]
1330///
1331/// objc-category-implementation-prologue:
1332/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001333Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001334 SourceLocation atLoc) {
1335 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1336 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1337 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001339 // Code completion after '@implementation'.
1340 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001341 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001342 cutOffParsing();
1343 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001344 }
1345
Chris Lattnerdf195262007-10-09 17:51:17 +00001346 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001348 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001349 }
1350 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001351 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001353
1354 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001356 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001357 SourceLocation categoryLoc, rparenLoc;
1358 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001360 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001361 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001362 cutOffParsing();
1363 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001364 }
1365
Chris Lattnerdf195262007-10-09 17:51:17 +00001366 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001367 categoryId = Tok.getIdentifierInfo();
1368 categoryLoc = ConsumeToken();
1369 } else {
1370 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001371 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001372 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001373 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001374 Diag(Tok, diag::err_expected_rparen);
1375 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001376 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001377 }
1378 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001379 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001380 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001381 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001382
1383 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001385 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001386 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001387 }
1388 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001389 SourceLocation superClassLoc;
1390 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001391 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001392 // We have a super class
1393 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001394 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001395 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001396 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001397 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001398 superClassId = Tok.getIdentifierInfo();
1399 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001400 }
John McCalld226f652010-08-21 09:40:31 +00001401 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001402 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001403 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Steve Naroff60fccee2007-10-29 21:38:07 +00001405 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001406 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1407
1408 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001410 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001411 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001412}
Steve Naroff60fccee2007-10-29 21:38:07 +00001413
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001414Parser::DeclGroupPtrTy
1415Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1417 "ParseObjCAtEndDeclaration(): Expected @end");
1418 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001419 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001420 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001421 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1422 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1423 DeclsInGroup.push_back(D);
1424 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001425 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001426
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001427 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001428 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001429 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001430 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001431 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001432 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001433 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001434
1435 LateParsedObjCMethods.clear();
1436 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001437 return Actions.BuildDeclaratorGroup(
1438 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001439}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001440
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001441Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1442 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001443 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001444 return Actions.ConvertDeclToDeclGroup(0);
1445 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001446 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001447 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1448}
1449
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001450/// compatibility-alias-decl:
1451/// @compatibility_alias alias-name class-name ';'
1452///
John McCalld226f652010-08-21 09:40:31 +00001453Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001454 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1455 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1456 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001457 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001458 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001459 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001460 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001461 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1462 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001463 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001464 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001465 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001466 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001467 IdentifierInfo *classId = Tok.getIdentifierInfo();
1468 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001469 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1470 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001471 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1472 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001473}
1474
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001475/// property-synthesis:
1476/// @synthesize property-ivar-list ';'
1477///
1478/// property-ivar-list:
1479/// property-ivar
1480/// property-ivar-list ',' property-ivar
1481///
1482/// property-ivar:
1483/// identifier
1484/// identifier '=' identifier
1485///
John McCalld226f652010-08-21 09:40:31 +00001486Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001487 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1488 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001489 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Douglas Gregorb328c422009-11-18 19:45:45 +00001491 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001492 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001493 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001494 cutOffParsing();
1495 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001496 }
1497
Douglas Gregorb328c422009-11-18 19:45:45 +00001498 if (Tok.isNot(tok::identifier)) {
1499 Diag(Tok, diag::err_synthesized_property_name);
1500 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001501 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001502 }
1503
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001504 IdentifierInfo *propertyIvar = 0;
1505 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1506 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001507 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001508 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001509 // property '=' ivar-name
1510 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001511
1512 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001513 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001514 cutOffParsing();
1515 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001516 }
1517
Chris Lattnerdf195262007-10-09 17:51:17 +00001518 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001519 Diag(Tok, diag::err_expected_ident);
1520 break;
1521 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001522 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001523 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001524 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001525 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001526 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001527 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001528 break;
1529 ConsumeToken(); // consume ','
1530 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001531 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001532 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001533}
1534
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001535/// property-dynamic:
1536/// @dynamic property-list
1537///
1538/// property-list:
1539/// identifier
1540/// property-list ',' identifier
1541///
John McCalld226f652010-08-21 09:40:31 +00001542Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001543 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1544 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001545 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001546 while (true) {
1547 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001548 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001549 cutOffParsing();
1550 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001551 }
1552
1553 if (Tok.isNot(tok::identifier)) {
1554 Diag(Tok, diag::err_expected_ident);
1555 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001556 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001557 }
1558
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001559 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1560 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001561 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001562 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001563
Chris Lattnerdf195262007-10-09 17:51:17 +00001564 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001565 break;
1566 ConsumeToken(); // consume ','
1567 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001568 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001569 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001570}
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001572/// objc-throw-statement:
1573/// throw expression[opt];
1574///
John McCall60d7b3a2010-08-24 06:29:42 +00001575StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1576 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001577 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001578 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001579 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001580 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001581 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001582 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001583 }
1584 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001585 // consume ';'
1586 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001587 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001588}
1589
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001590/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001591/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001592///
John McCall60d7b3a2010-08-24 06:29:42 +00001593StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001594Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001595 ConsumeToken(); // consume synchronized
1596 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001597 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001598 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001599 }
John McCall07524032011-07-27 21:50:02 +00001600
1601 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001602 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001603 ExprResult operand(ParseExpression());
1604
1605 if (Tok.is(tok::r_paren)) {
1606 ConsumeParen(); // ')'
1607 } else {
1608 if (!operand.isInvalid())
1609 Diag(Tok, diag::err_expected_rparen);
1610
1611 // Skip forward until we see a left brace, but don't consume it.
1612 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001613 }
John McCall07524032011-07-27 21:50:02 +00001614
1615 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001616 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001617 if (!operand.isInvalid())
1618 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001619 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001620 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001621
John McCall07524032011-07-27 21:50:02 +00001622 // Check the @synchronized operand now.
1623 if (!operand.isInvalid())
1624 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001625
John McCall07524032011-07-27 21:50:02 +00001626 // Parse the compound statement within a new scope.
1627 ParseScope bodyScope(this, Scope::DeclScope);
1628 StmtResult body(ParseCompoundStatementBody());
1629 bodyScope.Exit();
1630
1631 // If there was a semantic or parse error earlier with the
1632 // operand, fail now.
1633 if (operand.isInvalid())
1634 return StmtError();
1635
1636 if (body.isInvalid())
1637 body = Actions.ActOnNullStmt(Tok.getLocation());
1638
1639 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001640}
1641
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001642/// objc-try-catch-statement:
1643/// @try compound-statement objc-catch-list[opt]
1644/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1645///
1646/// objc-catch-list:
1647/// @catch ( parameter-declaration ) compound-statement
1648/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1649/// catch-parameter-declaration:
1650/// parameter-declaration
1651/// '...' [OBJC2]
1652///
John McCall60d7b3a2010-08-24 06:29:42 +00001653StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001654 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001655
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001656 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001657 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001658 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001659 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001660 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001661 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001662 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001663 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001664 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001665 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001666 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001667 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001668
Chris Lattnerdf195262007-10-09 17:51:17 +00001669 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001670 // At this point, we need to lookahead to determine if this @ is the start
1671 // of an @catch or @finally. We don't want to consume the @ token if this
1672 // is an @try or @encode or something else.
1673 Token AfterAt = GetLookAheadToken(1);
1674 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1675 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1676 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001677
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001678 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001679 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001680 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001681 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001682 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001683 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001684 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001685 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001686 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001687 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001688 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001689 ParseDeclarator(ParmDecl);
1690
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001691 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001692 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001693 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001694 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001695 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Steve Naroff93a25952009-04-07 22:56:58 +00001697 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Steve Naroff93a25952009-04-07 22:56:58 +00001699 if (Tok.is(tok::r_paren))
1700 RParenLoc = ConsumeParen();
1701 else // Skip over garbage, until we get to ')'. Eat the ')'.
1702 SkipUntil(tok::r_paren, true, false);
1703
John McCall60d7b3a2010-08-24 06:29:42 +00001704 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001705 if (Tok.is(tok::l_brace))
1706 CatchBody = ParseCompoundStatementBody();
1707 else
1708 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001709 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001710 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001711
John McCall60d7b3a2010-08-24 06:29:42 +00001712 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001713 RParenLoc,
1714 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001715 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001716 if (!Catch.isInvalid())
1717 CatchStmts.push_back(Catch.release());
1718
Steve Naroff64515f32008-02-05 21:27:35 +00001719 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001720 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1721 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001722 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001723 }
1724 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001725 } else {
1726 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001727 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001728 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001729
John McCall60d7b3a2010-08-24 06:29:42 +00001730 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001731 if (Tok.is(tok::l_brace))
1732 FinallyBody = ParseCompoundStatementBody();
1733 else
1734 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001735 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001736 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001737 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001738 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001739 catch_or_finally_seen = true;
1740 break;
1741 }
1742 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001743 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001744 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001745 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001746 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001747
John McCall9ae2f072010-08-23 23:25:46 +00001748 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001749 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001750 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001751}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001752
John McCallf85e1932011-06-15 23:02:42 +00001753/// objc-autoreleasepool-statement:
1754/// @autoreleasepool compound-statement
1755///
1756StmtResult
1757Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1758 ConsumeToken(); // consume autoreleasepool
1759 if (Tok.isNot(tok::l_brace)) {
1760 Diag(Tok, diag::err_expected_lbrace);
1761 return StmtError();
1762 }
1763 // Enter a scope to hold everything within the compound stmt. Compound
1764 // statements can always hold declarations.
1765 ParseScope BodyScope(this, Scope::DeclScope);
1766
1767 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1768
1769 BodyScope.Exit();
1770 if (AutoreleasePoolBody.isInvalid())
1771 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1772 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1773 AutoreleasePoolBody.take());
1774}
1775
Steve Naroff3536b442007-09-06 21:24:23 +00001776/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001777///
John McCalld226f652010-08-21 09:40:31 +00001778Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001779 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001780
John McCallf312b1e2010-08-26 23:41:50 +00001781 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1782 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001784 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001785 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001786 if (ObjCImpDecl) {
1787 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001788 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001789 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001790 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001791 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001792
Steve Naroff409be832007-11-11 19:54:21 +00001793 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001794 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001795 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Steve Naroff409be832007-11-11 19:54:21 +00001797 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1798 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Steve Naroff409be832007-11-11 19:54:21 +00001800 // If we didn't find the '{', bail out.
1801 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001802 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001803 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001804 // Allow the rest of sema to find private method decl implementations.
1805 if (MDecl)
1806 Actions.AddAnyMethodToGlobalPool(MDecl);
1807
1808 // Consume the tokens and store them for later parsing.
1809 LexedMethod* LM = new LexedMethod(this, MDecl);
1810 LateParsedObjCMethods.push_back(LM);
1811 CachedTokens &Toks = LM->Toks;
1812 // Begin by storing the '{' token.
1813 Toks.push_back(Tok);
1814 ConsumeBrace();
1815 // Consume everything up to (and including) the matching right brace.
1816 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001817 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001818}
Anders Carlsson55085182007-08-21 17:43:55 +00001819
John McCall60d7b3a2010-08-24 06:29:42 +00001820StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001821 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001822 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001823 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001824 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001825 }
1826
1827 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001828 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001829
1830 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001831 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001832
1833 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001834 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001835
1836 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1837 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001838
John McCall60d7b3a2010-08-24 06:29:42 +00001839 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001840 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001841 // If the expression is invalid, skip ahead to the next semicolon. Not
1842 // doing this opens us up to the possibility of infinite loops if
1843 // ParseExpression does not consume any tokens.
1844 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001845 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001846 }
Chris Lattner5d803162009-12-07 16:33:19 +00001847
Steve Naroff64515f32008-02-05 21:27:35 +00001848 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001849 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001850 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001851}
1852
John McCall60d7b3a2010-08-24 06:29:42 +00001853ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001854 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001855 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001856 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001857 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001858 return ExprError();
1859
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001860 case tok::string_literal: // primary-expression: string-literal
1861 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001862 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001863 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001864 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001865 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001866
Chris Lattner4fef81d2008-08-05 06:19:09 +00001867 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1868 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001869 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001870 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001871 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001872 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001873 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001874 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001875 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001876 }
Anders Carlsson55085182007-08-21 17:43:55 +00001877 }
Anders Carlsson55085182007-08-21 17:43:55 +00001878}
1879
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001880/// \brirg Parse the receiver of an Objective-C++ message send.
1881///
1882/// This routine parses the receiver of a message send in
1883/// Objective-C++ either as a type or as an expression. Note that this
1884/// routine must not be called to parse a send to 'super', since it
1885/// has no way to return such a result.
1886///
1887/// \param IsExpr Whether the receiver was parsed as an expression.
1888///
1889/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1890/// IsExpr is true), the parsed expression. If the receiver was parsed
1891/// as a type (\c IsExpr is false), the parsed type.
1892///
1893/// \returns True if an error occurred during parsing or semantic
1894/// analysis, in which case the arguments do not have valid
1895/// values. Otherwise, returns false for a successful parse.
1896///
1897/// objc-receiver: [C++]
1898/// 'super' [not parsed here]
1899/// expression
1900/// simple-type-specifier
1901/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001902bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001903 InMessageExpressionRAIIObject InMessage(*this, true);
1904
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001905 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1906 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1907 TryAnnotateTypeOrScopeToken();
1908
1909 if (!isCXXSimpleTypeSpecifier()) {
1910 // objc-receiver:
1911 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001912 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001913 if (Receiver.isInvalid())
1914 return true;
1915
1916 IsExpr = true;
1917 TypeOrExpr = Receiver.take();
1918 return false;
1919 }
1920
1921 // objc-receiver:
1922 // typename-specifier
1923 // simple-type-specifier
1924 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001925 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001926 ParseCXXSimpleTypeSpecifier(DS);
1927
1928 if (Tok.is(tok::l_paren)) {
1929 // If we see an opening parentheses at this point, we are
1930 // actually parsing an expression that starts with a
1931 // function-style cast, e.g.,
1932 //
1933 // postfix-expression:
1934 // simple-type-specifier ( expression-list [opt] )
1935 // typename-specifier ( expression-list [opt] )
1936 //
1937 // Parse the remainder of this case, then the (optional)
1938 // postfix-expression suffix, followed by the (optional)
1939 // right-hand side of the binary expression. We have an
1940 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001941 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001942 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001943 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001944 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001945 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001946 if (Receiver.isInvalid())
1947 return true;
1948
1949 IsExpr = true;
1950 TypeOrExpr = Receiver.take();
1951 return false;
1952 }
1953
1954 // We have a class message. Turn the simple-type-specifier or
1955 // typename-specifier we parsed into a type and parse the
1956 // remainder of the class message.
1957 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001958 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001959 if (Type.isInvalid())
1960 return true;
1961
1962 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001963 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001964 return false;
1965}
1966
Douglas Gregor1b730e82010-05-31 14:40:22 +00001967/// \brief Determine whether the parser is currently referring to a an
1968/// Objective-C message send, using a simplified heuristic to avoid overhead.
1969///
1970/// This routine will only return true for a subset of valid message-send
1971/// expressions.
1972bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001973 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001974 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001975 return GetLookAheadToken(1).is(tok::identifier) &&
1976 GetLookAheadToken(2).is(tok::identifier);
1977}
1978
Douglas Gregor9497a732010-09-16 01:51:54 +00001979bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1980 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1981 InMessageExpression)
1982 return false;
1983
1984
1985 ParsedType Type;
1986
1987 if (Tok.is(tok::annot_typename))
1988 Type = getTypeAnnotation(Tok);
1989 else if (Tok.is(tok::identifier))
1990 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1991 getCurScope());
1992 else
1993 return false;
1994
1995 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1996 const Token &AfterNext = GetLookAheadToken(2);
1997 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1998 if (Tok.is(tok::identifier))
1999 TryAnnotateTypeOrScopeToken();
2000
2001 return Tok.is(tok::annot_typename);
2002 }
2003 }
2004
2005 return false;
2006}
2007
Mike Stump1eb44332009-09-09 15:08:12 +00002008/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002009/// '[' objc-receiver objc-message-args ']'
2010///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002011/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002012/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002013/// expression
2014/// class-name
2015/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002016///
John McCall60d7b3a2010-08-24 06:29:42 +00002017ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002018 assert(Tok.is(tok::l_square) && "'[' expected");
2019 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2020
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002021 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002022 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002023 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002024 return ExprError();
2025 }
2026
Douglas Gregor0fbda682010-09-15 14:51:05 +00002027 InMessageExpressionRAIIObject InMessage(*this, true);
2028
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002029 if (getLang().CPlusPlus) {
2030 // We completely separate the C and C++ cases because C++ requires
2031 // more complicated (read: slower) parsing.
2032
2033 // Handle send to super.
2034 // FIXME: This doesn't benefit from the same typo-correction we
2035 // get in Objective-C.
2036 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002037 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002038 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2039 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002040
2041 // Parse the receiver, which is either a type or an expression.
2042 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002043 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002044 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2045 SkipUntil(tok::r_square);
2046 return ExprError();
2047 }
2048
2049 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002050 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2051 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002052 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002053
2054 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002055 ParsedType::getFromOpaquePtr(TypeOrExpr),
2056 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002057 }
2058
2059 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002060 IdentifierInfo *Name = Tok.getIdentifierInfo();
2061 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002062 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002063 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002064 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002065 NextToken().is(tok::period),
2066 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002067 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002068 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2069 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002070
John McCallf312b1e2010-08-26 23:41:50 +00002071 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002072 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002073 SkipUntil(tok::r_square);
2074 return ExprError();
2075 }
2076
Douglas Gregor1569f952010-04-21 20:38:13 +00002077 ConsumeToken(); // the type name
2078
2079 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002080 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002081
John McCallf312b1e2010-08-26 23:41:50 +00002082 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002083 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002084 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002085 }
Chris Lattner699b6612008-01-25 18:59:06 +00002086 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002087
2088 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002089 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002090 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002091 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002092 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002093 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002094
John McCallb3d87482010-08-24 05:47:05 +00002095 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2096 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002097}
Sebastian Redl1d922962008-12-13 15:32:12 +00002098
Douglas Gregor2725ca82010-04-21 19:57:20 +00002099/// \brief Parse the remainder of an Objective-C message following the
2100/// '[' objc-receiver.
2101///
2102/// This routine handles sends to super, class messages (sent to a
2103/// class name), and instance messages (sent to an object), and the
2104/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2105/// ReceiverExpr, respectively. Only one of these parameters may have
2106/// a valid value.
2107///
2108/// \param LBracLoc The location of the opening '['.
2109///
2110/// \param SuperLoc If this is a send to 'super', the location of the
2111/// 'super' keyword that indicates a send to the superclass.
2112///
2113/// \param ReceiverType If this is a class message, the type of the
2114/// class we are sending a message to.
2115///
2116/// \param ReceiverExpr If this is an instance message, the expression
2117/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002118///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002119/// objc-message-args:
2120/// objc-selector
2121/// objc-keywordarg-list
2122///
2123/// objc-keywordarg-list:
2124/// objc-keywordarg
2125/// objc-keywordarg-list objc-keywordarg
2126///
Mike Stump1eb44332009-09-09 15:08:12 +00002127/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002128/// selector-name[opt] ':' objc-keywordexpr
2129///
2130/// objc-keywordexpr:
2131/// nonempty-expr-list
2132///
2133/// nonempty-expr-list:
2134/// assignment-expression
2135/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002136///
John McCall60d7b3a2010-08-24 06:29:42 +00002137ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002138Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002139 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002140 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002141 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002142 InMessageExpressionRAIIObject InMessage(*this, true);
2143
Steve Naroffc4df6d22009-11-07 02:08:14 +00002144 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002145 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002146 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2147 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002148 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002149 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2150 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002151 else
John McCall9ae2f072010-08-23 23:25:46 +00002152 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002153 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002154 cutOffParsing();
2155 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002156 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002157
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002158 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002159 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002160 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002161
Anders Carlssonff975cf2009-02-14 18:21:46 +00002162 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Chris Lattner5f9e2722011-07-23 10:55:15 +00002164 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002165 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002166
Chris Lattnerdf195262007-10-09 17:51:17 +00002167 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002168 while (1) {
2169 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002170 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002171
Chris Lattnerdf195262007-10-09 17:51:17 +00002172 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002173 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002174 // We must manually skip to a ']', otherwise the expression skipper will
2175 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2176 // the enclosing expression.
2177 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002178 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002179 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002180
Steve Naroff68d331a2007-09-27 14:38:14 +00002181 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002182 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002183
2184 if (Tok.is(tok::code_completion)) {
2185 if (SuperLoc.isValid())
2186 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2187 KeyIdents.data(),
2188 KeyIdents.size(),
2189 /*AtArgumentEpression=*/true);
2190 else if (ReceiverType)
2191 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2192 KeyIdents.data(),
2193 KeyIdents.size(),
2194 /*AtArgumentEpression=*/true);
2195 else
2196 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2197 KeyIdents.data(),
2198 KeyIdents.size(),
2199 /*AtArgumentEpression=*/true);
2200
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002201 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002202 return ExprError();
2203 }
2204
John McCall60d7b3a2010-08-24 06:29:42 +00002205 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002206 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002207 // We must manually skip to a ']', otherwise the expression skipper will
2208 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2209 // the enclosing expression.
2210 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002211 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002212 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002213
Steve Naroff37387c92007-09-17 20:25:27 +00002214 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002215 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002216
Douglas Gregord3c68542009-11-19 01:08:35 +00002217 // Code completion after each argument.
2218 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002219 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002220 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002221 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002222 KeyIdents.size(),
2223 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002224 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002225 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002226 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002227 KeyIdents.size(),
2228 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002229 else
John McCall9ae2f072010-08-23 23:25:46 +00002230 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002231 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002232 KeyIdents.size(),
2233 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002234 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002235 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002236 }
2237
Steve Naroff37387c92007-09-17 20:25:27 +00002238 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002239 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002240 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002241 break;
2242 // We have a selector or a colon, continue parsing.
2243 }
2244 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002245 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002246 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002247 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002248 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002249 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002250 // We must manually skip to a ']', otherwise the expression skipper will
2251 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2252 // the enclosing expression.
2253 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002254 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002255 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002256
Steve Naroff49f109c2007-11-15 13:05:42 +00002257 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002258 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002259 }
2260 } else if (!selIdent) {
2261 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002262
Chris Lattner4fef81d2008-08-05 06:19:09 +00002263 // We must manually skip to a ']', otherwise the expression skipper will
2264 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2265 // the enclosing expression.
2266 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002267 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002268 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002269
Chris Lattnerdf195262007-10-09 17:51:17 +00002270 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002271 if (Tok.is(tok::identifier))
2272 Diag(Tok, diag::err_expected_colon);
2273 else
2274 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002275 // We must manually skip to a ']', otherwise the expression skipper will
2276 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2277 // the enclosing expression.
2278 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002279 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002280 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002281
Chris Lattner699b6612008-01-25 18:59:06 +00002282 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002283
Steve Naroff29238a02007-10-05 18:42:47 +00002284 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002285 if (nKeys == 0)
2286 KeyIdents.push_back(selIdent);
2287 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002288
Douglas Gregor2725ca82010-04-21 19:57:20 +00002289 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002290 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002291 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002292 MultiExprArg(Actions,
2293 KeyExprs.take(),
2294 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002295 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002296 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002297 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002298 MultiExprArg(Actions,
2299 KeyExprs.take(),
2300 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002301 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002302 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002303 MultiExprArg(Actions,
2304 KeyExprs.take(),
2305 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002306}
2307
John McCall60d7b3a2010-08-24 06:29:42 +00002308ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2309 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002310 if (Res.isInvalid()) return move(Res);
2311
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002312 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2313 // expressions. At this point, we know that the only valid thing that starts
2314 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002315 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002316 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002317 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002318 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002319
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002320 while (Tok.is(tok::at)) {
2321 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002322
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002323 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002324 if (!isTokenStringLiteral())
2325 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002326
John McCall60d7b3a2010-08-24 06:29:42 +00002327 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002328 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002329 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002330
Sebastian Redleffa8d12008-12-10 00:02:53 +00002331 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002332 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002333
2334 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2335 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002336}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002337
2338/// objc-encode-expression:
2339/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002340ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002341Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002342 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002343
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002344 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002345
Chris Lattner4fef81d2008-08-05 06:19:09 +00002346 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002347 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2348
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002349 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002350
Douglas Gregor809070a2009-02-18 17:45:20 +00002351 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002352
Anders Carlsson4988ae32007-08-23 15:31:37 +00002353 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002354
Douglas Gregor809070a2009-02-18 17:45:20 +00002355 if (Ty.isInvalid())
2356 return ExprError();
2357
Mike Stump1eb44332009-09-09 15:08:12 +00002358 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002359 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002360}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002361
2362/// objc-protocol-expression
2363/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002364ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002365Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002366 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002367
Chris Lattner4fef81d2008-08-05 06:19:09 +00002368 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002369 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2370
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002371 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002372
Chris Lattner4fef81d2008-08-05 06:19:09 +00002373 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002374 return ExprError(Diag(Tok, diag::err_expected_ident));
2375
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002376 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002377 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002378
Anders Carlsson4988ae32007-08-23 15:31:37 +00002379 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002380
Sebastian Redl1d922962008-12-13 15:32:12 +00002381 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2382 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002383}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002384
2385/// objc-selector-expression
2386/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002387ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002388 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002389
Chris Lattner4fef81d2008-08-05 06:19:09 +00002390 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002391 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2392
Chris Lattner5f9e2722011-07-23 10:55:15 +00002393 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002394 SourceLocation LParenLoc = ConsumeParen();
2395 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002396
2397 if (Tok.is(tok::code_completion)) {
2398 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2399 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002400 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002401 return ExprError();
2402 }
2403
Chris Lattner2fc5c242009-04-11 18:13:45 +00002404 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002405 if (!SelIdent && // missing selector name.
2406 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002407 return ExprError(Diag(Tok, diag::err_expected_ident));
2408
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002409 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002410 unsigned nColons = 0;
2411 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002412 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002413 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2414 ++nColons;
2415 KeyIdents.push_back(0);
2416 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002417 return ExprError(Diag(Tok, diag::err_expected_colon));
2418
Chris Lattner5add7542010-08-27 22:32:41 +00002419 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002420 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002421 if (Tok.is(tok::r_paren))
2422 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002423
2424 if (Tok.is(tok::code_completion)) {
2425 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2426 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002427 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002428 return ExprError();
2429 }
2430
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002431 // Check for another keyword selector.
2432 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002433 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002434 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002435 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002436 break;
2437 }
Steve Naroff887407e2007-12-05 22:21:29 +00002438 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002439 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002440 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002441 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2442 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002443 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002444
2445Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2446
2447 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2448 // Append the current token at the end of the new token stream so that it
2449 // doesn't get lost.
2450 LM.Toks.push_back(Tok);
2451 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2452
2453 // MDecl might be null due to error in method prototype, etc.
2454 Decl *MDecl = LM.D;
2455 // Consume the previously pushed token.
2456 ConsumeAnyToken();
2457
2458 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2459 SourceLocation BraceLoc = Tok.getLocation();
2460 // Enter a scope for the method body.
2461 ParseScope BodyScope(this,
2462 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2463
2464 // Tell the actions module that we have entered a method definition with the
2465 // specified Declarator for the method.
2466 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2467
2468 if (PP.isCodeCompletionEnabled()) {
2469 if (trySkippingFunctionBodyForCodeCompletion()) {
2470 BodyScope.Exit();
2471 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2472 }
2473 }
2474
2475 StmtResult FnBody(ParseCompoundStatementBody());
2476
2477 // If the function body could not be parsed, make a bogus compoundstmt.
2478 if (FnBody.isInvalid())
2479 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2480 MultiStmtArg(Actions), false);
2481
2482 // Leave the function body scope.
2483 BodyScope.Exit();
2484
2485 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2486}