blob: f54c3eef07982aa0a9f785c4fdd926228265d90b [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'
John McCalld226f652010-08-21 09:40:31 +000032Decl *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)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000038 }
39
Steve Naroff861cf3e2007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
43 case tok::objc_interface:
44 return ParseObjCAtInterfaceDeclaration(AtLoc);
45 case tok::objc_protocol:
46 return ParseObjCAtProtocolDeclaration(AtLoc);
47 case tok::objc_implementation:
48 return ParseObjCAtImplementationDeclaration(AtLoc);
49 case tok::objc_end:
50 return ParseObjCAtEndDeclaration(AtLoc);
51 case tok::objc_compatibility_alias:
52 return ParseObjCAtAliasDeclaration(AtLoc);
53 case tok::objc_synthesize:
54 return ParseObjCPropertySynthesize(AtLoc);
55 case tok::objc_dynamic:
56 return ParseObjCPropertyDynamic(AtLoc);
57 default:
58 Diag(AtLoc, diag::err_unexpected_at);
59 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000060 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
62}
63
64///
Mike Stump1eb44332009-09-09 15:08:12 +000065/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000066/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000067///
John McCalld226f652010-08-21 09:40:31 +000068Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 ConsumeToken(); // the identifier "class"
70 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000071 llvm::SmallVector<SourceLocation, 8> ClassLocs;
72
Mike Stump1eb44332009-09-09 15:08:12 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000076 Diag(Tok, diag::err_expected_ident);
77 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000078 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000081 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000082 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattnerdf195262007-10-09 17:51:17 +000084 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000085 break;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ConsumeToken();
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Consume the ';'.
91 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCalld226f652010-08-21 09:40:31 +000092 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekc09cba62009-11-17 23:12:20 +000094 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
95 ClassLocs.data(),
96 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
Steve Naroffdac269b2007-08-20 21:31:48 +000099///
100/// objc-interface:
101/// objc-class-interface-attributes[opt] objc-class-interface
102/// objc-category-interface
103///
104/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000107/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000108/// objc-interface-decl-list
109/// @end
110///
111/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000112/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000113/// objc-protocol-refs[opt]
114/// objc-interface-decl-list
115/// @end
116///
117/// objc-superclass:
118/// ':' identifier
119///
120/// objc-class-interface-attributes:
121/// __attribute__((visibility("default")))
122/// __attribute__((visibility("hidden")))
123/// __attribute__((deprecated))
124/// __attribute__((unavailable))
125/// __attribute__((objc_exception)) - used by NSException on 64-bit
126///
John McCalld226f652010-08-21 09:40:31 +0000127Decl *Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000129 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000130 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
131 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000133 // Code completion after '@interface'.
134 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000135 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000136 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000137 }
138
Chris Lattnerdf195262007-10-09 17:51:17 +0000139 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000140 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000141 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000143
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000145 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000146 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000147 if (Tok.is(tok::l_paren) &&
148 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000149 SourceLocation lparenLoc = ConsumeParen();
150 SourceLocation categoryLoc, rparenLoc;
151 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000152 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000153 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000154 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000155 }
156
Steve Naroff527fe232007-08-23 19:56:30 +0000157 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000158 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000159 categoryId = Tok.getIdentifierInfo();
160 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000161 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000162 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000163 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000164 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000166 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000167 Diag(Tok, diag::err_expected_rparen);
168 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +0000169 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
171 rparenLoc = ConsumeParen();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000172 // Next, we need to check for any protocol references.
173 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +0000174 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000175 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
176 if (Tok.is(tok::less) &&
177 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000178 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000179 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000181 if (attrList) // categories don't support attributes.
182 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
John McCalld226f652010-08-21 09:40:31 +0000184 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000185 Actions.ActOnStartCategoryInterface(atLoc,
186 nameId, nameLoc,
187 categoryId, categoryLoc,
188 ProtocolRefs.data(),
189 ProtocolRefs.size(),
190 ProtocolLocs.data(),
191 EndProtoLoc);
192 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000193 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
194 atLoc);
195
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000196 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
197 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000198 }
199 // Parse a class interface.
200 IdentifierInfo *superClassId = 0;
201 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000202
Chris Lattnerdf195262007-10-09 17:51:17 +0000203 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000205
206 // Code completion of superclass names.
207 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000208 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000209 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000210 }
211
Chris Lattnerdf195262007-10-09 17:51:17 +0000212 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000213 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000214 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000215 }
216 superClassId = Tok.getIdentifierInfo();
217 superClassLoc = ConsumeToken();
218 }
219 // Next, we need to check for any protocol references.
John McCalld226f652010-08-21 09:40:31 +0000220 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000221 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000223 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000224 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
225 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000226 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
John McCalld226f652010-08-21 09:40:31 +0000228 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000229 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000230 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000231 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000232 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000233 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattnerdf195262007-10-09 17:51:17 +0000235 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000236 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000237
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000238 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000239 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000240}
241
John McCalld0014542009-12-03 22:31:13 +0000242/// The Objective-C property callback. This should be defined where
243/// it's used, but instead it's been lifted to here to support VS2005.
244struct Parser::ObjCPropertyCallback : FieldCallback {
245 Parser &P;
John McCalld226f652010-08-21 09:40:31 +0000246 Decl *IDecl;
247 llvm::SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000248 ObjCDeclSpec &OCDS;
249 SourceLocation AtLoc;
250 tok::ObjCKeywordKind MethodImplKind;
251
John McCalld226f652010-08-21 09:40:31 +0000252 ObjCPropertyCallback(Parser &P, Decl *IDecl,
253 llvm::SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000254 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
255 tok::ObjCKeywordKind MethodImplKind) :
256 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
257 MethodImplKind(MethodImplKind) {
258 }
259
John McCalld226f652010-08-21 09:40:31 +0000260 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000261 if (FD.D.getIdentifier() == 0) {
262 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
263 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000264 return 0;
John McCalld0014542009-12-03 22:31:13 +0000265 }
266 if (FD.BitfieldSize) {
267 P.Diag(AtLoc, diag::err_objc_property_bitfield)
268 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000269 return 0;
John McCalld0014542009-12-03 22:31:13 +0000270 }
271
272 // Install the property declarator into interfaceDecl.
273 IdentifierInfo *SelName =
274 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
275
276 Selector GetterSel =
277 P.PP.getSelectorTable().getNullarySelector(SelName);
278 IdentifierInfo *SetterName = OCDS.getSetterName();
279 Selector SetterSel;
280 if (SetterName)
281 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
282 else
283 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
284 P.PP.getSelectorTable(),
285 FD.D.getIdentifier());
286 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000287 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000288 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000289 GetterSel, SetterSel, IDecl,
290 &isOverridingProperty,
291 MethodImplKind);
292 if (!isOverridingProperty)
293 Props.push_back(Property);
294
295 return Property;
296 }
297};
298
Steve Naroffdac269b2007-08-20 21:31:48 +0000299/// objc-interface-decl-list:
300/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000301/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000302/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000303/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000304/// objc-interface-decl-list declaration
305/// objc-interface-decl-list ';'
306///
Steve Naroff294494e2007-08-22 16:35:03 +0000307/// objc-method-requirement: [OBJC2]
308/// @required
309/// @optional
310///
John McCalld226f652010-08-21 09:40:31 +0000311void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000312 tok::ObjCKeywordKind contextKey) {
John McCalld226f652010-08-21 09:40:31 +0000313 llvm::SmallVector<Decl *, 32> allMethods;
314 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000315 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000316 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek782f2f52010-01-07 01:20:12 +0000318 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000319
Steve Naroff294494e2007-08-22 16:35:03 +0000320 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000321 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000322 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000323 Decl *methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000324 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000325 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000326 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
327 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000328 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
329 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000330 continue;
331 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000332 if (Tok.is(tok::l_paren)) {
333 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000334 ParseObjCMethodDecl(Tok.getLocation(),
335 tok::minus,
336 interfaceDecl,
337 MethodImplKind);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000338 continue;
339 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000340 // Ignore excess semicolons.
341 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000342 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000343 continue;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerbc662af2008-10-20 06:10:06 +0000346 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000347 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000348 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000350 // Code completion within an Objective-C interface.
351 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000352 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000353 ObjCImpDecl? Sema::PCC_ObjCImplementation
354 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000355 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000356 }
357
Chris Lattnere82a10f2008-10-20 05:46:22 +0000358 // If we don't have an @ directive, parse it as a function definition.
359 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000360 // The code below does not consume '}'s because it is afraid of eating the
361 // end of a namespace. Because of the way this code is structured, an
362 // erroneous r_brace would cause an infinite loop if not handled here.
363 if (Tok.is(tok::r_brace))
364 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Steve Naroff4985ace2007-08-22 18:35:33 +0000366 // FIXME: as the name implies, this rule allows function definitions.
367 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000368 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000369 continue;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattnere82a10f2008-10-20 05:46:22 +0000372 // Otherwise, we have an @ directive, eat the @.
373 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000374 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000375 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000376 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000377 break;
378 }
379
Chris Lattnera2449b22008-10-20 05:57:40 +0000380 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattnera2449b22008-10-20 05:57:40 +0000382 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000383 AtEnd.setBegin(AtLoc);
384 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000385 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000386 } else if (DirectiveKind == tok::objc_not_keyword) {
387 Diag(Tok, diag::err_objc_unknown_at);
388 SkipUntil(tok::semi);
389 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnerbc662af2008-10-20 06:10:06 +0000392 // Eat the identifier.
393 ConsumeToken();
394
Chris Lattnera2449b22008-10-20 05:57:40 +0000395 switch (DirectiveKind) {
396 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000397 // FIXME: If someone forgets an @end on a protocol, this loop will
398 // continue to eat up tons of stuff and spew lots of nonsense errors. It
399 // would probably be better to bail out if we saw an @class or @interface
400 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000401 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000402 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000403 SkipUntil(tok::r_brace, tok::at);
404 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattnera2449b22008-10-20 05:57:40 +0000406 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000407 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000408 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000410 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000411 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000412 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000413 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000414 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnera2449b22008-10-20 05:57:40 +0000416 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000417 if (!getLang().ObjC2)
418 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
419
Chris Lattnere82a10f2008-10-20 05:46:22 +0000420 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000421 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000422 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000423 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
424 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000425
John McCalld0014542009-12-03 22:31:13 +0000426 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
427 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000428
Chris Lattnere82a10f2008-10-20 05:46:22 +0000429 // Parse all the comma separated declarators.
430 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000431 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000433 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
434 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000435 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000436 }
Steve Naroff294494e2007-08-22 16:35:03 +0000437 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000438
439 // We break out of the big loop in two cases: when we see @end or when we see
440 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000441 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000442 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000443 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000444 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000445 ConsumeToken(); // the "end" identifier
446 else
447 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattnera2449b22008-10-20 05:57:40 +0000449 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000450 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000451 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000452 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000453 allProperties.data(), allProperties.size(),
454 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000455}
456
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000457/// Parse property attribute declarations.
458///
459/// property-attr-decl: '(' property-attrlist ')'
460/// property-attrlist:
461/// property-attribute
462/// property-attrlist ',' property-attribute
463/// property-attribute:
464/// getter '=' identifier
465/// setter '=' identifier ':'
466/// readonly
467/// readwrite
468/// assign
469/// retain
470/// copy
471/// nonatomic
472///
John McCalld226f652010-08-21 09:40:31 +0000473void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl,
474 Decl **Methods,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000475 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000476 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000477 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000479 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000480 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000481 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000482 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000483 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000484 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000486 // If this is not an identifier at all, bail out early.
487 if (II == 0) {
488 MatchRHSPunctuation(tok::r_paren, LHSLoc);
489 return;
490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattner156b0612008-10-20 07:37:22 +0000492 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner92e62b02008-11-20 04:42:34 +0000494 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000495 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000496 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000497 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000498 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000499 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000500 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000502 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000506 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000507 bool IsSetter = II->getNameStart()[0] == 's';
508
Chris Lattnere00da7c2008-10-20 07:39:53 +0000509 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000510 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
511 diag::err_objc_expected_equal_for_getter;
512
513 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000514 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregor4ad96852009-11-19 07:41:15 +0000516 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000517 if (IsSetter)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000518 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000519 Methods, NumMethods);
520 else
Douglas Gregor23c94db2010-07-02 17:43:08 +0000521 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000522 Methods, NumMethods);
Douglas Gregordc845342010-05-25 05:58:43 +0000523 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000524 }
525
Anders Carlsson42499be2010-10-02 17:45:21 +0000526
527 SourceLocation SelLoc;
528 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
529
530 if (!SelIdent) {
531 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
532 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000533 SkipUntil(tok::r_paren);
534 return;
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Anders Carlsson42499be2010-10-02 17:45:21 +0000537 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000539 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000541 if (ExpectAndConsume(tok::colon,
542 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000543 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000544 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000545 } else {
546 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000547 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000548 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000549 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000550 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000551 SkipUntil(tok::r_paren);
552 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattner156b0612008-10-20 07:37:22 +0000555 if (Tok.isNot(tok::comma))
556 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner156b0612008-10-20 07:37:22 +0000558 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner156b0612008-10-20 07:37:22 +0000561 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000562}
563
Steve Naroff3536b442007-09-06 21:24:23 +0000564/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000565/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000566/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000567///
568/// objc-instance-method: '-'
569/// objc-class-method: '+'
570///
Steve Naroff4985ace2007-08-22 18:35:33 +0000571/// objc-method-attributes: [OBJC2]
572/// __attribute__((deprecated))
573///
John McCalld226f652010-08-21 09:40:31 +0000574Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
575 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000576 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000577
Mike Stump1eb44332009-09-09 15:08:12 +0000578 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000579 SourceLocation mLoc = ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000580 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000581 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000582 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000583 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000584}
585
586/// objc-selector:
587/// identifier
588/// one of
589/// enum struct union if else while do for switch case default
590/// break continue return goto asm sizeof typeof __alignof
591/// unsigned long const short volatile signed restrict _Complex
592/// in out inout bycopy byref oneway int char float double void _Bool
593///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000594IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000595
Chris Lattnerff384912007-10-07 02:00:24 +0000596 switch (Tok.getKind()) {
597 default:
598 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000599 case tok::ampamp:
600 case tok::ampequal:
601 case tok::amp:
602 case tok::pipe:
603 case tok::tilde:
604 case tok::exclaim:
605 case tok::exclaimequal:
606 case tok::pipepipe:
607 case tok::pipeequal:
608 case tok::caret:
609 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000610 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000611 if (isalpha(ThisTok[0])) {
612 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
613 Tok.setKind(tok::identifier);
614 SelectorLoc = ConsumeToken();
615 return II;
616 }
617 return 0;
618 }
619
Chris Lattnerff384912007-10-07 02:00:24 +0000620 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000621 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000622 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000623 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000624 case tok::kw_break:
625 case tok::kw_case:
626 case tok::kw_catch:
627 case tok::kw_char:
628 case tok::kw_class:
629 case tok::kw_const:
630 case tok::kw_const_cast:
631 case tok::kw_continue:
632 case tok::kw_default:
633 case tok::kw_delete:
634 case tok::kw_do:
635 case tok::kw_double:
636 case tok::kw_dynamic_cast:
637 case tok::kw_else:
638 case tok::kw_enum:
639 case tok::kw_explicit:
640 case tok::kw_export:
641 case tok::kw_extern:
642 case tok::kw_false:
643 case tok::kw_float:
644 case tok::kw_for:
645 case tok::kw_friend:
646 case tok::kw_goto:
647 case tok::kw_if:
648 case tok::kw_inline:
649 case tok::kw_int:
650 case tok::kw_long:
651 case tok::kw_mutable:
652 case tok::kw_namespace:
653 case tok::kw_new:
654 case tok::kw_operator:
655 case tok::kw_private:
656 case tok::kw_protected:
657 case tok::kw_public:
658 case tok::kw_register:
659 case tok::kw_reinterpret_cast:
660 case tok::kw_restrict:
661 case tok::kw_return:
662 case tok::kw_short:
663 case tok::kw_signed:
664 case tok::kw_sizeof:
665 case tok::kw_static:
666 case tok::kw_static_cast:
667 case tok::kw_struct:
668 case tok::kw_switch:
669 case tok::kw_template:
670 case tok::kw_this:
671 case tok::kw_throw:
672 case tok::kw_true:
673 case tok::kw_try:
674 case tok::kw_typedef:
675 case tok::kw_typeid:
676 case tok::kw_typename:
677 case tok::kw_typeof:
678 case tok::kw_union:
679 case tok::kw_unsigned:
680 case tok::kw_using:
681 case tok::kw_virtual:
682 case tok::kw_void:
683 case tok::kw_volatile:
684 case tok::kw_wchar_t:
685 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000686 case tok::kw__Bool:
687 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000688 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000689 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000690 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000691 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000692 }
Steve Naroff294494e2007-08-22 16:35:03 +0000693}
694
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000695/// objc-for-collection-in: 'in'
696///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000697bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000698 // FIXME: May have to do additional look-ahead to only allow for
699 // valid tokens following an 'in'; such as an identifier, unary operators,
700 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000701 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000702 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000703}
704
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000705/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000706/// qualifier list and builds their bitmask representation in the input
707/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000708///
709/// objc-type-qualifiers:
710/// objc-type-qualifier
711/// objc-type-qualifiers objc-type-qualifier
712///
Douglas Gregord32b0222010-08-24 01:06:58 +0000713void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000714 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000715 if (Tok.is(tok::code_completion)) {
716 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
717 ConsumeCodeCompletionToken();
718 }
719
Chris Lattnercb53b362007-12-27 19:57:00 +0000720 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000721 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattnere8b724d2007-12-12 06:56:32 +0000723 const IdentifierInfo *II = Tok.getIdentifierInfo();
724 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000726 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000729 switch (i) {
730 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
732 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
733 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
734 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
735 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
736 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000737 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000738 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000739 ConsumeToken();
740 II = 0;
741 break;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Chris Lattnere8b724d2007-12-12 06:56:32 +0000744 // If this wasn't a recognized qualifier, bail out.
745 if (II) return;
746 }
747}
748
749/// objc-type-name:
750/// '(' objc-type-qualifiers[opt] type-name ')'
751/// '(' objc-type-qualifiers[opt] ')'
752///
John McCallb3d87482010-08-24 05:47:05 +0000753ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000754 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner4a76b292008-10-22 03:52:06 +0000756 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000757 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000759 // Parse type qualifiers, in, inout, etc.
Douglas Gregord32b0222010-08-24 01:06:58 +0000760 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000761
John McCallb3d87482010-08-24 05:47:05 +0000762 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000763 if (isTypeSpecifierQualifier()) {
764 TypeResult TypeSpec = ParseTypeName();
765 if (!TypeSpec.isInvalid())
766 Ty = TypeSpec.get();
767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Steve Naroffd7333c22008-10-21 14:15:04 +0000769 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000770 ConsumeParen();
771 else if (Tok.getLocation() == TypeStartLoc) {
772 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000773 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000774 SkipUntil(tok::r_paren);
775 } else {
776 // Otherwise, we found *something*, but didn't get a ')' in the right
777 // place. Emit an error then return what we have as the type.
778 MatchRHSPunctuation(tok::r_paren, LParenLoc);
779 }
Steve Narofff28b2642007-09-05 23:30:30 +0000780 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000781}
782
783/// objc-method-decl:
784/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000785/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000786/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000787/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000788///
789/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000790/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000791/// objc-keyword-selector objc-keyword-decl
792///
793/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000794/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
795/// objc-selector ':' objc-keyword-attributes[opt] identifier
796/// ':' objc-type-name objc-keyword-attributes[opt] identifier
797/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000798///
Steve Naroff4985ace2007-08-22 18:35:33 +0000799/// objc-parmlist:
800/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000801///
Steve Naroff4985ace2007-08-22 18:35:33 +0000802/// objc-parms:
803/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000804///
Steve Naroff4985ace2007-08-22 18:35:33 +0000805/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000806/// , ...
807///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000808/// objc-keyword-attributes: [OBJC2]
809/// __attribute__((unused))
810///
John McCalld226f652010-08-21 09:40:31 +0000811Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000812 tok::TokenKind mType,
813 Decl *IDecl,
814 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000815 ParsingDeclRAIIObject PD(*this);
816
Douglas Gregore8f5a172010-04-07 00:21:17 +0000817 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000818 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000819 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000820 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000821 }
822
Chris Lattnere8904e92008-08-23 01:48:03 +0000823 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000824 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000826 if (Tok.is(tok::l_paren))
Douglas Gregord32b0222010-08-24 01:06:58 +0000827 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek9e049352010-02-18 23:05:16 +0000829 // If attributes exist before the method, parse them.
830 llvm::OwningPtr<AttributeList> MethodAttrs;
831 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
832 MethodAttrs.reset(ParseGNUAttributes());
833
Douglas Gregore8f5a172010-04-07 00:21:17 +0000834 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000835 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000836 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000837 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000838 }
839
Ted Kremenek9e049352010-02-18 23:05:16 +0000840 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000841 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000842 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000843
Steve Naroff84c43102009-02-11 20:43:13 +0000844 // An unnamed colon is valid.
845 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000846 Diag(Tok, diag::err_expected_selector_for_method)
847 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000848 // Skip until we get a ; or {}.
849 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000850 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000853 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000854 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000855 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000856 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000857 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
858 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattnerff384912007-10-07 02:00:24 +0000860 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000861 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000862 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000863 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000864 0,
865 CParamInfo.data(), CParamInfo.size(),
866 MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000867 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000868 PD.complete(Result);
869 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000870 }
Steve Narofff28b2642007-09-05 23:30:30 +0000871
Steve Naroff68d331a2007-09-27 14:38:14 +0000872 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000873 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattnerff384912007-10-07 02:00:24 +0000875 while (1) {
John McCallf312b1e2010-08-26 23:41:50 +0000876 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattnerff384912007-10-07 02:00:24 +0000878 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000879 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000880 Diag(Tok, diag::err_expected_colon);
881 break;
882 }
883 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000884
John McCallb3d87482010-08-24 05:47:05 +0000885 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000886 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregord32b0222010-08-24 01:06:58 +0000887 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000888
Chris Lattnerff384912007-10-07 02:00:24 +0000889 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000890 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000891 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000892 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000893
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000894 // Code completion for the next piece of the selector.
895 if (Tok.is(tok::code_completion)) {
896 ConsumeCodeCompletionToken();
897 KeyIdents.push_back(SelIdent);
898 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
899 mType == tok::minus,
900 /*AtParameterName=*/true,
901 ReturnType,
902 KeyIdents.data(),
903 KeyIdents.size());
904 KeyIdents.pop_back();
905 break;
906 }
907
Chris Lattnerdf195262007-10-09 17:51:17 +0000908 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000909 Diag(Tok, diag::err_expected_ident); // missing argument name.
910 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattnere294d3f2009-04-11 18:57:04 +0000913 ArgInfo.Name = Tok.getIdentifierInfo();
914 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000915 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattnere294d3f2009-04-11 18:57:04 +0000917 ArgInfos.push_back(ArgInfo);
918 KeyIdents.push_back(SelIdent);
919
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000920 // Code completion for the next piece of the selector.
921 if (Tok.is(tok::code_completion)) {
922 ConsumeCodeCompletionToken();
923 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
924 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000925 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000926 ReturnType,
927 KeyIdents.data(),
928 KeyIdents.size());
929 break;
930 }
931
Chris Lattnerff384912007-10-07 02:00:24 +0000932 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000933 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000934 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000935 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000936 break;
937 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000938 }
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Steve Naroff335eafa2007-11-15 12:35:21 +0000940 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattnerff384912007-10-07 02:00:24 +0000942 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000943 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000944 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000945 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000946 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000947 ConsumeToken();
948 break;
949 }
Chris Lattnerff384912007-10-07 02:00:24 +0000950 DeclSpec DS;
951 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000952 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000953 Declarator ParmDecl(DS, Declarator::PrototypeContext);
954 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000955 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000956 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000957 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
958 ParmDecl.getIdentifierLoc(),
959 Param,
960 0));
961
Chris Lattnerff384912007-10-07 02:00:24 +0000962 }
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000964 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000965 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000966 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000967 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
968 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000970 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +0000971 return 0;
Chris Lattnerff384912007-10-07 02:00:24 +0000972 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
973 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +0000974 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000975 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000976 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000977 &ArgInfos[0],
978 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +0000979 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000980 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000981 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000982
983 // Delete referenced AttributeList objects.
John McCallf312b1e2010-08-26 23:41:50 +0000984 for (llvm::SmallVectorImpl<Sema::ObjCArgInfo>::iterator
Ted Kremenek1e377652010-02-11 02:19:13 +0000985 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
986 delete I->ArgAttrs;
987
John McCall54abf7d2009-11-04 02:18:39 +0000988 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000989}
990
Steve Naroffdac269b2007-08-20 21:31:48 +0000991/// objc-protocol-refs:
992/// '<' identifier-list '>'
993///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000994bool Parser::
John McCalld226f652010-08-21 09:40:31 +0000995ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000996 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
997 bool WarnOnDeclarations,
998 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000999 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001001 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattnere13b9592008-07-26 04:03:38 +00001003 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattnere13b9592008-07-26 04:03:38 +00001005 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001006 if (Tok.is(tok::code_completion)) {
1007 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1008 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001009 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001010 }
1011
Chris Lattnere13b9592008-07-26 04:03:38 +00001012 if (Tok.isNot(tok::identifier)) {
1013 Diag(Tok, diag::err_expected_ident);
1014 SkipUntil(tok::greater);
1015 return true;
1016 }
1017 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1018 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001019 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001020 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattnere13b9592008-07-26 04:03:38 +00001022 if (Tok.isNot(tok::comma))
1023 break;
1024 ConsumeToken();
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattnere13b9592008-07-26 04:03:38 +00001027 // Consume the '>'.
1028 if (Tok.isNot(tok::greater)) {
1029 Diag(Tok, diag::err_expected_greater);
1030 return true;
1031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattnere13b9592008-07-26 04:03:38 +00001033 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Chris Lattnere13b9592008-07-26 04:03:38 +00001035 // Convert the list of protocols identifiers into a list of protocol decls.
1036 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1037 &ProtocolIdents[0], ProtocolIdents.size(),
1038 Protocols);
1039 return false;
1040}
1041
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001042/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1043/// in a decl-specifier-seq, starting at the '<'.
1044void Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
1045 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1046 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1047 SourceLocation LAngleLoc, EndProtoLoc;
1048 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1049 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1050 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1051 LAngleLoc, EndProtoLoc);
1052 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1053 ProtocolLocs.data(), LAngleLoc);
1054 if (EndProtoLoc.isValid())
1055 DS.SetRangeEnd(EndProtoLoc);
1056}
1057
1058
Steve Naroffdac269b2007-08-20 21:31:48 +00001059/// objc-class-instance-variables:
1060/// '{' objc-instance-variable-decl-list[opt] '}'
1061///
1062/// objc-instance-variable-decl-list:
1063/// objc-visibility-spec
1064/// objc-instance-variable-decl ';'
1065/// ';'
1066/// objc-instance-variable-decl-list objc-visibility-spec
1067/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1068/// objc-instance-variable-decl-list ';'
1069///
1070/// objc-visibility-spec:
1071/// @private
1072/// @protected
1073/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001074/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001075///
1076/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001077/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001078///
John McCalld226f652010-08-21 09:40:31 +00001079void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001080 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001081 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001082 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001083 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001084
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001085 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001086
Steve Naroffddbff782007-08-21 21:17:12 +00001087 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Steve Naroffddbff782007-08-21 21:17:12 +00001089 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001091 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Steve Naroffddbff782007-08-21 21:17:12 +00001093 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001094 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001095 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001096 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001097 ConsumeToken();
1098 continue;
1099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Steve Naroffddbff782007-08-21 21:17:12 +00001101 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001102 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001103 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001104
1105 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001106 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001107 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001108 }
1109
Steve Naroff861cf3e2007-08-23 18:16:40 +00001110 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001111 case tok::objc_private:
1112 case tok::objc_public:
1113 case tok::objc_protected:
1114 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001115 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001116 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001117 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001118 default:
1119 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001120 continue;
1121 }
1122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001124 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001125 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001126 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001127 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001128 }
1129
John McCallbdd563e2009-11-03 02:38:08 +00001130 struct ObjCIvarCallback : FieldCallback {
1131 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001132 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001133 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001134 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001135
John McCalld226f652010-08-21 09:40:31 +00001136 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1137 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001138 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1139 }
1140
John McCalld226f652010-08-21 09:40:31 +00001141 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001142 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001143 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001144 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001145 FD.D.getDeclSpec().getSourceRange().getBegin(),
1146 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001147 if (Field)
1148 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001149 return Field;
1150 }
1151 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001152
Chris Lattnere1359422008-04-10 06:46:29 +00001153 // Parse all the comma separated declarators.
1154 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001155 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattnerdf195262007-10-09 17:51:17 +00001157 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001158 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001159 } else {
1160 Diag(Tok, diag::err_expected_semi_decl_list);
1161 // Skip to end of block or statement
1162 SkipUntil(tok::r_brace, true, true);
1163 }
1164 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001165 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001166 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001167 // Call ActOnFields() even if we don't have any decls. This is useful
1168 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001169 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001170 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001171 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001172 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001173}
Steve Naroffdac269b2007-08-20 21:31:48 +00001174
1175/// objc-protocol-declaration:
1176/// objc-protocol-definition
1177/// objc-protocol-forward-reference
1178///
1179/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001180/// @protocol identifier
1181/// objc-protocol-refs[opt]
1182/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001183/// @end
1184///
1185/// objc-protocol-forward-reference:
1186/// @protocol identifier-list ';'
1187///
1188/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001189/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001190/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001191Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001192 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001193 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001194 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1195 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Douglas Gregor083128f2009-11-18 04:49:41 +00001197 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001198 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001199 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001200 }
1201
Chris Lattnerdf195262007-10-09 17:51:17 +00001202 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001203 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001204 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001205 }
1206 // Save the protocol name, then consume it.
1207 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1208 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattnerdf195262007-10-09 17:51:17 +00001210 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001211 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001212 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001213 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001214 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Chris Lattnerdf195262007-10-09 17:51:17 +00001217 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001218 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1219 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1220
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001221 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001222 while (1) {
1223 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001224 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001225 Diag(Tok, diag::err_expected_ident);
1226 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001227 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001228 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001229 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1230 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001231 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Chris Lattnerdf195262007-10-09 17:51:17 +00001233 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001234 break;
1235 }
1236 // Consume the ';'.
1237 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001238 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Steve Naroffe440eb82007-10-10 17:32:04 +00001240 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001241 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001242 ProtocolRefs.size(),
1243 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001246 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001247 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001248
John McCalld226f652010-08-21 09:40:31 +00001249 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001250 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001251 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001252 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1253 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001254 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001255
John McCalld226f652010-08-21 09:40:31 +00001256 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001257 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001258 ProtocolRefs.data(),
1259 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001260 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001261 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001262 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001263 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001264}
Steve Naroffdac269b2007-08-20 21:31:48 +00001265
1266/// objc-implementation:
1267/// objc-class-implementation-prologue
1268/// objc-category-implementation-prologue
1269///
1270/// objc-class-implementation-prologue:
1271/// @implementation identifier objc-superclass[opt]
1272/// objc-class-instance-variables[opt]
1273///
1274/// objc-category-implementation-prologue:
1275/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001276Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001277 SourceLocation atLoc) {
1278 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1279 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1280 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001282 // Code completion after '@implementation'.
1283 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001285 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001286 }
1287
Chris Lattnerdf195262007-10-09 17:51:17 +00001288 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001289 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001290 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001291 }
1292 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001293 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001294 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001295
1296 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001297 // we have a category implementation.
1298 SourceLocation lparenLoc = ConsumeParen();
1299 SourceLocation categoryLoc, rparenLoc;
1300 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001302 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001303 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001304 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001305 }
1306
Chris Lattnerdf195262007-10-09 17:51:17 +00001307 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001308 categoryId = Tok.getIdentifierInfo();
1309 categoryLoc = ConsumeToken();
1310 } else {
1311 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001312 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001313 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001314 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001315 Diag(Tok, diag::err_expected_rparen);
1316 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001317 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001318 }
1319 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001320 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001321 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001322 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001324 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001325 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001326 }
1327 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001328 SourceLocation superClassLoc;
1329 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001330 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001331 // We have a super class
1332 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001333 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001334 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001335 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001336 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001337 superClassId = Tok.getIdentifierInfo();
1338 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001339 }
John McCalld226f652010-08-21 09:40:31 +00001340 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001341 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001342 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Steve Naroff60fccee2007-10-29 21:38:07 +00001344 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001345 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001346 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001347 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001348 PendingObjCImpDecl.push_back(ObjCImpDecl);
1349
John McCalld226f652010-08-21 09:40:31 +00001350 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001351}
Steve Naroff60fccee2007-10-29 21:38:07 +00001352
John McCalld226f652010-08-21 09:40:31 +00001353Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001354 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1355 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001356 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001357 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001358 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001359 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001360 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001361 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001362 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001363 else {
1364 // missing @implementation
1365 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1366 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001367 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001368}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001369
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001370Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1371 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001372 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001373 return Actions.ConvertDeclToDeclGroup(0);
1374 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001375 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001376 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1377}
1378
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001379/// compatibility-alias-decl:
1380/// @compatibility_alias alias-name class-name ';'
1381///
John McCalld226f652010-08-21 09:40:31 +00001382Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001383 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1384 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1385 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001386 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001387 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001388 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001389 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001390 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1391 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001392 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001393 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001394 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001395 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001396 IdentifierInfo *classId = Tok.getIdentifierInfo();
1397 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1398 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001399 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCalld226f652010-08-21 09:40:31 +00001400 return 0;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001401 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001402 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1403 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001404}
1405
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001406/// property-synthesis:
1407/// @synthesize property-ivar-list ';'
1408///
1409/// property-ivar-list:
1410/// property-ivar
1411/// property-ivar-list ',' property-ivar
1412///
1413/// property-ivar:
1414/// identifier
1415/// identifier '=' identifier
1416///
John McCalld226f652010-08-21 09:40:31 +00001417Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001418 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1419 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001420 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Douglas Gregorb328c422009-11-18 19:45:45 +00001422 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001423 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001424 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001425 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001426 }
1427
Douglas Gregorb328c422009-11-18 19:45:45 +00001428 if (Tok.isNot(tok::identifier)) {
1429 Diag(Tok, diag::err_synthesized_property_name);
1430 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001431 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001432 }
1433
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001434 IdentifierInfo *propertyIvar = 0;
1435 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1436 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001437 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001438 // property '=' ivar-name
1439 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001440
1441 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001442 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001443 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001444 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001445 }
1446
Chris Lattnerdf195262007-10-09 17:51:17 +00001447 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001448 Diag(Tok, diag::err_expected_ident);
1449 break;
1450 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001451 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001452 ConsumeToken(); // consume ivar-name
1453 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001455 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001456 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001457 break;
1458 ConsumeToken(); // consume ','
1459 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001460 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001461 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001462 SkipUntil(tok::semi);
1463 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001464 else
1465 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001466 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001467}
1468
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001469/// property-dynamic:
1470/// @dynamic property-list
1471///
1472/// property-list:
1473/// identifier
1474/// property-list ',' identifier
1475///
John McCalld226f652010-08-21 09:40:31 +00001476Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001477 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1478 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1479 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001480 while (true) {
1481 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001482 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001483 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001484 }
1485
1486 if (Tok.isNot(tok::identifier)) {
1487 Diag(Tok, diag::err_expected_ident);
1488 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001489 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001490 }
1491
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001492 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1493 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001494 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001495 propertyId, 0);
1496
Chris Lattnerdf195262007-10-09 17:51:17 +00001497 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001498 break;
1499 ConsumeToken(); // consume ','
1500 }
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001501 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001502 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001503 SkipUntil(tok::semi);
1504 }
1505 else
1506 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001507 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001508}
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001510/// objc-throw-statement:
1511/// throw expression[opt];
1512///
John McCall60d7b3a2010-08-24 06:29:42 +00001513StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1514 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001515 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001516 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001517 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001518 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001519 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001520 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001521 }
1522 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001523 // consume ';'
1524 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001525 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001526}
1527
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001528/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001529/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001530///
John McCall60d7b3a2010-08-24 06:29:42 +00001531StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001532Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001533 ConsumeToken(); // consume synchronized
1534 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001535 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001536 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001537 }
1538 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001539 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001540 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001541 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001542 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001543 }
1544 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001545 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001546 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001547 }
1548 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001549 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001550 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001551 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001552 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001553 // Enter a scope to hold everything within the compound stmt. Compound
1554 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001555 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001556
John McCall60d7b3a2010-08-24 06:29:42 +00001557 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001558
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001559 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001560 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001561 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001562 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001563}
1564
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001565/// objc-try-catch-statement:
1566/// @try compound-statement objc-catch-list[opt]
1567/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1568///
1569/// objc-catch-list:
1570/// @catch ( parameter-declaration ) compound-statement
1571/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1572/// catch-parameter-declaration:
1573/// parameter-declaration
1574/// '...' [OBJC2]
1575///
John McCall60d7b3a2010-08-24 06:29:42 +00001576StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001577 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001578
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001579 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001580 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001581 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001582 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001583 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001584 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001585 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001586 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001587 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001588 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001589 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001590 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001591
Chris Lattnerdf195262007-10-09 17:51:17 +00001592 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001593 // At this point, we need to lookahead to determine if this @ is the start
1594 // of an @catch or @finally. We don't want to consume the @ token if this
1595 // is an @try or @encode or something else.
1596 Token AfterAt = GetLookAheadToken(1);
1597 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1598 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1599 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001600
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001601 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001602 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001603 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001604 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001605 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001606 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001607 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001608 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001609 DeclSpec DS;
1610 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001611 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001612 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001613 // we must accept either 'declarator' or 'abstract-declarator' here.
1614 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1615 ParseDeclarator(ParmDecl);
1616
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001617 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001618 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001619 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001620 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001621 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Steve Naroff93a25952009-04-07 22:56:58 +00001623 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Steve Naroff93a25952009-04-07 22:56:58 +00001625 if (Tok.is(tok::r_paren))
1626 RParenLoc = ConsumeParen();
1627 else // Skip over garbage, until we get to ')'. Eat the ')'.
1628 SkipUntil(tok::r_paren, true, false);
1629
John McCall60d7b3a2010-08-24 06:29:42 +00001630 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001631 if (Tok.is(tok::l_brace))
1632 CatchBody = ParseCompoundStatementBody();
1633 else
1634 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001635 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001636 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001637
John McCall60d7b3a2010-08-24 06:29:42 +00001638 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001639 RParenLoc,
1640 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001641 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001642 if (!Catch.isInvalid())
1643 CatchStmts.push_back(Catch.release());
1644
Steve Naroff64515f32008-02-05 21:27:35 +00001645 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001646 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1647 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001648 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001649 }
1650 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001651 } else {
1652 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001653 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001654 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001655
John McCall60d7b3a2010-08-24 06:29:42 +00001656 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001657 if (Tok.is(tok::l_brace))
1658 FinallyBody = ParseCompoundStatementBody();
1659 else
1660 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001661 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001662 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001663 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001664 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001665 catch_or_finally_seen = true;
1666 break;
1667 }
1668 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001669 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001670 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001671 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001672 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001673
John McCall9ae2f072010-08-23 23:25:46 +00001674 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001675 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001676 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001677}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001678
Steve Naroff3536b442007-09-06 21:24:23 +00001679/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001680///
John McCalld226f652010-08-21 09:40:31 +00001681Decl *Parser::ParseObjCMethodDefinition() {
1682 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001683
John McCallf312b1e2010-08-26 23:41:50 +00001684 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1685 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001687 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001688 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001689 if (ObjCImpDecl) {
1690 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001691 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001692 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001693 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001694 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001695
Steve Naroff409be832007-11-11 19:54:21 +00001696 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001697 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001698 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Steve Naroff409be832007-11-11 19:54:21 +00001700 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1701 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Steve Naroff409be832007-11-11 19:54:21 +00001703 // If we didn't find the '{', bail out.
1704 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001705 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001706 }
Steve Naroff409be832007-11-11 19:54:21 +00001707 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Steve Naroff409be832007-11-11 19:54:21 +00001709 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001710 ParseScope BodyScope(this,
1711 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Steve Naroff409be832007-11-11 19:54:21 +00001713 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001714 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001715 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001716
John McCall60d7b3a2010-08-24 06:29:42 +00001717 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001718
Steve Naroff409be832007-11-11 19:54:21 +00001719 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001720 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001721 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1722 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001723
Steve Naroff32ce8372009-03-02 22:00:56 +00001724 // TODO: Pass argument information.
John McCall9ae2f072010-08-23 23:25:46 +00001725 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Steve Naroff409be832007-11-11 19:54:21 +00001727 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001728 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001729
Steve Naroff71c0a952007-11-13 23:01:27 +00001730 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001731}
Anders Carlsson55085182007-08-21 17:43:55 +00001732
John McCall60d7b3a2010-08-24 06:29:42 +00001733StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001734 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001735 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001736 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001737 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001738 }
1739
1740 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001741 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001742
1743 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001744 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001745
1746 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001747 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001748
John McCall60d7b3a2010-08-24 06:29:42 +00001749 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001750 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001751 // If the expression is invalid, skip ahead to the next semicolon. Not
1752 // doing this opens us up to the possibility of infinite loops if
1753 // ParseExpression does not consume any tokens.
1754 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001755 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001756 }
Chris Lattner5d803162009-12-07 16:33:19 +00001757
Steve Naroff64515f32008-02-05 21:27:35 +00001758 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001759 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001760 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001761}
1762
John McCall60d7b3a2010-08-24 06:29:42 +00001763ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001764 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001765 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001766 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001767 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001768 return ExprError();
1769
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001770 case tok::string_literal: // primary-expression: string-literal
1771 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001772 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001773 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001774 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001775 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001776
Chris Lattner4fef81d2008-08-05 06:19:09 +00001777 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1778 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001779 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001780 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001781 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001782 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001783 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001784 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001785 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001786 }
Anders Carlsson55085182007-08-21 17:43:55 +00001787 }
Anders Carlsson55085182007-08-21 17:43:55 +00001788}
1789
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001790/// \brirg Parse the receiver of an Objective-C++ message send.
1791///
1792/// This routine parses the receiver of a message send in
1793/// Objective-C++ either as a type or as an expression. Note that this
1794/// routine must not be called to parse a send to 'super', since it
1795/// has no way to return such a result.
1796///
1797/// \param IsExpr Whether the receiver was parsed as an expression.
1798///
1799/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1800/// IsExpr is true), the parsed expression. If the receiver was parsed
1801/// as a type (\c IsExpr is false), the parsed type.
1802///
1803/// \returns True if an error occurred during parsing or semantic
1804/// analysis, in which case the arguments do not have valid
1805/// values. Otherwise, returns false for a successful parse.
1806///
1807/// objc-receiver: [C++]
1808/// 'super' [not parsed here]
1809/// expression
1810/// simple-type-specifier
1811/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001812bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001813 InMessageExpressionRAIIObject InMessage(*this, true);
1814
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001815 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1816 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1817 TryAnnotateTypeOrScopeToken();
1818
1819 if (!isCXXSimpleTypeSpecifier()) {
1820 // objc-receiver:
1821 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001822 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001823 if (Receiver.isInvalid())
1824 return true;
1825
1826 IsExpr = true;
1827 TypeOrExpr = Receiver.take();
1828 return false;
1829 }
1830
1831 // objc-receiver:
1832 // typename-specifier
1833 // simple-type-specifier
1834 // expression (that starts with one of the above)
1835 DeclSpec DS;
1836 ParseCXXSimpleTypeSpecifier(DS);
1837
1838 if (Tok.is(tok::l_paren)) {
1839 // If we see an opening parentheses at this point, we are
1840 // actually parsing an expression that starts with a
1841 // function-style cast, e.g.,
1842 //
1843 // postfix-expression:
1844 // simple-type-specifier ( expression-list [opt] )
1845 // typename-specifier ( expression-list [opt] )
1846 //
1847 // Parse the remainder of this case, then the (optional)
1848 // postfix-expression suffix, followed by the (optional)
1849 // right-hand side of the binary expression. We have an
1850 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001851 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001852 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001853 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001854 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001855 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001856 if (Receiver.isInvalid())
1857 return true;
1858
1859 IsExpr = true;
1860 TypeOrExpr = Receiver.take();
1861 return false;
1862 }
1863
1864 // We have a class message. Turn the simple-type-specifier or
1865 // typename-specifier we parsed into a type and parse the
1866 // remainder of the class message.
1867 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001868 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001869 if (Type.isInvalid())
1870 return true;
1871
1872 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001873 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001874 return false;
1875}
1876
Douglas Gregor1b730e82010-05-31 14:40:22 +00001877/// \brief Determine whether the parser is currently referring to a an
1878/// Objective-C message send, using a simplified heuristic to avoid overhead.
1879///
1880/// This routine will only return true for a subset of valid message-send
1881/// expressions.
1882bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001883 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001884 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001885 return GetLookAheadToken(1).is(tok::identifier) &&
1886 GetLookAheadToken(2).is(tok::identifier);
1887}
1888
Douglas Gregor9497a732010-09-16 01:51:54 +00001889bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1890 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1891 InMessageExpression)
1892 return false;
1893
1894
1895 ParsedType Type;
1896
1897 if (Tok.is(tok::annot_typename))
1898 Type = getTypeAnnotation(Tok);
1899 else if (Tok.is(tok::identifier))
1900 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1901 getCurScope());
1902 else
1903 return false;
1904
1905 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1906 const Token &AfterNext = GetLookAheadToken(2);
1907 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1908 if (Tok.is(tok::identifier))
1909 TryAnnotateTypeOrScopeToken();
1910
1911 return Tok.is(tok::annot_typename);
1912 }
1913 }
1914
1915 return false;
1916}
1917
Mike Stump1eb44332009-09-09 15:08:12 +00001918/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001919/// '[' objc-receiver objc-message-args ']'
1920///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001921/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001922/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001923/// expression
1924/// class-name
1925/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001926///
John McCall60d7b3a2010-08-24 06:29:42 +00001927ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001928 assert(Tok.is(tok::l_square) && "'[' expected");
1929 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1930
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001931 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001932 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001933 ConsumeCodeCompletionToken();
1934 SkipUntil(tok::r_square);
1935 return ExprError();
1936 }
1937
Douglas Gregor0fbda682010-09-15 14:51:05 +00001938 InMessageExpressionRAIIObject InMessage(*this, true);
1939
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001940 if (getLang().CPlusPlus) {
1941 // We completely separate the C and C++ cases because C++ requires
1942 // more complicated (read: slower) parsing.
1943
1944 // Handle send to super.
1945 // FIXME: This doesn't benefit from the same typo-correction we
1946 // get in Objective-C.
1947 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001948 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001949 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1950 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001951
1952 // Parse the receiver, which is either a type or an expression.
1953 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00001954 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001955 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1956 SkipUntil(tok::r_square);
1957 return ExprError();
1958 }
1959
1960 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001961 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1962 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001963 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001964
1965 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001966 ParsedType::getFromOpaquePtr(TypeOrExpr),
1967 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001968 }
1969
1970 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001971 IdentifierInfo *Name = Tok.getIdentifierInfo();
1972 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001973 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001974 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001975 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001976 NextToken().is(tok::period),
1977 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001978 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001979 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1980 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001981
John McCallf312b1e2010-08-26 23:41:50 +00001982 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00001983 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001984 SkipUntil(tok::r_square);
1985 return ExprError();
1986 }
1987
Douglas Gregor1569f952010-04-21 20:38:13 +00001988 ConsumeToken(); // the type name
1989
1990 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001991 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001992
John McCallf312b1e2010-08-26 23:41:50 +00001993 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001994 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001995 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001996 }
Chris Lattner699b6612008-01-25 18:59:06 +00001997 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001998
1999 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002000 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002001 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002002 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002003 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002004 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002005
John McCallb3d87482010-08-24 05:47:05 +00002006 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2007 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002008}
Sebastian Redl1d922962008-12-13 15:32:12 +00002009
Douglas Gregor2725ca82010-04-21 19:57:20 +00002010/// \brief Parse the remainder of an Objective-C message following the
2011/// '[' objc-receiver.
2012///
2013/// This routine handles sends to super, class messages (sent to a
2014/// class name), and instance messages (sent to an object), and the
2015/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2016/// ReceiverExpr, respectively. Only one of these parameters may have
2017/// a valid value.
2018///
2019/// \param LBracLoc The location of the opening '['.
2020///
2021/// \param SuperLoc If this is a send to 'super', the location of the
2022/// 'super' keyword that indicates a send to the superclass.
2023///
2024/// \param ReceiverType If this is a class message, the type of the
2025/// class we are sending a message to.
2026///
2027/// \param ReceiverExpr If this is an instance message, the expression
2028/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002029///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002030/// objc-message-args:
2031/// objc-selector
2032/// objc-keywordarg-list
2033///
2034/// objc-keywordarg-list:
2035/// objc-keywordarg
2036/// objc-keywordarg-list objc-keywordarg
2037///
Mike Stump1eb44332009-09-09 15:08:12 +00002038/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002039/// selector-name[opt] ':' objc-keywordexpr
2040///
2041/// objc-keywordexpr:
2042/// nonempty-expr-list
2043///
2044/// nonempty-expr-list:
2045/// assignment-expression
2046/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002047///
John McCall60d7b3a2010-08-24 06:29:42 +00002048ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002049Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002050 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002051 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002052 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002053 InMessageExpressionRAIIObject InMessage(*this, true);
2054
Steve Naroffc4df6d22009-11-07 02:08:14 +00002055 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002056 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002057 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2058 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002059 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002060 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2061 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002062 else
John McCall9ae2f072010-08-23 23:25:46 +00002063 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002064 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002065 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002066 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002067
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002068 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002069 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002070 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002071
Anders Carlssonff975cf2009-02-14 18:21:46 +00002072 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Steve Naroff68d331a2007-09-27 14:38:14 +00002074 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002075 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002076
Chris Lattnerdf195262007-10-09 17:51:17 +00002077 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002078 while (1) {
2079 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002080 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002081
Chris Lattnerdf195262007-10-09 17:51:17 +00002082 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002083 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002084 // We must manually skip to a ']', otherwise the expression skipper will
2085 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2086 // the enclosing expression.
2087 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002088 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002089 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002090
Steve Naroff68d331a2007-09-27 14:38:14 +00002091 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002092 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002093
2094 if (Tok.is(tok::code_completion)) {
2095 if (SuperLoc.isValid())
2096 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2097 KeyIdents.data(),
2098 KeyIdents.size(),
2099 /*AtArgumentEpression=*/true);
2100 else if (ReceiverType)
2101 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2102 KeyIdents.data(),
2103 KeyIdents.size(),
2104 /*AtArgumentEpression=*/true);
2105 else
2106 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2107 KeyIdents.data(),
2108 KeyIdents.size(),
2109 /*AtArgumentEpression=*/true);
2110
2111 ConsumeCodeCompletionToken();
2112 SkipUntil(tok::r_square);
2113 return ExprError();
2114 }
2115
John McCall60d7b3a2010-08-24 06:29:42 +00002116 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002117 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002118 // We must manually skip to a ']', otherwise the expression skipper will
2119 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2120 // the enclosing expression.
2121 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002122 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002123 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002124
Steve Naroff37387c92007-09-17 20:25:27 +00002125 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002126 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002127
Douglas Gregord3c68542009-11-19 01:08:35 +00002128 // Code completion after each argument.
2129 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002130 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002131 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002132 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002133 KeyIdents.size(),
2134 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002135 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002136 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002137 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002138 KeyIdents.size(),
2139 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002140 else
John McCall9ae2f072010-08-23 23:25:46 +00002141 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002142 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002143 KeyIdents.size(),
2144 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002145 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002146 SkipUntil(tok::r_square);
2147 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002148 }
2149
Steve Naroff37387c92007-09-17 20:25:27 +00002150 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002151 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002152 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002153 break;
2154 // We have a selector or a colon, continue parsing.
2155 }
2156 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002157 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002158 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002159 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002160 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002161 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002162 // We must manually skip to a ']', otherwise the expression skipper will
2163 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2164 // the enclosing expression.
2165 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002166 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002167 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002168
Steve Naroff49f109c2007-11-15 13:05:42 +00002169 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002170 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002171 }
2172 } else if (!selIdent) {
2173 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002174
Chris Lattner4fef81d2008-08-05 06:19:09 +00002175 // We must manually skip to a ']', otherwise the expression skipper will
2176 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2177 // the enclosing expression.
2178 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002179 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002180 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002181
Chris Lattnerdf195262007-10-09 17:51:17 +00002182 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002183 if (Tok.is(tok::identifier))
2184 Diag(Tok, diag::err_expected_colon);
2185 else
2186 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002187 // We must manually skip to a ']', otherwise the expression skipper will
2188 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2189 // the enclosing expression.
2190 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002191 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002192 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002193
Chris Lattner699b6612008-01-25 18:59:06 +00002194 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002195
Steve Naroff29238a02007-10-05 18:42:47 +00002196 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002197 if (nKeys == 0)
2198 KeyIdents.push_back(selIdent);
2199 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002200
Douglas Gregor2725ca82010-04-21 19:57:20 +00002201 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002202 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002203 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002204 MultiExprArg(Actions,
2205 KeyExprs.take(),
2206 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002207 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002208 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002209 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002210 MultiExprArg(Actions,
2211 KeyExprs.take(),
2212 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002213 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002214 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002215 MultiExprArg(Actions,
2216 KeyExprs.take(),
2217 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002218}
2219
John McCall60d7b3a2010-08-24 06:29:42 +00002220ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2221 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002222 if (Res.isInvalid()) return move(Res);
2223
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002224 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2225 // expressions. At this point, we know that the only valid thing that starts
2226 // with '@' is an @"".
2227 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002228 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002229 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002230 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002231
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002232 while (Tok.is(tok::at)) {
2233 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002234
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002235 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002236 if (!isTokenStringLiteral())
2237 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002238
John McCall60d7b3a2010-08-24 06:29:42 +00002239 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002240 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002241 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002242
Sebastian Redleffa8d12008-12-10 00:02:53 +00002243 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002244 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002245
2246 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2247 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002248}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002249
2250/// objc-encode-expression:
2251/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002252ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002253Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002254 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002255
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002256 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002257
Chris Lattner4fef81d2008-08-05 06:19:09 +00002258 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002259 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2260
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002261 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002262
Douglas Gregor809070a2009-02-18 17:45:20 +00002263 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002264
Anders Carlsson4988ae32007-08-23 15:31:37 +00002265 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002266
Douglas Gregor809070a2009-02-18 17:45:20 +00002267 if (Ty.isInvalid())
2268 return ExprError();
2269
Mike Stump1eb44332009-09-09 15:08:12 +00002270 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002271 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002272}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002273
2274/// objc-protocol-expression
2275/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002276ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002277Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002278 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002279
Chris Lattner4fef81d2008-08-05 06:19:09 +00002280 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002281 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2282
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002283 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002284
Chris Lattner4fef81d2008-08-05 06:19:09 +00002285 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002286 return ExprError(Diag(Tok, diag::err_expected_ident));
2287
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002288 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002289 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002290
Anders Carlsson4988ae32007-08-23 15:31:37 +00002291 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002292
Sebastian Redl1d922962008-12-13 15:32:12 +00002293 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2294 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002295}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002296
2297/// objc-selector-expression
2298/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002299ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002300 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002301
Chris Lattner4fef81d2008-08-05 06:19:09 +00002302 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002303 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2304
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002305 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002306 SourceLocation LParenLoc = ConsumeParen();
2307 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002308
2309 if (Tok.is(tok::code_completion)) {
2310 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2311 KeyIdents.size());
2312 ConsumeCodeCompletionToken();
2313 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2314 return ExprError();
2315 }
2316
Chris Lattner2fc5c242009-04-11 18:13:45 +00002317 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002318 if (!SelIdent && // missing selector name.
2319 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002320 return ExprError(Diag(Tok, diag::err_expected_ident));
2321
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002322 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002323 unsigned nColons = 0;
2324 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002325 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002326 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2327 ++nColons;
2328 KeyIdents.push_back(0);
2329 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002330 return ExprError(Diag(Tok, diag::err_expected_colon));
2331
Chris Lattner5add7542010-08-27 22:32:41 +00002332 ++nColons;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002333 ConsumeToken(); // Eat the ':'.
2334 if (Tok.is(tok::r_paren))
2335 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002336
2337 if (Tok.is(tok::code_completion)) {
2338 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2339 KeyIdents.size());
2340 ConsumeCodeCompletionToken();
2341 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2342 return ExprError();
2343 }
2344
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002345 // Check for another keyword selector.
2346 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002347 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002348 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002349 if (!SelIdent && Tok.isNot(tok::colon))
2350 break;
2351 }
Steve Naroff887407e2007-12-05 22:21:29 +00002352 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002353 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002354 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002355 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2356 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002357 }