blob: 44808c8922443476302f60209e15b8264274f8de [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000039 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000045 break;
John McCall7f040a92010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall7f040a92010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000053 SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
54 break;
John McCall7f040a92010-12-24 02:08:15 +000055 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000056 case tok::objc_implementation:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000057 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
58 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000059 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000060 return ParseObjCAtEndDeclaration(AtLoc);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000062 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
64 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000065 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
67 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000068 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000069 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
70 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000071 default:
72 Diag(AtLoc, diag::err_unexpected_at);
73 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000074 SingleDecl = 0;
75 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000077 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000078}
79
80///
Mike Stump1eb44332009-09-09 15:08:12 +000081/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000084Parser::DeclGroupPtrTy
85Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 SmallVector<IdentifierInfo *, 8> ClassNames;
88 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000089
Mike Stump1eb44332009-09-09 15:08:12 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000092 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000093 Diag(Tok, diag::err_expected_ident);
94 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
Reid Spencer5f016e22007-07-11 17:01:13 +000097 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000098 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattnerdf195262007-10-09 17:51:17 +0000101 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 ConsumeToken();
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // Consume the ';'.
108 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000109 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekc09cba62009-11-17 23:12:20 +0000111 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
112 ClassLocs.data(),
113 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
Steve Naroffdac269b2007-08-20 21:31:48 +0000116///
117/// objc-interface:
118/// objc-class-interface-attributes[opt] objc-class-interface
119/// objc-category-interface
120///
121/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000122/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000123/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000124/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000125/// objc-interface-decl-list
126/// @end
127///
128/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000129/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000130/// objc-protocol-refs[opt]
131/// objc-interface-decl-list
132/// @end
133///
134/// objc-superclass:
135/// ':' identifier
136///
137/// objc-class-interface-attributes:
138/// __attribute__((visibility("default")))
139/// __attribute__((visibility("hidden")))
140/// __attribute__((deprecated))
141/// __attribute__((unavailable))
142/// __attribute__((objc_exception)) - used by NSException on 64-bit
143///
John McCall7f040a92010-12-24 02:08:15 +0000144Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
145 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000146 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
148 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000150 // Code completion after '@interface'.
151 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000152 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000153 cutOffParsing();
154 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000155 }
156
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000161
Steve Naroffdac269b2007-08-20 21:31:48 +0000162 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000163 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000165 if (Tok.is(tok::l_paren) &&
166 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000167 SourceLocation LParenLoc = ConsumeParen();
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 SourceLocation categoryLoc, rparenLoc;
169 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000170 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000172 cutOffParsing();
173 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000174 }
175
Steve Naroff527fe232007-08-23 19:56:30 +0000176 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 categoryId = Tok.getIdentifierInfo();
179 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000180 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000181 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000182 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000183 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000185
186 rparenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
187 if (rparenLoc.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000189
190 if (!attrs.empty()) { // categories don't support attributes.
191 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
192 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000193 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000194
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000195 // Next, we need to check for any protocol references.
196 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 SmallVector<Decl *, 8> ProtocolRefs;
198 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000199 if (Tok.is(tok::less) &&
200 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000201 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000202 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
John McCalld226f652010-08-21 09:40:31 +0000204 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000205 Actions.ActOnStartCategoryInterface(atLoc,
206 nameId, nameLoc,
207 categoryId, categoryLoc,
208 ProtocolRefs.data(),
209 ProtocolRefs.size(),
210 ProtocolLocs.data(),
211 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000212
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000213 if (Tok.is(tok::l_brace))
214 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
215
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000216 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000217 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000218 }
219 // Parse a class interface.
220 IdentifierInfo *superClassId = 0;
221 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000222
Chris Lattnerdf195262007-10-09 17:51:17 +0000223 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000224 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000225
226 // Code completion of superclass names.
227 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000228 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000229 cutOffParsing();
230 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000231 }
232
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000234 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000235 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000236 }
237 superClassId = Tok.getIdentifierInfo();
238 superClassLoc = ConsumeToken();
239 }
240 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 SmallVector<Decl *, 8> ProtocolRefs;
242 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000243 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000244 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000245 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
246 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000247 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
John McCalld226f652010-08-21 09:40:31 +0000249 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000250 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000251 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000252 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000253 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000254 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattnerdf195262007-10-09 17:51:17 +0000256 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000257 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000258
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000259 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000260 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000261}
262
John McCalld0014542009-12-03 22:31:13 +0000263/// The Objective-C property callback. This should be defined where
264/// it's used, but instead it's been lifted to here to support VS2005.
265struct Parser::ObjCPropertyCallback : FieldCallback {
266 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000268 ObjCDeclSpec &OCDS;
269 SourceLocation AtLoc;
270 tok::ObjCKeywordKind MethodImplKind;
271
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000272 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000274 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
275 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000276 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000277 MethodImplKind(MethodImplKind) {
278 }
279
John McCalld226f652010-08-21 09:40:31 +0000280 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000281 if (FD.D.getIdentifier() == 0) {
282 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
283 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000284 return 0;
John McCalld0014542009-12-03 22:31:13 +0000285 }
286 if (FD.BitfieldSize) {
287 P.Diag(AtLoc, diag::err_objc_property_bitfield)
288 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000289 return 0;
John McCalld0014542009-12-03 22:31:13 +0000290 }
291
292 // Install the property declarator into interfaceDecl.
293 IdentifierInfo *SelName =
294 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
295
296 Selector GetterSel =
297 P.PP.getSelectorTable().getNullarySelector(SelName);
298 IdentifierInfo *SetterName = OCDS.getSetterName();
299 Selector SetterSel;
300 if (SetterName)
301 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
302 else
303 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
304 P.PP.getSelectorTable(),
305 FD.D.getIdentifier());
306 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000307 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000308 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000309 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000310 &isOverridingProperty,
311 MethodImplKind);
312 if (!isOverridingProperty)
313 Props.push_back(Property);
314
315 return Property;
316 }
317};
318
Steve Naroffdac269b2007-08-20 21:31:48 +0000319/// objc-interface-decl-list:
320/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000321/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000322/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000323/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000324/// objc-interface-decl-list declaration
325/// objc-interface-decl-list ';'
326///
Steve Naroff294494e2007-08-22 16:35:03 +0000327/// objc-method-requirement: [OBJC2]
328/// @required
329/// @optional
330///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000331void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
332 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000333 SmallVector<Decl *, 32> allMethods;
334 SmallVector<Decl *, 16> allProperties;
335 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000336 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek782f2f52010-01-07 01:20:12 +0000338 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000339 Actions.ActOnObjCContainerStartDefinition(CDecl);
340
Steve Naroff294494e2007-08-22 16:35:03 +0000341 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000342 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000343 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000344 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000345 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000346 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000347 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
348 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000349 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
350 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000351 continue;
352 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000353 if (Tok.is(tok::l_paren)) {
354 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000355 ParseObjCMethodDecl(Tok.getLocation(),
356 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000357 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000358 continue;
359 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000360 // Ignore excess semicolons.
361 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000362 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000363 continue;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattnerbc662af2008-10-20 06:10:06 +0000366 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000367 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000368 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000370 // Code completion within an Objective-C interface.
371 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000372 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000373 ObjCImpDecl? Sema::PCC_ObjCImplementation
374 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000375 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000376 }
377
Chris Lattnere82a10f2008-10-20 05:46:22 +0000378 // If we don't have an @ directive, parse it as a function definition.
379 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000380 // The code below does not consume '}'s because it is afraid of eating the
381 // end of a namespace. Because of the way this code is structured, an
382 // erroneous r_brace would cause an infinite loop if not handled here.
383 if (Tok.is(tok::r_brace))
384 break;
John McCall0b7e6782011-03-24 11:26:52 +0000385 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000386 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000387 continue;
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnere82a10f2008-10-20 05:46:22 +0000390 // Otherwise, we have an @ directive, eat the @.
391 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000392 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000393 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000394 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000395 break;
396 }
397
Chris Lattnera2449b22008-10-20 05:57:40 +0000398 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnera2449b22008-10-20 05:57:40 +0000400 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000401 AtEnd.setBegin(AtLoc);
402 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000403 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000404 } else if (DirectiveKind == tok::objc_not_keyword) {
405 Diag(Tok, diag::err_objc_unknown_at);
406 SkipUntil(tok::semi);
407 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnerbc662af2008-10-20 06:10:06 +0000410 // Eat the identifier.
411 ConsumeToken();
412
Chris Lattnera2449b22008-10-20 05:57:40 +0000413 switch (DirectiveKind) {
414 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000415 // FIXME: If someone forgets an @end on a protocol, this loop will
416 // continue to eat up tons of stuff and spew lots of nonsense errors. It
417 // would probably be better to bail out if we saw an @class or @interface
418 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000419 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000420 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000421 SkipUntil(tok::r_brace, tok::at);
422 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000423
424 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000425 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000426 Diag(Tok, diag::err_objc_missing_end);
427 ConsumeToken();
428 break;
429
Chris Lattnera2449b22008-10-20 05:57:40 +0000430 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000431 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000433 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000434 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000435 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000436 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000437 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000438 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattnera2449b22008-10-20 05:57:40 +0000440 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000441 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000442 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000443
Chris Lattnere82a10f2008-10-20 05:46:22 +0000444 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000445 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000446 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000447 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000449 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000450 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000451
Chris Lattnere82a10f2008-10-20 05:46:22 +0000452 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000453 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000454 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
John McCall7da19ea2011-03-26 01:53:26 +0000456 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000457 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000458 }
Steve Naroff294494e2007-08-22 16:35:03 +0000459 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000460
461 // We break out of the big loop in two cases: when we see @end or when we see
462 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000463 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000464 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000465 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000466 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000467 ConsumeToken(); // the "end" identifier
468 else
469 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000472 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000473 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000474 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000475 allProperties.data(), allProperties.size(),
476 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000477}
478
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000479/// Parse property attribute declarations.
480///
481/// property-attr-decl: '(' property-attrlist ')'
482/// property-attrlist:
483/// property-attribute
484/// property-attrlist ',' property-attribute
485/// property-attribute:
486/// getter '=' identifier
487/// setter '=' identifier ':'
488/// readonly
489/// readwrite
490/// assign
491/// retain
492/// copy
493/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000494/// atomic
495/// strong
496/// weak
497/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000498///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000499void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000500 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000501 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000503 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000504 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000505 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000506 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000507 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000508 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000510 // If this is not an identifier at all, bail out early.
511 if (II == 0) {
512 MatchRHSPunctuation(tok::r_paren, LHSLoc);
513 return;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner156b0612008-10-20 07:37:22 +0000516 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner92e62b02008-11-20 04:42:34 +0000518 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000519 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000520 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000521 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000522 else if (II->isStr("unsafe_unretained"))
523 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000524 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000525 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000526 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000527 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000528 else if (II->isStr("strong"))
529 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000530 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000531 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000532 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000533 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000534 else if (II->isStr("atomic"))
535 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000536 else if (II->isStr("weak"))
537 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000538 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000539 bool IsSetter = II->getNameStart()[0] == 's';
540
Chris Lattnere00da7c2008-10-20 07:39:53 +0000541 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000542 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
543 diag::err_objc_expected_equal_for_getter;
544
545 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000546 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor4ad96852009-11-19 07:41:15 +0000548 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000549 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000550 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000551 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000552 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000553 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000554 }
555
Anders Carlsson42499be2010-10-02 17:45:21 +0000556
557 SourceLocation SelLoc;
558 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
559
560 if (!SelIdent) {
561 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
562 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000563 SkipUntil(tok::r_paren);
564 return;
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Anders Carlsson42499be2010-10-02 17:45:21 +0000567 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000568 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000569 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000571 if (ExpectAndConsume(tok::colon,
572 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000573 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000574 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000575 } else {
576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000577 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000578 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000579 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000580 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000581 SkipUntil(tok::r_paren);
582 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner156b0612008-10-20 07:37:22 +0000585 if (Tok.isNot(tok::comma))
586 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner156b0612008-10-20 07:37:22 +0000588 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner156b0612008-10-20 07:37:22 +0000591 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000592}
593
Steve Naroff3536b442007-09-06 21:24:23 +0000594/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000595/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000596/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000597///
598/// objc-instance-method: '-'
599/// objc-class-method: '+'
600///
Steve Naroff4985ace2007-08-22 18:35:33 +0000601/// objc-method-attributes: [OBJC2]
602/// __attribute__((deprecated))
603///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000604Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000605 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000606 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000607
Mike Stump1eb44332009-09-09 15:08:12 +0000608 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000609 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000610 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000611 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000612 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000613 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000614 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000615}
616
617/// objc-selector:
618/// identifier
619/// one of
620/// enum struct union if else while do for switch case default
621/// break continue return goto asm sizeof typeof __alignof
622/// unsigned long const short volatile signed restrict _Complex
623/// in out inout bycopy byref oneway int char float double void _Bool
624///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000625IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000626
Chris Lattnerff384912007-10-07 02:00:24 +0000627 switch (Tok.getKind()) {
628 default:
629 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000630 case tok::ampamp:
631 case tok::ampequal:
632 case tok::amp:
633 case tok::pipe:
634 case tok::tilde:
635 case tok::exclaim:
636 case tok::exclaimequal:
637 case tok::pipepipe:
638 case tok::pipeequal:
639 case tok::caret:
640 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000641 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000642 if (isalpha(ThisTok[0])) {
643 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
644 Tok.setKind(tok::identifier);
645 SelectorLoc = ConsumeToken();
646 return II;
647 }
648 return 0;
649 }
650
Chris Lattnerff384912007-10-07 02:00:24 +0000651 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000652 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000653 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000654 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000655 case tok::kw_break:
656 case tok::kw_case:
657 case tok::kw_catch:
658 case tok::kw_char:
659 case tok::kw_class:
660 case tok::kw_const:
661 case tok::kw_const_cast:
662 case tok::kw_continue:
663 case tok::kw_default:
664 case tok::kw_delete:
665 case tok::kw_do:
666 case tok::kw_double:
667 case tok::kw_dynamic_cast:
668 case tok::kw_else:
669 case tok::kw_enum:
670 case tok::kw_explicit:
671 case tok::kw_export:
672 case tok::kw_extern:
673 case tok::kw_false:
674 case tok::kw_float:
675 case tok::kw_for:
676 case tok::kw_friend:
677 case tok::kw_goto:
678 case tok::kw_if:
679 case tok::kw_inline:
680 case tok::kw_int:
681 case tok::kw_long:
682 case tok::kw_mutable:
683 case tok::kw_namespace:
684 case tok::kw_new:
685 case tok::kw_operator:
686 case tok::kw_private:
687 case tok::kw_protected:
688 case tok::kw_public:
689 case tok::kw_register:
690 case tok::kw_reinterpret_cast:
691 case tok::kw_restrict:
692 case tok::kw_return:
693 case tok::kw_short:
694 case tok::kw_signed:
695 case tok::kw_sizeof:
696 case tok::kw_static:
697 case tok::kw_static_cast:
698 case tok::kw_struct:
699 case tok::kw_switch:
700 case tok::kw_template:
701 case tok::kw_this:
702 case tok::kw_throw:
703 case tok::kw_true:
704 case tok::kw_try:
705 case tok::kw_typedef:
706 case tok::kw_typeid:
707 case tok::kw_typename:
708 case tok::kw_typeof:
709 case tok::kw_union:
710 case tok::kw_unsigned:
711 case tok::kw_using:
712 case tok::kw_virtual:
713 case tok::kw_void:
714 case tok::kw_volatile:
715 case tok::kw_wchar_t:
716 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000717 case tok::kw__Bool:
718 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000719 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000720 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000721 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000722 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000723 }
Steve Naroff294494e2007-08-22 16:35:03 +0000724}
725
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000726/// objc-for-collection-in: 'in'
727///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000728bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000729 // FIXME: May have to do additional look-ahead to only allow for
730 // valid tokens following an 'in'; such as an identifier, unary operators,
731 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000732 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000733 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000734}
735
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000737/// qualifier list and builds their bitmask representation in the input
738/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000739///
740/// objc-type-qualifiers:
741/// objc-type-qualifier
742/// objc-type-qualifiers objc-type-qualifier
743///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000744void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000745 Declarator::TheContext Context) {
746 assert(Context == Declarator::ObjCParameterContext ||
747 Context == Declarator::ObjCResultContext);
748
Chris Lattnere8b724d2007-12-12 06:56:32 +0000749 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000750 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000751 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000752 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000753 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000754 }
755
Chris Lattnercb53b362007-12-27 19:57:00 +0000756 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000757 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattnere8b724d2007-12-12 06:56:32 +0000759 const IdentifierInfo *II = Tok.getIdentifierInfo();
760 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000762 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000765 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000766 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
768 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
769 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
770 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
771 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
772 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000773 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000774 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000775 ConsumeToken();
776 II = 0;
777 break;
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattnere8b724d2007-12-12 06:56:32 +0000780 // If this wasn't a recognized qualifier, bail out.
781 if (II) return;
782 }
783}
784
John McCallcdda47f2011-10-01 09:56:14 +0000785/// Take all the decl attributes out of the given list and add
786/// them to the given attribute set.
787static void takeDeclAttributes(ParsedAttributes &attrs,
788 AttributeList *list) {
789 while (list) {
790 AttributeList *cur = list;
791 list = cur->getNext();
792
793 if (!cur->isUsedAsTypeAttr()) {
794 // Clear out the next pointer. We're really completely
795 // destroying the internal invariants of the declarator here,
796 // but it doesn't matter because we're done with it.
797 cur->setNext(0);
798 attrs.add(cur);
799 }
800 }
801}
802
803/// takeDeclAttributes - Take all the decl attributes from the given
804/// declarator and add them to the given list.
805static void takeDeclAttributes(ParsedAttributes &attrs,
806 Declarator &D) {
807 // First, take ownership of all attributes.
808 attrs.getPool().takeAllFrom(D.getAttributePool());
809 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
810
811 // Now actually move the attributes over.
812 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
813 takeDeclAttributes(attrs, D.getAttributes());
814 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
815 takeDeclAttributes(attrs,
816 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
817}
818
Chris Lattnere8b724d2007-12-12 06:56:32 +0000819/// objc-type-name:
820/// '(' objc-type-qualifiers[opt] type-name ')'
821/// '(' objc-type-qualifiers[opt] ')'
822///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000823ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000824 Declarator::TheContext context,
825 ParsedAttributes *paramAttrs) {
826 assert(context == Declarator::ObjCParameterContext ||
827 context == Declarator::ObjCResultContext);
828 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
829
Chris Lattnerdf195262007-10-09 17:51:17 +0000830 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattner4a76b292008-10-22 03:52:06 +0000832 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000833 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000834 ObjCDeclContextSwitch ObjCDC(*this);
835
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000836 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000837 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000838
John McCallb3d87482010-08-24 05:47:05 +0000839 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000840 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000841 // Parse an abstract declarator.
842 DeclSpec declSpec(AttrFactory);
843 declSpec.setObjCQualifiers(&DS);
844 ParseSpecifierQualifierList(declSpec);
845 Declarator declarator(declSpec, context);
846 ParseDeclarator(declarator);
847
848 // If that's not invalid, extract a type.
849 if (!declarator.isInvalidType()) {
850 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
851 if (!type.isInvalid())
852 Ty = type.get();
853
854 // If we're parsing a parameter, steal all the decl attributes
855 // and add them to the decl spec.
856 if (context == Declarator::ObjCParameterContext)
857 takeDeclAttributes(*paramAttrs, declarator);
858 }
859 } else if (context == Declarator::ObjCResultContext &&
860 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000861 if (!Ident_instancetype)
862 Ident_instancetype = PP.getIdentifierInfo("instancetype");
863
864 if (Tok.getIdentifierInfo() == Ident_instancetype) {
865 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
866 ConsumeToken();
867 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000868 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000869
Steve Naroffd7333c22008-10-21 14:15:04 +0000870 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000871 ConsumeParen();
872 else if (Tok.getLocation() == TypeStartLoc) {
873 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000874 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000875 SkipUntil(tok::r_paren);
876 } else {
877 // Otherwise, we found *something*, but didn't get a ')' in the right
878 // place. Emit an error then return what we have as the type.
879 MatchRHSPunctuation(tok::r_paren, LParenLoc);
880 }
Steve Narofff28b2642007-09-05 23:30:30 +0000881 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000882}
883
884/// objc-method-decl:
885/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000886/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000887/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000888/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000889///
890/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000891/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000892/// objc-keyword-selector objc-keyword-decl
893///
894/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000895/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
896/// objc-selector ':' objc-keyword-attributes[opt] identifier
897/// ':' objc-type-name objc-keyword-attributes[opt] identifier
898/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000899///
Steve Naroff4985ace2007-08-22 18:35:33 +0000900/// objc-parmlist:
901/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000902///
Steve Naroff4985ace2007-08-22 18:35:33 +0000903/// objc-parms:
904/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000905///
Steve Naroff4985ace2007-08-22 18:35:33 +0000906/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000907/// , ...
908///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000909/// objc-keyword-attributes: [OBJC2]
910/// __attribute__((unused))
911///
John McCalld226f652010-08-21 09:40:31 +0000912Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000913 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000914 tok::ObjCKeywordKind MethodImplKind,
915 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000916 ParsingDeclRAIIObject PD(*this);
917
Douglas Gregore8f5a172010-04-07 00:21:17 +0000918 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000919 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000920 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000921 cutOffParsing();
922 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000923 }
924
Chris Lattnere8904e92008-08-23 01:48:03 +0000925 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000926 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000929 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek9e049352010-02-18 23:05:16 +0000931 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000932 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000933 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000934 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000935
Douglas Gregore8f5a172010-04-07 00:21:17 +0000936 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000937 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000938 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000939 cutOffParsing();
940 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000941 }
942
Ted Kremenek9e049352010-02-18 23:05:16 +0000943 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000944 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000945 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000946
Steve Naroff84c43102009-02-11 20:43:13 +0000947 // An unnamed colon is valid.
948 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000949 Diag(Tok, diag::err_expected_selector_for_method)
950 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000951 // Skip until we get a ; or {}.
952 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000953 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattner5f9e2722011-07-23 10:55:15 +0000956 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000957 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000958 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +0000959 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000960 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattnerff384912007-10-07 02:00:24 +0000962 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000963 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000964 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000965 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000966 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000967 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +0000968 methodAttrs.getList(), MethodImplKind,
969 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +0000970 PD.complete(Result);
971 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000972 }
Steve Narofff28b2642007-09-05 23:30:30 +0000973
Chris Lattner5f9e2722011-07-23 10:55:15 +0000974 SmallVector<IdentifierInfo *, 12> KeyIdents;
975 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000976 ParseScope PrototypeScope(this,
977 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +0000978
979 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +0000980
Chris Lattnerff384912007-10-07 02:00:24 +0000981 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +0000982 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +0000983 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattnerff384912007-10-07 02:00:24 +0000985 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000986 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000987 Diag(Tok, diag::err_expected_colon);
988 break;
989 }
990 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000991
John McCallb3d87482010-08-24 05:47:05 +0000992 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000993 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +0000994 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
995 Declarator::ObjCParameterContext,
996 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000997
Chris Lattnerff384912007-10-07 02:00:24 +0000998 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +0000999 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001000 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001001 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001002 MaybeParseGNUAttributes(paramAttrs);
1003 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001004 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001005
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001006 // Code completion for the next piece of the selector.
1007 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001008 KeyIdents.push_back(SelIdent);
1009 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1010 mType == tok::minus,
1011 /*AtParameterName=*/true,
1012 ReturnType,
1013 KeyIdents.data(),
1014 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001015 cutOffParsing();
1016 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001017 }
1018
Chris Lattnerdf195262007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001020 Diag(Tok, diag::err_expected_ident); // missing argument name.
1021 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Chris Lattnere294d3f2009-04-11 18:57:04 +00001024 ArgInfo.Name = Tok.getIdentifierInfo();
1025 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001026 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattnere294d3f2009-04-11 18:57:04 +00001028 ArgInfos.push_back(ArgInfo);
1029 KeyIdents.push_back(SelIdent);
1030
John McCall0b7e6782011-03-24 11:26:52 +00001031 // Make sure the attributes persist.
1032 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1033
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001034 // Code completion for the next piece of the selector.
1035 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001036 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1037 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001038 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001039 ReturnType,
1040 KeyIdents.data(),
1041 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001042 cutOffParsing();
1043 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001044 }
1045
Chris Lattnerff384912007-10-07 02:00:24 +00001046 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001047 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001048 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001049 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001050 break;
1051 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Steve Naroff335eafa2007-11-15 12:35:21 +00001054 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattnerff384912007-10-07 02:00:24 +00001056 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001058 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001059 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001060 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001061 ConsumeToken();
1062 break;
1063 }
John McCall0b7e6782011-03-24 11:26:52 +00001064 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001065 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001066 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001067 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1068 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001069 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001070 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001071 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1072 ParmDecl.getIdentifierLoc(),
1073 Param,
1074 0));
1075
Chris Lattnerff384912007-10-07 02:00:24 +00001076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001078 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001079 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001080 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001081 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001082
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001083 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001084 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001085
Chris Lattnerff384912007-10-07 02:00:24 +00001086 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1087 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001088 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001089 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001090 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001091 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001092 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001093 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001094 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001095
John McCall54abf7d2009-11-04 02:18:39 +00001096 PD.complete(Result);
1097 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001098}
1099
Steve Naroffdac269b2007-08-20 21:31:48 +00001100/// objc-protocol-refs:
1101/// '<' identifier-list '>'
1102///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001103bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001104ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1105 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001106 bool WarnOnDeclarations,
1107 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001108 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001110 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner5f9e2722011-07-23 10:55:15 +00001112 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Chris Lattnere13b9592008-07-26 04:03:38 +00001114 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001115 if (Tok.is(tok::code_completion)) {
1116 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1117 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001118 cutOffParsing();
1119 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001120 }
1121
Chris Lattnere13b9592008-07-26 04:03:38 +00001122 if (Tok.isNot(tok::identifier)) {
1123 Diag(Tok, diag::err_expected_ident);
1124 SkipUntil(tok::greater);
1125 return true;
1126 }
1127 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1128 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001129 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001130 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattnere13b9592008-07-26 04:03:38 +00001132 if (Tok.isNot(tok::comma))
1133 break;
1134 ConsumeToken();
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Chris Lattnere13b9592008-07-26 04:03:38 +00001137 // Consume the '>'.
1138 if (Tok.isNot(tok::greater)) {
1139 Diag(Tok, diag::err_expected_greater);
1140 return true;
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattnere13b9592008-07-26 04:03:38 +00001143 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattnere13b9592008-07-26 04:03:38 +00001145 // Convert the list of protocols identifiers into a list of protocol decls.
1146 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1147 &ProtocolIdents[0], ProtocolIdents.size(),
1148 Protocols);
1149 return false;
1150}
1151
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001152/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1153/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001154bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001155 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1156 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1157 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158 SmallVector<Decl *, 8> ProtocolDecl;
1159 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001160 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1161 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001162 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1163 ProtocolLocs.data(), LAngleLoc);
1164 if (EndProtoLoc.isValid())
1165 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001166 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001167}
1168
1169
Steve Naroffdac269b2007-08-20 21:31:48 +00001170/// objc-class-instance-variables:
1171/// '{' objc-instance-variable-decl-list[opt] '}'
1172///
1173/// objc-instance-variable-decl-list:
1174/// objc-visibility-spec
1175/// objc-instance-variable-decl ';'
1176/// ';'
1177/// objc-instance-variable-decl-list objc-visibility-spec
1178/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1179/// objc-instance-variable-decl-list ';'
1180///
1181/// objc-visibility-spec:
1182/// @private
1183/// @protected
1184/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001185/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001186///
1187/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001188/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001189///
John McCalld226f652010-08-21 09:40:31 +00001190void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001191 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001192 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001193 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001194 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001195
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001196 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001197
Steve Naroffddbff782007-08-21 21:17:12 +00001198 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Steve Naroffddbff782007-08-21 21:17:12 +00001200 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001201 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001202 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Steve Naroffddbff782007-08-21 21:17:12 +00001204 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001205 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001206 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001207 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001208 ConsumeToken();
1209 continue;
1210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Steve Naroffddbff782007-08-21 21:17:12 +00001212 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001213 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001214 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001215
1216 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001217 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001218 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001219 }
1220
Steve Naroff861cf3e2007-08-23 18:16:40 +00001221 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001222 case tok::objc_private:
1223 case tok::objc_public:
1224 case tok::objc_protected:
1225 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001226 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001227 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001228 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001229 default:
1230 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001231 continue;
1232 }
1233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001235 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001236 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001237 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001238 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001239 }
1240
John McCallbdd563e2009-11-03 02:38:08 +00001241 struct ObjCIvarCallback : FieldCallback {
1242 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001243 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001244 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001245 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001246
John McCalld226f652010-08-21 09:40:31 +00001247 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001248 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001249 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1250 }
1251
John McCalld226f652010-08-21 09:40:31 +00001252 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001253 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001254 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001255 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001256 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001257 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001258 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001259 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001260 if (Field)
1261 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001262 return Field;
1263 }
1264 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001265
Chris Lattnere1359422008-04-10 06:46:29 +00001266 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001267 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001268 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Chris Lattnerdf195262007-10-09 17:51:17 +00001270 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001271 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001272 } else {
1273 Diag(Tok, diag::err_expected_semi_decl_list);
1274 // Skip to end of block or statement
1275 SkipUntil(tok::r_brace, true, true);
1276 }
1277 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001278 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001279 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1280 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001281 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001282 // Call ActOnFields() even if we don't have any decls. This is useful
1283 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001285 AllIvarDecls,
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001286 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001287 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288}
Steve Naroffdac269b2007-08-20 21:31:48 +00001289
1290/// objc-protocol-declaration:
1291/// objc-protocol-definition
1292/// objc-protocol-forward-reference
1293///
1294/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001295/// @protocol identifier
1296/// objc-protocol-refs[opt]
1297/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001298/// @end
1299///
1300/// objc-protocol-forward-reference:
1301/// @protocol identifier-list ';'
1302///
1303/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001304/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001305/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001306Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +00001307 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001308 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001309 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1310 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Douglas Gregor083128f2009-11-18 04:49:41 +00001312 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001313 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001314 cutOffParsing();
1315 return 0;
Douglas Gregor083128f2009-11-18 04:49:41 +00001316 }
1317
Chris Lattnerdf195262007-10-09 17:51:17 +00001318 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001319 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001320 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001321 }
1322 // Save the protocol name, then consume it.
1323 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1324 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Chris Lattnerdf195262007-10-09 17:51:17 +00001326 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001327 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001328 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001329 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001330 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001331 }
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chris Lattnerdf195262007-10-09 17:51:17 +00001333 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001334 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001335 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1336
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001337 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001338 while (1) {
1339 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001340 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001341 Diag(Tok, diag::err_expected_ident);
1342 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001343 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001344 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001345 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1346 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001347 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Chris Lattnerdf195262007-10-09 17:51:17 +00001349 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001350 break;
1351 }
1352 // Consume the ';'.
1353 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001354 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Steve Naroffe440eb82007-10-10 17:32:04 +00001356 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001357 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001358 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001359 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001360 }
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001362 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001363 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001364
Chris Lattner5f9e2722011-07-23 10:55:15 +00001365 SmallVector<Decl *, 8> ProtocolRefs;
1366 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001367 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001368 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1369 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001370 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
John McCalld226f652010-08-21 09:40:31 +00001372 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001373 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001374 ProtocolRefs.data(),
1375 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001376 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001377 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001378
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001379 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001380 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001381}
Steve Naroffdac269b2007-08-20 21:31:48 +00001382
1383/// objc-implementation:
1384/// objc-class-implementation-prologue
1385/// objc-category-implementation-prologue
1386///
1387/// objc-class-implementation-prologue:
1388/// @implementation identifier objc-superclass[opt]
1389/// objc-class-instance-variables[opt]
1390///
1391/// objc-category-implementation-prologue:
1392/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001393Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001394 SourceLocation atLoc) {
1395 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1396 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1397 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001399 // Code completion after '@implementation'.
1400 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001401 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001402 cutOffParsing();
1403 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001404 }
1405
Chris Lattnerdf195262007-10-09 17:51:17 +00001406 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001407 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001408 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001409 }
1410 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001411 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001412 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001413
1414 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001415 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001416 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001417 SourceLocation categoryLoc, rparenLoc;
1418 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001420 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001421 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001422 cutOffParsing();
1423 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001424 }
1425
Chris Lattnerdf195262007-10-09 17:51:17 +00001426 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001427 categoryId = Tok.getIdentifierInfo();
1428 categoryLoc = ConsumeToken();
1429 } else {
1430 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001431 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001432 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001433 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001434 Diag(Tok, diag::err_expected_rparen);
1435 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001436 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001437 }
1438 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001439 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001440 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001441 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001442
1443 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001445 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001446 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001447 }
1448 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001449 SourceLocation superClassLoc;
1450 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001451 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001452 // We have a super class
1453 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001454 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001455 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001456 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001457 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001458 superClassId = Tok.getIdentifierInfo();
1459 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 }
John McCalld226f652010-08-21 09:40:31 +00001461 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001462 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001463 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Steve Naroff60fccee2007-10-29 21:38:07 +00001465 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001466 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1467
1468 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001470 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001471 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001472}
Steve Naroff60fccee2007-10-29 21:38:07 +00001473
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001474Parser::DeclGroupPtrTy
1475Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001476 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1477 "ParseObjCAtEndDeclaration(): Expected @end");
1478 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001479 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001480 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001481 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1482 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1483 DeclsInGroup.push_back(D);
1484 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001485 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001486
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001487 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001488 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001489 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001490 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001491 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001492 // missing @implementation
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00001493 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001494
1495 LateParsedObjCMethods.clear();
1496 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001497 return Actions.BuildDeclaratorGroup(
1498 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001499}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001500
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001501Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1502 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001503 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001504 return Actions.ConvertDeclToDeclGroup(0);
1505 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001506 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001507 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1508}
1509
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001510/// compatibility-alias-decl:
1511/// @compatibility_alias alias-name class-name ';'
1512///
John McCalld226f652010-08-21 09:40:31 +00001513Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001514 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1515 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1516 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001517 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001518 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001519 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001520 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001521 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1522 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001523 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001524 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001525 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001526 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001527 IdentifierInfo *classId = Tok.getIdentifierInfo();
1528 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001529 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1530 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001531 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1532 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001533}
1534
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001535/// property-synthesis:
1536/// @synthesize property-ivar-list ';'
1537///
1538/// property-ivar-list:
1539/// property-ivar
1540/// property-ivar-list ',' property-ivar
1541///
1542/// property-ivar:
1543/// identifier
1544/// identifier '=' identifier
1545///
John McCalld226f652010-08-21 09:40:31 +00001546Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001547 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1548 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001549 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Douglas Gregorb328c422009-11-18 19:45:45 +00001551 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001552 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001553 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001554 cutOffParsing();
1555 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001556 }
1557
Douglas Gregorb328c422009-11-18 19:45:45 +00001558 if (Tok.isNot(tok::identifier)) {
1559 Diag(Tok, diag::err_synthesized_property_name);
1560 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001561 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001562 }
1563
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001564 IdentifierInfo *propertyIvar = 0;
1565 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1566 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001567 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001568 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001569 // property '=' ivar-name
1570 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001571
1572 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001573 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001574 cutOffParsing();
1575 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001576 }
1577
Chris Lattnerdf195262007-10-09 17:51:17 +00001578 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001579 Diag(Tok, diag::err_expected_ident);
1580 break;
1581 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001582 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001583 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001584 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001585 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001586 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001587 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001588 break;
1589 ConsumeToken(); // consume ','
1590 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001591 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001592 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001593}
1594
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001595/// property-dynamic:
1596/// @dynamic property-list
1597///
1598/// property-list:
1599/// identifier
1600/// property-list ',' identifier
1601///
John McCalld226f652010-08-21 09:40:31 +00001602Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001603 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1604 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001605 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001606 while (true) {
1607 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001608 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001609 cutOffParsing();
1610 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001611 }
1612
1613 if (Tok.isNot(tok::identifier)) {
1614 Diag(Tok, diag::err_expected_ident);
1615 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001616 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001617 }
1618
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001619 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1620 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001621 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001622 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001623
Chris Lattnerdf195262007-10-09 17:51:17 +00001624 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001625 break;
1626 ConsumeToken(); // consume ','
1627 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001628 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001629 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001630}
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001632/// objc-throw-statement:
1633/// throw expression[opt];
1634///
John McCall60d7b3a2010-08-24 06:29:42 +00001635StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1636 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001637 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001638 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001639 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001640 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001641 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001642 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001643 }
1644 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001645 // consume ';'
1646 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001647 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001648}
1649
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001650/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001651/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001652///
John McCall60d7b3a2010-08-24 06:29:42 +00001653StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001654Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001655 ConsumeToken(); // consume synchronized
1656 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001657 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001658 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001659 }
John McCall07524032011-07-27 21:50:02 +00001660
1661 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001662 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001663 ExprResult operand(ParseExpression());
1664
1665 if (Tok.is(tok::r_paren)) {
1666 ConsumeParen(); // ')'
1667 } else {
1668 if (!operand.isInvalid())
1669 Diag(Tok, diag::err_expected_rparen);
1670
1671 // Skip forward until we see a left brace, but don't consume it.
1672 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001673 }
John McCall07524032011-07-27 21:50:02 +00001674
1675 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001676 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001677 if (!operand.isInvalid())
1678 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001679 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001680 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001681
John McCall07524032011-07-27 21:50:02 +00001682 // Check the @synchronized operand now.
1683 if (!operand.isInvalid())
1684 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001685
John McCall07524032011-07-27 21:50:02 +00001686 // Parse the compound statement within a new scope.
1687 ParseScope bodyScope(this, Scope::DeclScope);
1688 StmtResult body(ParseCompoundStatementBody());
1689 bodyScope.Exit();
1690
1691 // If there was a semantic or parse error earlier with the
1692 // operand, fail now.
1693 if (operand.isInvalid())
1694 return StmtError();
1695
1696 if (body.isInvalid())
1697 body = Actions.ActOnNullStmt(Tok.getLocation());
1698
1699 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001700}
1701
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001702/// objc-try-catch-statement:
1703/// @try compound-statement objc-catch-list[opt]
1704/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1705///
1706/// objc-catch-list:
1707/// @catch ( parameter-declaration ) compound-statement
1708/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1709/// catch-parameter-declaration:
1710/// parameter-declaration
1711/// '...' [OBJC2]
1712///
John McCall60d7b3a2010-08-24 06:29:42 +00001713StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001714 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001715
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001716 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001717 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001718 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001719 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001720 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001721 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001722 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001723 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001724 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001725 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001726 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001727 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001728
Chris Lattnerdf195262007-10-09 17:51:17 +00001729 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001730 // At this point, we need to lookahead to determine if this @ is the start
1731 // of an @catch or @finally. We don't want to consume the @ token if this
1732 // is an @try or @encode or something else.
1733 Token AfterAt = GetLookAheadToken(1);
1734 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1735 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1736 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001737
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001738 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001739 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001740 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001741 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001742 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001743 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001744 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001745 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001746 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001747 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001748 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001749 ParseDeclarator(ParmDecl);
1750
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001751 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001752 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001753 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001754 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001755 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Steve Naroff93a25952009-04-07 22:56:58 +00001757 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Steve Naroff93a25952009-04-07 22:56:58 +00001759 if (Tok.is(tok::r_paren))
1760 RParenLoc = ConsumeParen();
1761 else // Skip over garbage, until we get to ')'. Eat the ')'.
1762 SkipUntil(tok::r_paren, true, false);
1763
John McCall60d7b3a2010-08-24 06:29:42 +00001764 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001765 if (Tok.is(tok::l_brace))
1766 CatchBody = ParseCompoundStatementBody();
1767 else
1768 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001769 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001770 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001771
John McCall60d7b3a2010-08-24 06:29:42 +00001772 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001773 RParenLoc,
1774 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001775 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001776 if (!Catch.isInvalid())
1777 CatchStmts.push_back(Catch.release());
1778
Steve Naroff64515f32008-02-05 21:27:35 +00001779 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001780 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1781 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001782 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001783 }
1784 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001785 } else {
1786 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001787 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001788 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001789
John McCall60d7b3a2010-08-24 06:29:42 +00001790 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001791 if (Tok.is(tok::l_brace))
1792 FinallyBody = ParseCompoundStatementBody();
1793 else
1794 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001795 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001796 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001797 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001798 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001799 catch_or_finally_seen = true;
1800 break;
1801 }
1802 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001803 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001804 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001805 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001806 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001807
John McCall9ae2f072010-08-23 23:25:46 +00001808 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001809 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001810 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001811}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001812
John McCallf85e1932011-06-15 23:02:42 +00001813/// objc-autoreleasepool-statement:
1814/// @autoreleasepool compound-statement
1815///
1816StmtResult
1817Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1818 ConsumeToken(); // consume autoreleasepool
1819 if (Tok.isNot(tok::l_brace)) {
1820 Diag(Tok, diag::err_expected_lbrace);
1821 return StmtError();
1822 }
1823 // Enter a scope to hold everything within the compound stmt. Compound
1824 // statements can always hold declarations.
1825 ParseScope BodyScope(this, Scope::DeclScope);
1826
1827 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1828
1829 BodyScope.Exit();
1830 if (AutoreleasePoolBody.isInvalid())
1831 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1832 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1833 AutoreleasePoolBody.take());
1834}
1835
Steve Naroff3536b442007-09-06 21:24:23 +00001836/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001837///
John McCalld226f652010-08-21 09:40:31 +00001838Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001839 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001840
John McCallf312b1e2010-08-26 23:41:50 +00001841 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1842 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001844 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001845 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001846 if (ObjCImpDecl) {
1847 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001848 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001849 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001850 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001851 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001852
Steve Naroff409be832007-11-11 19:54:21 +00001853 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001854 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001855 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Steve Naroff409be832007-11-11 19:54:21 +00001857 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1858 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Steve Naroff409be832007-11-11 19:54:21 +00001860 // If we didn't find the '{', bail out.
1861 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001862 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001863 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001864 // Allow the rest of sema to find private method decl implementations.
1865 if (MDecl)
1866 Actions.AddAnyMethodToGlobalPool(MDecl);
1867
1868 // Consume the tokens and store them for later parsing.
1869 LexedMethod* LM = new LexedMethod(this, MDecl);
1870 LateParsedObjCMethods.push_back(LM);
1871 CachedTokens &Toks = LM->Toks;
1872 // Begin by storing the '{' token.
1873 Toks.push_back(Tok);
1874 ConsumeBrace();
1875 // Consume everything up to (and including) the matching right brace.
1876 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001877 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001878}
Anders Carlsson55085182007-08-21 17:43:55 +00001879
John McCall60d7b3a2010-08-24 06:29:42 +00001880StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001881 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001882 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001883 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001884 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001885 }
1886
1887 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001888 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001889
1890 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001891 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001892
1893 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001894 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001895
1896 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1897 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001898
John McCall60d7b3a2010-08-24 06:29:42 +00001899 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001900 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001901 // If the expression is invalid, skip ahead to the next semicolon. Not
1902 // doing this opens us up to the possibility of infinite loops if
1903 // ParseExpression does not consume any tokens.
1904 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001905 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001906 }
Chris Lattner5d803162009-12-07 16:33:19 +00001907
Steve Naroff64515f32008-02-05 21:27:35 +00001908 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001909 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001910 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001911}
1912
John McCall60d7b3a2010-08-24 06:29:42 +00001913ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001914 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001915 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001916 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001917 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001918 return ExprError();
1919
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001920 case tok::string_literal: // primary-expression: string-literal
1921 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001922 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001923 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001924 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001925 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001926
Chris Lattner4fef81d2008-08-05 06:19:09 +00001927 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1928 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001929 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001930 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001931 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001932 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001933 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001934 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001935 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001936 }
Anders Carlsson55085182007-08-21 17:43:55 +00001937 }
Anders Carlsson55085182007-08-21 17:43:55 +00001938}
1939
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001940/// \brirg Parse the receiver of an Objective-C++ message send.
1941///
1942/// This routine parses the receiver of a message send in
1943/// Objective-C++ either as a type or as an expression. Note that this
1944/// routine must not be called to parse a send to 'super', since it
1945/// has no way to return such a result.
1946///
1947/// \param IsExpr Whether the receiver was parsed as an expression.
1948///
1949/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1950/// IsExpr is true), the parsed expression. If the receiver was parsed
1951/// as a type (\c IsExpr is false), the parsed type.
1952///
1953/// \returns True if an error occurred during parsing or semantic
1954/// analysis, in which case the arguments do not have valid
1955/// values. Otherwise, returns false for a successful parse.
1956///
1957/// objc-receiver: [C++]
1958/// 'super' [not parsed here]
1959/// expression
1960/// simple-type-specifier
1961/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001962bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001963 InMessageExpressionRAIIObject InMessage(*this, true);
1964
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001965 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1966 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1967 TryAnnotateTypeOrScopeToken();
1968
1969 if (!isCXXSimpleTypeSpecifier()) {
1970 // objc-receiver:
1971 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001972 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001973 if (Receiver.isInvalid())
1974 return true;
1975
1976 IsExpr = true;
1977 TypeOrExpr = Receiver.take();
1978 return false;
1979 }
1980
1981 // objc-receiver:
1982 // typename-specifier
1983 // simple-type-specifier
1984 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00001985 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001986 ParseCXXSimpleTypeSpecifier(DS);
1987
1988 if (Tok.is(tok::l_paren)) {
1989 // If we see an opening parentheses at this point, we are
1990 // actually parsing an expression that starts with a
1991 // function-style cast, e.g.,
1992 //
1993 // postfix-expression:
1994 // simple-type-specifier ( expression-list [opt] )
1995 // typename-specifier ( expression-list [opt] )
1996 //
1997 // Parse the remainder of this case, then the (optional)
1998 // postfix-expression suffix, followed by the (optional)
1999 // right-hand side of the binary expression. We have an
2000 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002001 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002002 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002003 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002004 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002005 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002006 if (Receiver.isInvalid())
2007 return true;
2008
2009 IsExpr = true;
2010 TypeOrExpr = Receiver.take();
2011 return false;
2012 }
2013
2014 // We have a class message. Turn the simple-type-specifier or
2015 // typename-specifier we parsed into a type and parse the
2016 // remainder of the class message.
2017 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002018 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002019 if (Type.isInvalid())
2020 return true;
2021
2022 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002023 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002024 return false;
2025}
2026
Douglas Gregor1b730e82010-05-31 14:40:22 +00002027/// \brief Determine whether the parser is currently referring to a an
2028/// Objective-C message send, using a simplified heuristic to avoid overhead.
2029///
2030/// This routine will only return true for a subset of valid message-send
2031/// expressions.
2032bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002033 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002034 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002035 return GetLookAheadToken(1).is(tok::identifier) &&
2036 GetLookAheadToken(2).is(tok::identifier);
2037}
2038
Douglas Gregor9497a732010-09-16 01:51:54 +00002039bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2040 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2041 InMessageExpression)
2042 return false;
2043
2044
2045 ParsedType Type;
2046
2047 if (Tok.is(tok::annot_typename))
2048 Type = getTypeAnnotation(Tok);
2049 else if (Tok.is(tok::identifier))
2050 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2051 getCurScope());
2052 else
2053 return false;
2054
2055 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2056 const Token &AfterNext = GetLookAheadToken(2);
2057 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2058 if (Tok.is(tok::identifier))
2059 TryAnnotateTypeOrScopeToken();
2060
2061 return Tok.is(tok::annot_typename);
2062 }
2063 }
2064
2065 return false;
2066}
2067
Mike Stump1eb44332009-09-09 15:08:12 +00002068/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002069/// '[' objc-receiver objc-message-args ']'
2070///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002071/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002072/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002073/// expression
2074/// class-name
2075/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002076///
John McCall60d7b3a2010-08-24 06:29:42 +00002077ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002078 assert(Tok.is(tok::l_square) && "'[' expected");
2079 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2080
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002081 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002082 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002083 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002084 return ExprError();
2085 }
2086
Douglas Gregor0fbda682010-09-15 14:51:05 +00002087 InMessageExpressionRAIIObject InMessage(*this, true);
2088
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002089 if (getLang().CPlusPlus) {
2090 // We completely separate the C and C++ cases because C++ requires
2091 // more complicated (read: slower) parsing.
2092
2093 // Handle send to super.
2094 // FIXME: This doesn't benefit from the same typo-correction we
2095 // get in Objective-C.
2096 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002097 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002098 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2099 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002100
2101 // Parse the receiver, which is either a type or an expression.
2102 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002103 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002104 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2105 SkipUntil(tok::r_square);
2106 return ExprError();
2107 }
2108
2109 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002110 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2111 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002112 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002113
2114 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002115 ParsedType::getFromOpaquePtr(TypeOrExpr),
2116 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002117 }
2118
2119 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002120 IdentifierInfo *Name = Tok.getIdentifierInfo();
2121 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002122 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002123 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002124 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002125 NextToken().is(tok::period),
2126 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002127 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002128 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2129 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002130
John McCallf312b1e2010-08-26 23:41:50 +00002131 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002132 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002133 SkipUntil(tok::r_square);
2134 return ExprError();
2135 }
2136
Douglas Gregor1569f952010-04-21 20:38:13 +00002137 ConsumeToken(); // the type name
2138
2139 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002140 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002141
John McCallf312b1e2010-08-26 23:41:50 +00002142 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002143 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002144 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002145 }
Chris Lattner699b6612008-01-25 18:59:06 +00002146 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002147
2148 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002149 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002150 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002151 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002152 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002153 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002154
John McCallb3d87482010-08-24 05:47:05 +00002155 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2156 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002157}
Sebastian Redl1d922962008-12-13 15:32:12 +00002158
Douglas Gregor2725ca82010-04-21 19:57:20 +00002159/// \brief Parse the remainder of an Objective-C message following the
2160/// '[' objc-receiver.
2161///
2162/// This routine handles sends to super, class messages (sent to a
2163/// class name), and instance messages (sent to an object), and the
2164/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2165/// ReceiverExpr, respectively. Only one of these parameters may have
2166/// a valid value.
2167///
2168/// \param LBracLoc The location of the opening '['.
2169///
2170/// \param SuperLoc If this is a send to 'super', the location of the
2171/// 'super' keyword that indicates a send to the superclass.
2172///
2173/// \param ReceiverType If this is a class message, the type of the
2174/// class we are sending a message to.
2175///
2176/// \param ReceiverExpr If this is an instance message, the expression
2177/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002178///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002179/// objc-message-args:
2180/// objc-selector
2181/// objc-keywordarg-list
2182///
2183/// objc-keywordarg-list:
2184/// objc-keywordarg
2185/// objc-keywordarg-list objc-keywordarg
2186///
Mike Stump1eb44332009-09-09 15:08:12 +00002187/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002188/// selector-name[opt] ':' objc-keywordexpr
2189///
2190/// objc-keywordexpr:
2191/// nonempty-expr-list
2192///
2193/// nonempty-expr-list:
2194/// assignment-expression
2195/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002196///
John McCall60d7b3a2010-08-24 06:29:42 +00002197ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002198Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002199 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002200 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002201 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002202 InMessageExpressionRAIIObject InMessage(*this, true);
2203
Steve Naroffc4df6d22009-11-07 02:08:14 +00002204 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002205 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002206 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2207 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002208 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002209 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2210 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002211 else
John McCall9ae2f072010-08-23 23:25:46 +00002212 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002213 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002214 cutOffParsing();
2215 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002216 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002217
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002218 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002219 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002220 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002221
Chris Lattner5f9e2722011-07-23 10:55:15 +00002222 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002223 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002224 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002225
Chris Lattnerdf195262007-10-09 17:51:17 +00002226 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002227 while (1) {
2228 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002229 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002230 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002231
Chris Lattnerdf195262007-10-09 17:51:17 +00002232 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002233 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002234 // We must manually skip to a ']', otherwise the expression skipper will
2235 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2236 // the enclosing expression.
2237 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002238 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002239 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002240
Steve Naroff68d331a2007-09-27 14:38:14 +00002241 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002242 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002243
2244 if (Tok.is(tok::code_completion)) {
2245 if (SuperLoc.isValid())
2246 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2247 KeyIdents.data(),
2248 KeyIdents.size(),
2249 /*AtArgumentEpression=*/true);
2250 else if (ReceiverType)
2251 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2252 KeyIdents.data(),
2253 KeyIdents.size(),
2254 /*AtArgumentEpression=*/true);
2255 else
2256 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2257 KeyIdents.data(),
2258 KeyIdents.size(),
2259 /*AtArgumentEpression=*/true);
2260
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002261 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002262 return ExprError();
2263 }
2264
John McCall60d7b3a2010-08-24 06:29:42 +00002265 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002266 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002267 // We must manually skip to a ']', otherwise the expression skipper will
2268 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2269 // the enclosing expression.
2270 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002271 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002272 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002273
Steve Naroff37387c92007-09-17 20:25:27 +00002274 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002275 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002276
Douglas Gregord3c68542009-11-19 01:08:35 +00002277 // Code completion after each argument.
2278 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002279 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002280 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002281 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002282 KeyIdents.size(),
2283 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002284 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002285 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002286 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002287 KeyIdents.size(),
2288 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002289 else
John McCall9ae2f072010-08-23 23:25:46 +00002290 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002291 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002292 KeyIdents.size(),
2293 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002294 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002295 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002296 }
2297
Steve Naroff37387c92007-09-17 20:25:27 +00002298 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002299 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002300 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002301 break;
2302 // We have a selector or a colon, continue parsing.
2303 }
2304 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002305 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002306 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002307 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002308 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002309 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002310 // We must manually skip to a ']', otherwise the expression skipper will
2311 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2312 // the enclosing expression.
2313 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002314 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002315 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002316
Steve Naroff49f109c2007-11-15 13:05:42 +00002317 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002318 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002319 }
2320 } else if (!selIdent) {
2321 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002322
Chris Lattner4fef81d2008-08-05 06:19:09 +00002323 // We must manually skip to a ']', otherwise the expression skipper will
2324 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2325 // the enclosing expression.
2326 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002327 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002328 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002329
Chris Lattnerdf195262007-10-09 17:51:17 +00002330 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002331 if (Tok.is(tok::identifier))
2332 Diag(Tok, diag::err_expected_colon);
2333 else
2334 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002335 // We must manually skip to a ']', otherwise the expression skipper will
2336 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2337 // the enclosing expression.
2338 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002339 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002340 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002341
Chris Lattner699b6612008-01-25 18:59:06 +00002342 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002343
Steve Naroff29238a02007-10-05 18:42:47 +00002344 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002345 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002346 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002347 KeyLocs.push_back(Loc);
2348 }
Chris Lattnerff384912007-10-07 02:00:24 +00002349 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002350
Douglas Gregor2725ca82010-04-21 19:57:20 +00002351 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002352 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002353 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002354 MultiExprArg(Actions,
2355 KeyExprs.take(),
2356 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002357 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002358 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002359 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002360 MultiExprArg(Actions,
2361 KeyExprs.take(),
2362 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002363 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002364 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002365 MultiExprArg(Actions,
2366 KeyExprs.take(),
2367 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002368}
2369
John McCall60d7b3a2010-08-24 06:29:42 +00002370ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2371 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002372 if (Res.isInvalid()) return move(Res);
2373
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002374 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2375 // expressions. At this point, we know that the only valid thing that starts
2376 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002377 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002378 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002379 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002380 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002381
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002382 while (Tok.is(tok::at)) {
2383 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002384
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002385 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002386 if (!isTokenStringLiteral())
2387 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002388
John McCall60d7b3a2010-08-24 06:29:42 +00002389 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002390 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002391 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002392
Sebastian Redleffa8d12008-12-10 00:02:53 +00002393 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002394 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002395
2396 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2397 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002398}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002399
2400/// objc-encode-expression:
2401/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002402ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002403Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002404 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002405
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002406 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002407
Chris Lattner4fef81d2008-08-05 06:19:09 +00002408 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002409 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2410
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002411 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002412
Douglas Gregor809070a2009-02-18 17:45:20 +00002413 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002414
Anders Carlsson4988ae32007-08-23 15:31:37 +00002415 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002416
Douglas Gregor809070a2009-02-18 17:45:20 +00002417 if (Ty.isInvalid())
2418 return ExprError();
2419
Mike Stump1eb44332009-09-09 15:08:12 +00002420 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002421 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002422}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002423
2424/// objc-protocol-expression
2425/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002426ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002427Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002428 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002429
Chris Lattner4fef81d2008-08-05 06:19:09 +00002430 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002431 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2432
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002433 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002434
Chris Lattner4fef81d2008-08-05 06:19:09 +00002435 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002436 return ExprError(Diag(Tok, diag::err_expected_ident));
2437
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002438 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002439 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002440
Anders Carlsson4988ae32007-08-23 15:31:37 +00002441 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002442
Sebastian Redl1d922962008-12-13 15:32:12 +00002443 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2444 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002445}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002446
2447/// objc-selector-expression
2448/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002449ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002450 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002451
Chris Lattner4fef81d2008-08-05 06:19:09 +00002452 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002453 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2454
Chris Lattner5f9e2722011-07-23 10:55:15 +00002455 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002456 SourceLocation LParenLoc = ConsumeParen();
2457 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002458
2459 if (Tok.is(tok::code_completion)) {
2460 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2461 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002462 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002463 return ExprError();
2464 }
2465
Chris Lattner2fc5c242009-04-11 18:13:45 +00002466 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002467 if (!SelIdent && // missing selector name.
2468 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002469 return ExprError(Diag(Tok, diag::err_expected_ident));
2470
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002471 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002472 unsigned nColons = 0;
2473 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002474 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002475 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2476 ++nColons;
2477 KeyIdents.push_back(0);
2478 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002479 return ExprError(Diag(Tok, diag::err_expected_colon));
2480
Chris Lattner5add7542010-08-27 22:32:41 +00002481 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002482 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002483 if (Tok.is(tok::r_paren))
2484 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002485
2486 if (Tok.is(tok::code_completion)) {
2487 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2488 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002489 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002490 return ExprError();
2491 }
2492
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002493 // Check for another keyword selector.
2494 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002495 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002496 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002497 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002498 break;
2499 }
Steve Naroff887407e2007-12-05 22:21:29 +00002500 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002501 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002502 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002503 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2504 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002505 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002506
2507Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2508
2509 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2510 // Append the current token at the end of the new token stream so that it
2511 // doesn't get lost.
2512 LM.Toks.push_back(Tok);
2513 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2514
2515 // MDecl might be null due to error in method prototype, etc.
2516 Decl *MDecl = LM.D;
2517 // Consume the previously pushed token.
2518 ConsumeAnyToken();
2519
2520 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2521 SourceLocation BraceLoc = Tok.getLocation();
2522 // Enter a scope for the method body.
2523 ParseScope BodyScope(this,
2524 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2525
2526 // Tell the actions module that we have entered a method definition with the
2527 // specified Declarator for the method.
2528 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2529
2530 if (PP.isCodeCompletionEnabled()) {
2531 if (trySkippingFunctionBodyForCodeCompletion()) {
2532 BodyScope.Exit();
2533 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2534 }
2535 }
2536
2537 StmtResult FnBody(ParseCompoundStatementBody());
2538
2539 // If the function body could not be parsed, make a bogus compoundstmt.
2540 if (FnBody.isInvalid())
2541 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2542 MultiStmtArg(Actions), false);
2543
2544 // Leave the function body scope.
2545 BodyScope.Exit();
2546
2547 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2548}