blob: d18efac5e2c1eb6baa2ac60d210da7d28117c328 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000020#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000021using namespace clang;
22
23
Chris Lattner3a907162008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroffe0933392007-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 McCall48871652010-08-21 09:40:31 +000032Decl *Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000034
Douglas Gregorf48706c2009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +000038 }
39
Steve Naroff7c348172007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-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 McCall48871652010-08-21 09:40:31 +000060 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000061 }
62}
63
64///
Mike Stump11289f42009-09-09 15:08:12 +000065/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000066/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000067///
John McCall48871652010-08-21 09:40:31 +000068Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000069 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000070 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000071 llvm::SmallVector<SourceLocation, 8> ClassLocs;
72
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattnerda59c2f2006-11-05 02:08:13 +000074 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000075 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000076 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000078 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000079 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000081 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000082 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattner0ef13522007-10-09 17:51:17 +000084 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000085 break;
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattnerda59c2f2006-11-05 02:08:13 +000087 ConsumeToken();
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattnerda59c2f2006-11-05 02:08:13 +000090 // Consume the ';'.
91 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCall48871652010-08-21 09:40:31 +000092 return 0;
Mike Stump11289f42009-09-09 15:08:12 +000093
Ted Kremeneka26da852009-11-17 23:12:20 +000094 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
95 ClassLocs.data(),
96 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000097}
98
Steve Naroff1eb1ad62007-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 Stump11289f42009-09-09 15:08:12 +0000105/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000107/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000108/// objc-interface-decl-list
109/// @end
110///
111/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000112/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-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 McCall48871652010-08-21 09:40:31 +0000127Decl *Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000128 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000129 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000130 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
131 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregor49c22a72009-11-18 16:26:39 +0000133 // Code completion after '@interface'.
134 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000135 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000136 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000137 }
138
Chris Lattner0ef13522007-10-09 17:51:17 +0000139 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000140 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000141 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000143
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000145 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000146 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000147 if (Tok.is(tok::l_paren) &&
148 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000149 SourceLocation lparenLoc = ConsumeParen();
150 SourceLocation categoryLoc, rparenLoc;
151 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000152 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000153 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000154 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000155 }
156
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000157 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000158 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000159 categoryId = Tok.getIdentifierInfo();
160 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000161 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000162 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000163 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000164 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000166 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000167 Diag(Tok, diag::err_expected_rparen);
168 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +0000169 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000170 }
171 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000172 // Next, we need to check for any protocol references.
173 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +0000174 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000175 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
176 if (Tok.is(tok::less) &&
177 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000178 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000179 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000181 if (attrList) // categories don't support attributes.
182 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000183
John McCall48871652010-08-21 09:40:31 +0000184 Decl *CategoryType =
Fariborz Jahanian65654df2010-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 Jahanian4c172c62010-02-22 23:04:20 +0000193 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
194 atLoc);
195
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000196 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
197 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000198 }
199 // Parse a class interface.
200 IdentifierInfo *superClassId = 0;
201 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000202
Chris Lattner0ef13522007-10-09 17:51:17 +0000203 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000204 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000205
206 // Code completion of superclass names.
207 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000208 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000209 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000210 }
211
Chris Lattner0ef13522007-10-09 17:51:17 +0000212 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000213 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000214 return 0;
Steve Naroff1eb1ad62007-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 McCall48871652010-08-21 09:40:31 +0000220 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000221 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000223 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000224 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
225 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000226 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000227
John McCall48871652010-08-21 09:40:31 +0000228 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000229 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000230 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000231 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000232 ProtocolLocs.data(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000233 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner0ef13522007-10-09 17:51:17 +0000235 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000236 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000237
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000238 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000239 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000240}
241
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000246 Decl *IDecl;
247 llvm::SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000248 ObjCDeclSpec &OCDS;
249 SourceLocation AtLoc;
250 tok::ObjCKeywordKind MethodImplKind;
251
John McCall48871652010-08-21 09:40:31 +0000252 ObjCPropertyCallback(Parser &P, Decl *IDecl,
253 llvm::SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000260 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000264 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000265 }
266 if (FD.BitfieldSize) {
267 P.Diag(AtLoc, diag::err_objc_property_bitfield)
268 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000269 return 0;
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000287 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000288 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCall064d77b2009-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 Naroff1eb1ad62007-08-20 21:31:48 +0000299/// objc-interface-decl-list:
300/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000301/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000302/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000303/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000304/// objc-interface-decl-list declaration
305/// objc-interface-decl-list ';'
306///
Steve Naroff99264b42007-08-22 16:35:03 +0000307/// objc-method-requirement: [OBJC2]
308/// @required
309/// @optional
310///
John McCall48871652010-08-21 09:40:31 +0000311void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000312 tok::ObjCKeywordKind contextKey) {
John McCall48871652010-08-21 09:40:31 +0000313 llvm::SmallVector<Decl *, 32> allMethods;
314 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000315 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000316 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Ted Kremenekc7c64312010-01-07 01:20:12 +0000318 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000319
Steve Naroff99264b42007-08-22 16:35:03 +0000320 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000321 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000322 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000323 Decl *methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000324 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000325 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000326 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
327 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000328 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
329 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000330 continue;
331 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000332 if (Tok.is(tok::l_paren)) {
333 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000334 ParseObjCMethodDecl(Tok.getLocation(),
335 tok::minus,
336 interfaceDecl,
337 MethodImplKind);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000338 continue;
339 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000340 // Ignore excess semicolons.
341 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000342 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000343 continue;
344 }
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattnerda9fb152008-10-20 06:10:06 +0000346 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000347 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000348 break;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Douglas Gregorf1934162010-01-13 21:24:21 +0000350 // Code completion within an Objective-C interface.
351 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000352 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000353 ObjCImpDecl? Sema::PCC_ObjCImplementation
354 : Sema::PCC_ObjCInterface);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000355 ConsumeCodeCompletionToken();
Douglas Gregorf1934162010-01-13 21:24:21 +0000356 }
357
Chris Lattner038a3e32008-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 Lattnerc7c9ab72009-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 Stump11289f42009-09-09 15:08:12 +0000365
Steve Narofff1bc45b2007-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.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000368 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000369 continue;
370 }
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattner038a3e32008-10-20 05:46:22 +0000372 // Otherwise, we have an @ directive, eat the @.
373 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000374 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000375 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000376 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000377 break;
378 }
379
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000380 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000381
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000382 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000383 AtEnd.setBegin(AtLoc);
384 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000385 break;
Douglas Gregor00a0cf72010-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 Lattnerda9fb152008-10-20 06:10:06 +0000390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerda9fb152008-10-20 06:10:06 +0000392 // Eat the identifier.
393 ConsumeToken();
394
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000395 switch (DirectiveKind) {
396 default:
Chris Lattnerda9fb152008-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 Lattner76619232008-10-20 07:22:18 +0000401 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000402 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000403 SkipUntil(tok::r_brace, tok::at);
404 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000405
406 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000407 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000408 Diag(Tok, diag::err_objc_missing_end);
409 ConsumeToken();
410 break;
411
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000412 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000413 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000414 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000415 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000416 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000417 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000419 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000420 break;
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000422 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000423 if (!getLang().ObjC2)
424 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
425
Chris Lattner038a3e32008-10-20 05:46:22 +0000426 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000427 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000428 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000429 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
430 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000431
John McCall064d77b2009-12-03 22:31:13 +0000432 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
433 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000434
Chris Lattner038a3e32008-10-20 05:46:22 +0000435 // Parse all the comma separated declarators.
436 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000437 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000438
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000439 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
440 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000441 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000442 }
Steve Naroff99264b42007-08-22 16:35:03 +0000443 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000444
445 // We break out of the big loop in two cases: when we see @end or when we see
446 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000447 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000448 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000449 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000450 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000451 ConsumeToken(); // the "end" identifier
452 else
453 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000455 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000456 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000457 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000458 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000459 allProperties.data(), allProperties.size(),
460 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000461}
462
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000463/// Parse property attribute declarations.
464///
465/// property-attr-decl: '(' property-attrlist ')'
466/// property-attrlist:
467/// property-attribute
468/// property-attrlist ',' property-attribute
469/// property-attribute:
470/// getter '=' identifier
471/// setter '=' identifier ':'
472/// readonly
473/// readwrite
474/// assign
475/// retain
476/// copy
477/// nonatomic
478///
John McCall48871652010-08-21 09:40:31 +0000479void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl,
480 Decl **Methods,
Douglas Gregorc8537c52009-11-19 07:41:15 +0000481 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000482 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000483 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000485 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000486 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000487 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000488 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000489 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000490 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattner76619232008-10-20 07:22:18 +0000492 // If this is not an identifier at all, bail out early.
493 if (II == 0) {
494 MatchRHSPunctuation(tok::r_paren, LHSLoc);
495 return;
496 }
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattner1db33542008-10-20 07:37:22 +0000498 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000499
Chris Lattner68e48682008-11-20 04:42:34 +0000500 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000502 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000504 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000506 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000508 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000510 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000512 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000513 bool IsSetter = II->getNameStart()[0] == 's';
514
Chris Lattner825bca12008-10-20 07:39:53 +0000515 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000516 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
517 diag::err_objc_expected_equal_for_getter;
518
519 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000520 return;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Douglas Gregorc8537c52009-11-19 07:41:15 +0000522 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000523 if (IsSetter)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000524 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
Douglas Gregorc8537c52009-11-19 07:41:15 +0000525 Methods, NumMethods);
526 else
Douglas Gregor0be31a22010-07-02 17:43:08 +0000527 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
Douglas Gregorc8537c52009-11-19 07:41:15 +0000528 Methods, NumMethods);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000529 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000530 }
531
Anders Carlssonfe15a782010-10-02 17:45:21 +0000532
533 SourceLocation SelLoc;
534 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
535
536 if (!SelIdent) {
537 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
538 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000539 SkipUntil(tok::r_paren);
540 return;
541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Anders Carlssonfe15a782010-10-02 17:45:21 +0000543 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000544 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000545 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000546
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000547 if (ExpectAndConsume(tok::colon,
548 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000549 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000550 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000551 } else {
552 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000553 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000554 }
Chris Lattner825bca12008-10-20 07:39:53 +0000555 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000556 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000557 SkipUntil(tok::r_paren);
558 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner1db33542008-10-20 07:37:22 +0000561 if (Tok.isNot(tok::comma))
562 break;
Mike Stump11289f42009-09-09 15:08:12 +0000563
Chris Lattner1db33542008-10-20 07:37:22 +0000564 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattner1db33542008-10-20 07:37:22 +0000567 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000568}
569
Steve Naroff09bf8152007-09-06 21:24:23 +0000570/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000571/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000572/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000573///
574/// objc-instance-method: '-'
575/// objc-class-method: '+'
576///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000577/// objc-method-attributes: [OBJC2]
578/// __attribute__((deprecated))
579///
John McCall48871652010-08-21 09:40:31 +0000580Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
581 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000582 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000583
Mike Stump11289f42009-09-09 15:08:12 +0000584 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000585 SourceLocation mLoc = ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000586 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000587 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000588 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000589 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000590}
591
592/// objc-selector:
593/// identifier
594/// one of
595/// enum struct union if else while do for switch case default
596/// break continue return goto asm sizeof typeof __alignof
597/// unsigned long const short volatile signed restrict _Complex
598/// in out inout bycopy byref oneway int char float double void _Bool
599///
Chris Lattner4f472a32009-04-11 18:13:45 +0000600IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000601
Chris Lattner5700fab2007-10-07 02:00:24 +0000602 switch (Tok.getKind()) {
603 default:
604 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000605 case tok::ampamp:
606 case tok::ampequal:
607 case tok::amp:
608 case tok::pipe:
609 case tok::tilde:
610 case tok::exclaim:
611 case tok::exclaimequal:
612 case tok::pipepipe:
613 case tok::pipeequal:
614 case tok::caret:
615 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000616 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000617 if (isalpha(ThisTok[0])) {
618 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
619 Tok.setKind(tok::identifier);
620 SelectorLoc = ConsumeToken();
621 return II;
622 }
623 return 0;
624 }
625
Chris Lattner5700fab2007-10-07 02:00:24 +0000626 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000627 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000628 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000629 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000630 case tok::kw_break:
631 case tok::kw_case:
632 case tok::kw_catch:
633 case tok::kw_char:
634 case tok::kw_class:
635 case tok::kw_const:
636 case tok::kw_const_cast:
637 case tok::kw_continue:
638 case tok::kw_default:
639 case tok::kw_delete:
640 case tok::kw_do:
641 case tok::kw_double:
642 case tok::kw_dynamic_cast:
643 case tok::kw_else:
644 case tok::kw_enum:
645 case tok::kw_explicit:
646 case tok::kw_export:
647 case tok::kw_extern:
648 case tok::kw_false:
649 case tok::kw_float:
650 case tok::kw_for:
651 case tok::kw_friend:
652 case tok::kw_goto:
653 case tok::kw_if:
654 case tok::kw_inline:
655 case tok::kw_int:
656 case tok::kw_long:
657 case tok::kw_mutable:
658 case tok::kw_namespace:
659 case tok::kw_new:
660 case tok::kw_operator:
661 case tok::kw_private:
662 case tok::kw_protected:
663 case tok::kw_public:
664 case tok::kw_register:
665 case tok::kw_reinterpret_cast:
666 case tok::kw_restrict:
667 case tok::kw_return:
668 case tok::kw_short:
669 case tok::kw_signed:
670 case tok::kw_sizeof:
671 case tok::kw_static:
672 case tok::kw_static_cast:
673 case tok::kw_struct:
674 case tok::kw_switch:
675 case tok::kw_template:
676 case tok::kw_this:
677 case tok::kw_throw:
678 case tok::kw_true:
679 case tok::kw_try:
680 case tok::kw_typedef:
681 case tok::kw_typeid:
682 case tok::kw_typename:
683 case tok::kw_typeof:
684 case tok::kw_union:
685 case tok::kw_unsigned:
686 case tok::kw_using:
687 case tok::kw_virtual:
688 case tok::kw_void:
689 case tok::kw_volatile:
690 case tok::kw_wchar_t:
691 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000692 case tok::kw__Bool:
693 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000694 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000695 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000696 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000697 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000698 }
Steve Naroff99264b42007-08-22 16:35:03 +0000699}
700
Fariborz Jahanian83615522008-01-02 22:54:34 +0000701/// objc-for-collection-in: 'in'
702///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000703bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000704 // FIXME: May have to do additional look-ahead to only allow for
705 // valid tokens following an 'in'; such as an identifier, unary operators,
706 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000707 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000708 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000709}
710
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000711/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000712/// qualifier list and builds their bitmask representation in the input
713/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000714///
715/// objc-type-qualifiers:
716/// objc-type-qualifier
717/// objc-type-qualifiers objc-type-qualifier
718///
Douglas Gregor99fa2642010-08-24 01:06:58 +0000719void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattner61511e12007-12-12 06:56:32 +0000720 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000721 if (Tok.is(tok::code_completion)) {
722 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
723 ConsumeCodeCompletionToken();
724 }
725
Chris Lattner5e530bc2007-12-27 19:57:00 +0000726 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000727 return;
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattner61511e12007-12-12 06:56:32 +0000729 const IdentifierInfo *II = Tok.getIdentifierInfo();
730 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000731 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000732 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000733
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000734 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000735 switch (i) {
736 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000737 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
738 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
739 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
740 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
741 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
742 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000743 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000744 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000745 ConsumeToken();
746 II = 0;
747 break;
748 }
Mike Stump11289f42009-09-09 15:08:12 +0000749
Chris Lattner61511e12007-12-12 06:56:32 +0000750 // If this wasn't a recognized qualifier, bail out.
751 if (II) return;
752 }
753}
754
755/// objc-type-name:
756/// '(' objc-type-qualifiers[opt] type-name ')'
757/// '(' objc-type-qualifiers[opt] ')'
758///
John McCallba7bf592010-08-24 05:47:05 +0000759ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000760 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattnerb7954432008-10-22 03:52:06 +0000762 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000763 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000764
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000765 // Parse type qualifiers, in, inout, etc.
Douglas Gregor99fa2642010-08-24 01:06:58 +0000766 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000767
John McCallba7bf592010-08-24 05:47:05 +0000768 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000769 if (isTypeSpecifierQualifier()) {
770 TypeResult TypeSpec = ParseTypeName();
771 if (!TypeSpec.isInvalid())
772 Ty = TypeSpec.get();
773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Steve Naroff90255b42008-10-21 14:15:04 +0000775 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000776 ConsumeParen();
777 else if (Tok.getLocation() == TypeStartLoc) {
778 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000779 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000780 SkipUntil(tok::r_paren);
781 } else {
782 // Otherwise, we found *something*, but didn't get a ')' in the right
783 // place. Emit an error then return what we have as the type.
784 MatchRHSPunctuation(tok::r_paren, LParenLoc);
785 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000786 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000787}
788
789/// objc-method-decl:
790/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000791/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000792/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000793/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000794///
795/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000796/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000797/// objc-keyword-selector objc-keyword-decl
798///
799/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000800/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
801/// objc-selector ':' objc-keyword-attributes[opt] identifier
802/// ':' objc-type-name objc-keyword-attributes[opt] identifier
803/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000804///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000805/// objc-parmlist:
806/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000807///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000808/// objc-parms:
809/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000810///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000811/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000812/// , ...
813///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000814/// objc-keyword-attributes: [OBJC2]
815/// __attribute__((unused))
816///
John McCall48871652010-08-21 09:40:31 +0000817Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000818 tok::TokenKind mType,
819 Decl *IDecl,
820 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000821 ParsingDeclRAIIObject PD(*this);
822
Douglas Gregor636a61e2010-04-07 00:21:17 +0000823 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000824 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallba7bf592010-08-24 05:47:05 +0000825 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000826 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000827 }
828
Chris Lattner2ebb1782008-08-23 01:48:03 +0000829 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000830 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000831 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000832 if (Tok.is(tok::l_paren))
Douglas Gregor99fa2642010-08-24 01:06:58 +0000833 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump11289f42009-09-09 15:08:12 +0000834
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000835 // If attributes exist before the method, parse them.
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000836 AttributeList *MethodAttrs = 0;
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000837 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000838 MethodAttrs = ParseGNUAttributes();
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000839
Douglas Gregor636a61e2010-04-07 00:21:17 +0000840 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000841 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregor636a61e2010-04-07 00:21:17 +0000842 ReturnType, IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000843 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000844 }
845
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000846 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000847 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000848 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000849
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000850 // An unnamed colon is valid.
851 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000852 Diag(Tok, diag::err_expected_selector_for_method)
853 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000854 // Skip until we get a ; or {}.
855 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000856 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000857 }
Mike Stump11289f42009-09-09 15:08:12 +0000858
Fariborz Jahanian60462092010-04-08 00:30:06 +0000859 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000860 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000861 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000862 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000863 MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner5700fab2007-10-07 02:00:24 +0000865 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000866 Decl *Result
John McCall28a6aea2009-11-04 02:18:39 +0000867 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000868 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000869 0,
870 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000871 MethodAttrs, MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000872 PD.complete(Result);
873 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000874 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000875
Steve Narofff73590d2007-09-27 14:38:14 +0000876 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallfaf5fb42010-08-26 23:41:50 +0000877 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner5700fab2007-10-07 02:00:24 +0000879 while (1) {
John McCallfaf5fb42010-08-26 23:41:50 +0000880 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000881
Chris Lattner5700fab2007-10-07 02:00:24 +0000882 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000883 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000884 Diag(Tok, diag::err_expected_colon);
885 break;
886 }
887 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000888
John McCallba7bf592010-08-24 05:47:05 +0000889 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000890 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor99fa2642010-08-24 01:06:58 +0000891 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000892
Chris Lattner5700fab2007-10-07 02:00:24 +0000893 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000894 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000895 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000896 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000897
Douglas Gregor45879692010-07-08 23:37:41 +0000898 // Code completion for the next piece of the selector.
899 if (Tok.is(tok::code_completion)) {
900 ConsumeCodeCompletionToken();
901 KeyIdents.push_back(SelIdent);
902 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
903 mType == tok::minus,
904 /*AtParameterName=*/true,
905 ReturnType,
906 KeyIdents.data(),
907 KeyIdents.size());
908 KeyIdents.pop_back();
909 break;
910 }
911
Chris Lattner0ef13522007-10-09 17:51:17 +0000912 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000913 Diag(Tok, diag::err_expected_ident); // missing argument name.
914 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000915 }
Mike Stump11289f42009-09-09 15:08:12 +0000916
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000917 ArgInfo.Name = Tok.getIdentifierInfo();
918 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000919 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000920
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000921 ArgInfos.push_back(ArgInfo);
922 KeyIdents.push_back(SelIdent);
923
Douglas Gregor95887f92010-07-08 23:20:03 +0000924 // Code completion for the next piece of the selector.
925 if (Tok.is(tok::code_completion)) {
926 ConsumeCodeCompletionToken();
927 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
928 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000929 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000930 ReturnType,
931 KeyIdents.data(),
932 KeyIdents.size());
933 break;
934 }
935
Chris Lattner5700fab2007-10-07 02:00:24 +0000936 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000937 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000938 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000939 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000940 break;
941 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000942 }
Mike Stump11289f42009-09-09 15:08:12 +0000943
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000944 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattner5700fab2007-10-07 02:00:24 +0000946 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000947 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000948 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000949 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000950 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000951 ConsumeToken();
952 break;
953 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000954 DeclSpec DS;
955 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000956 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000957 Declarator ParmDecl(DS, Declarator::PrototypeContext);
958 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000959 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +0000960 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000961 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
962 ParmDecl.getIdentifierLoc(),
963 Param,
964 0));
965
Chris Lattner5700fab2007-10-07 02:00:24 +0000966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Cameron Esfahanif6c73c42010-10-12 00:21:25 +0000968 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000969 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000970 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000971 MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +0000972
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000973 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +0000974 return 0;
Chris Lattner5700fab2007-10-07 02:00:24 +0000975 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
976 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +0000977 Decl *Result
John McCall28a6aea2009-11-04 02:18:39 +0000978 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000979 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000980 &ArgInfos[0],
981 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000982 MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000983 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000984 PD.complete(Result);
985 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000986}
987
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000988/// objc-protocol-refs:
989/// '<' identifier-list '>'
990///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000991bool Parser::
John McCall48871652010-08-21 09:40:31 +0000992ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000993 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
994 bool WarnOnDeclarations,
995 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000996 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000997
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000998 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattner3bbae002008-07-26 04:03:38 +00001000 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001001
Chris Lattner3bbae002008-07-26 04:03:38 +00001002 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001003 if (Tok.is(tok::code_completion)) {
1004 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1005 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001006 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +00001007 }
1008
Chris Lattner3bbae002008-07-26 04:03:38 +00001009 if (Tok.isNot(tok::identifier)) {
1010 Diag(Tok, diag::err_expected_ident);
1011 SkipUntil(tok::greater);
1012 return true;
1013 }
1014 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1015 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001016 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001017 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001018
Chris Lattner3bbae002008-07-26 04:03:38 +00001019 if (Tok.isNot(tok::comma))
1020 break;
1021 ConsumeToken();
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner3bbae002008-07-26 04:03:38 +00001024 // Consume the '>'.
1025 if (Tok.isNot(tok::greater)) {
1026 Diag(Tok, diag::err_expected_greater);
1027 return true;
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner3bbae002008-07-26 04:03:38 +00001030 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001031
Chris Lattner3bbae002008-07-26 04:03:38 +00001032 // Convert the list of protocols identifiers into a list of protocol decls.
1033 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1034 &ProtocolIdents[0], ProtocolIdents.size(),
1035 Protocols);
1036 return false;
1037}
1038
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001039/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1040/// in a decl-specifier-seq, starting at the '<'.
1041void Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
1042 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1043 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1044 SourceLocation LAngleLoc, EndProtoLoc;
1045 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1046 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1047 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1048 LAngleLoc, EndProtoLoc);
1049 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1050 ProtocolLocs.data(), LAngleLoc);
1051 if (EndProtoLoc.isValid())
1052 DS.SetRangeEnd(EndProtoLoc);
1053}
1054
1055
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001056/// objc-class-instance-variables:
1057/// '{' objc-instance-variable-decl-list[opt] '}'
1058///
1059/// objc-instance-variable-decl-list:
1060/// objc-visibility-spec
1061/// objc-instance-variable-decl ';'
1062/// ';'
1063/// objc-instance-variable-decl-list objc-visibility-spec
1064/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1065/// objc-instance-variable-decl-list ';'
1066///
1067/// objc-visibility-spec:
1068/// @private
1069/// @protected
1070/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001071/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001072///
1073/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001074/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001075///
John McCall48871652010-08-21 09:40:31 +00001076void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001077 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001078 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001079 assert(Tok.is(tok::l_brace) && "expected {");
John McCall48871652010-08-21 09:40:31 +00001080 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001081
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001082 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001083
Steve Naroff00433d32007-08-21 21:17:12 +00001084 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001085
Steve Naroff00433d32007-08-21 21:17:12 +00001086 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001087 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001088 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001089
Steve Naroff00433d32007-08-21 21:17:12 +00001090 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001091 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001092 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001093 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001094 ConsumeToken();
1095 continue;
1096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
Steve Naroff00433d32007-08-21 21:17:12 +00001098 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001099 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001100 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001101
1102 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001103 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001104 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001105 }
1106
Steve Naroff7c348172007-08-23 18:16:40 +00001107 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001108 case tok::objc_private:
1109 case tok::objc_public:
1110 case tok::objc_protected:
1111 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001112 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001113 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001114 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001115 default:
1116 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001117 continue;
1118 }
1119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Douglas Gregor48d46252010-01-13 21:54:15 +00001121 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001122 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001123 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001124 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001125 }
1126
John McCallcfefb6d2009-11-03 02:38:08 +00001127 struct ObjCIvarCallback : FieldCallback {
1128 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001129 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001130 tok::ObjCKeywordKind visibility;
John McCall48871652010-08-21 09:40:31 +00001131 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001132
John McCall48871652010-08-21 09:40:31 +00001133 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1134 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001135 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1136 }
1137
John McCall48871652010-08-21 09:40:31 +00001138 Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001139 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001140 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001141 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001142 FD.D.getDeclSpec().getSourceRange().getBegin(),
1143 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001144 if (Field)
1145 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001146 return Field;
1147 }
1148 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001149
Chris Lattnera12405b2008-04-10 06:46:29 +00001150 // Parse all the comma separated declarators.
1151 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001152 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner0ef13522007-10-09 17:51:17 +00001154 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001155 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001156 } else {
1157 Diag(Tok, diag::err_expected_semi_decl_list);
1158 // Skip to end of block or statement
1159 SkipUntil(tok::r_brace, true, true);
1160 }
1161 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001162 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001163 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001164 // Call ActOnFields() even if we don't have any decls. This is useful
1165 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001166 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001167 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001168 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001169 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001170}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001171
1172/// objc-protocol-declaration:
1173/// objc-protocol-definition
1174/// objc-protocol-forward-reference
1175///
1176/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001177/// @protocol identifier
1178/// objc-protocol-refs[opt]
1179/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001180/// @end
1181///
1182/// objc-protocol-forward-reference:
1183/// @protocol identifier-list ';'
1184///
1185/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001186/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001187/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001188Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001189 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001190 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001191 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1192 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001193
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001194 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001195 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001196 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001197 }
1198
Chris Lattner0ef13522007-10-09 17:51:17 +00001199 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001200 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001201 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001202 }
1203 // Save the protocol name, then consume it.
1204 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1205 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner0ef13522007-10-09 17:51:17 +00001207 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001208 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001209 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001210 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001211 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001212 }
Mike Stump11289f42009-09-09 15:08:12 +00001213
Chris Lattner0ef13522007-10-09 17:51:17 +00001214 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001215 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1216 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1217
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001218 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001219 while (1) {
1220 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001221 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001222 Diag(Tok, diag::err_expected_ident);
1223 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001224 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001225 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001226 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1227 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001228 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001229
Chris Lattner0ef13522007-10-09 17:51:17 +00001230 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001231 break;
1232 }
1233 // Consume the ';'.
1234 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001235 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001236
Steve Naroff93eb5f12007-10-10 17:32:04 +00001237 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001238 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001239 ProtocolRefs.size(),
1240 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001241 }
Mike Stump11289f42009-09-09 15:08:12 +00001242
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001243 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001244 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001245
John McCall48871652010-08-21 09:40:31 +00001246 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001247 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001248 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001249 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1250 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001251 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001252
John McCall48871652010-08-21 09:40:31 +00001253 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001254 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001255 ProtocolRefs.data(),
1256 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001257 ProtocolLocs.data(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001258 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001259 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001260 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001261}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001262
1263/// objc-implementation:
1264/// objc-class-implementation-prologue
1265/// objc-category-implementation-prologue
1266///
1267/// objc-class-implementation-prologue:
1268/// @implementation identifier objc-superclass[opt]
1269/// objc-class-instance-variables[opt]
1270///
1271/// objc-category-implementation-prologue:
1272/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001273Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001274 SourceLocation atLoc) {
1275 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1276 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1277 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001278
Douglas Gregor49c22a72009-11-18 16:26:39 +00001279 // Code completion after '@implementation'.
1280 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001281 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001282 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001283 }
1284
Chris Lattner0ef13522007-10-09 17:51:17 +00001285 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001286 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001287 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001288 }
1289 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001290 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001291 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001292
1293 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001294 // we have a category implementation.
1295 SourceLocation lparenLoc = ConsumeParen();
1296 SourceLocation categoryLoc, rparenLoc;
1297 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001299 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001300 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001301 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001302 }
1303
Chris Lattner0ef13522007-10-09 17:51:17 +00001304 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001305 categoryId = Tok.getIdentifierInfo();
1306 categoryLoc = ConsumeToken();
1307 } else {
1308 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001309 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001310 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001311 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001312 Diag(Tok, diag::err_expected_rparen);
1313 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001314 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001315 }
1316 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001317 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001318 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001319 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001320 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001321 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001322 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001323 }
1324 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001325 SourceLocation superClassLoc;
1326 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001327 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001328 // We have a super class
1329 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001330 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001331 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001332 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001333 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001334 superClassId = Tok.getIdentifierInfo();
1335 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001336 }
John McCall48871652010-08-21 09:40:31 +00001337 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001338 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001339 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001340
Steve Naroff33a1e802007-10-29 21:38:07 +00001341 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001342 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahaniana9effb52010-03-22 19:04:14 +00001343 tok::objc_private, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001344 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001345 PendingObjCImpDecl.push_back(ObjCImpDecl);
1346
John McCall48871652010-08-21 09:40:31 +00001347 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001348}
Steve Naroff33a1e802007-10-29 21:38:07 +00001349
John McCall48871652010-08-21 09:40:31 +00001350Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001351 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1352 "ParseObjCAtEndDeclaration(): Expected @end");
John McCall48871652010-08-21 09:40:31 +00001353 Decl *Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001354 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001355 if (ObjCImpDecl) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001356 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001357 ObjCImpDecl = 0;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001358 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001359 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001360 else {
1361 // missing @implementation
1362 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1363 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001364 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001365}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001366
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001367Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1368 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001369 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001370 return Actions.ConvertDeclToDeclGroup(0);
1371 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001372 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001373 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1374}
1375
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001376/// compatibility-alias-decl:
1377/// @compatibility_alias alias-name class-name ';'
1378///
John McCall48871652010-08-21 09:40:31 +00001379Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001380 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1381 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1382 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001383 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001384 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001385 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001386 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001387 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1388 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001389 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001390 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001391 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001392 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001393 IdentifierInfo *classId = Tok.getIdentifierInfo();
1394 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1395 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001396 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCall48871652010-08-21 09:40:31 +00001397 return 0;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001398 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001399 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1400 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001401}
1402
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001403/// property-synthesis:
1404/// @synthesize property-ivar-list ';'
1405///
1406/// property-ivar-list:
1407/// property-ivar
1408/// property-ivar-list ',' property-ivar
1409///
1410/// property-ivar:
1411/// identifier
1412/// identifier '=' identifier
1413///
John McCall48871652010-08-21 09:40:31 +00001414Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001415 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1416 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001417 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001418
Douglas Gregor88e72a02009-11-18 19:45:45 +00001419 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001420 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001421 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001422 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001423 }
1424
Douglas Gregor88e72a02009-11-18 19:45:45 +00001425 if (Tok.isNot(tok::identifier)) {
1426 Diag(Tok, diag::err_synthesized_property_name);
1427 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001428 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001429 }
1430
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001431 IdentifierInfo *propertyIvar = 0;
1432 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1433 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001434 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001435 // property '=' ivar-name
1436 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001437
1438 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001439 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor5d649882009-11-18 22:32:06 +00001440 ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001441 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001442 }
1443
Chris Lattner0ef13522007-10-09 17:51:17 +00001444 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001445 Diag(Tok, diag::err_expected_ident);
1446 break;
1447 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001448 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001449 ConsumeToken(); // consume ivar-name
1450 }
Douglas Gregor0be31a22010-07-02 17:43:08 +00001451 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001452 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001453 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001454 break;
1455 ConsumeToken(); // consume ','
1456 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001457 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001458 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001459 SkipUntil(tok::semi);
1460 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001461 else
1462 ConsumeToken(); // consume ';'
John McCall48871652010-08-21 09:40:31 +00001463 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001464}
1465
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001466/// property-dynamic:
1467/// @dynamic property-list
1468///
1469/// property-list:
1470/// identifier
1471/// property-list ',' identifier
1472///
John McCall48871652010-08-21 09:40:31 +00001473Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001474 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1475 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1476 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001477 while (true) {
1478 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001479 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001480 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001481 }
1482
1483 if (Tok.isNot(tok::identifier)) {
1484 Diag(Tok, diag::err_expected_ident);
1485 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001486 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001487 }
1488
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001489 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1490 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor0be31a22010-07-02 17:43:08 +00001491 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001492 propertyId, 0);
1493
Chris Lattner0ef13522007-10-09 17:51:17 +00001494 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001495 break;
1496 ConsumeToken(); // consume ','
1497 }
Fariborz Jahanianb5795c02010-04-14 20:52:42 +00001498 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001499 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianb5795c02010-04-14 20:52:42 +00001500 SkipUntil(tok::semi);
1501 }
1502 else
1503 ConsumeToken(); // consume ';'
John McCall48871652010-08-21 09:40:31 +00001504 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001505}
Mike Stump11289f42009-09-09 15:08:12 +00001506
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001507/// objc-throw-statement:
1508/// throw expression[opt];
1509///
John McCalldadc5752010-08-24 06:29:42 +00001510StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1511 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001512 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001513 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001514 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001515 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001516 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001517 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001518 }
1519 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001520 // consume ';'
1521 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001522 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001523}
1524
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001525/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001526/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001527///
John McCalldadc5752010-08-24 06:29:42 +00001528StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001529Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001530 ConsumeToken(); // consume synchronized
1531 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001532 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001533 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001534 }
1535 ConsumeParen(); // '('
John McCalldadc5752010-08-24 06:29:42 +00001536 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001537 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001538 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001539 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001540 }
1541 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001542 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001543 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001544 }
1545 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001546 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001547 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001548 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001549 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001550 // Enter a scope to hold everything within the compound stmt. Compound
1551 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001552 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001553
John McCalldadc5752010-08-24 06:29:42 +00001554 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001555
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001556 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001557 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001558 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCallb268a282010-08-23 23:25:46 +00001559 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001560}
1561
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001562/// objc-try-catch-statement:
1563/// @try compound-statement objc-catch-list[opt]
1564/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1565///
1566/// objc-catch-list:
1567/// @catch ( parameter-declaration ) compound-statement
1568/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1569/// catch-parameter-declaration:
1570/// parameter-declaration
1571/// '...' [OBJC2]
1572///
John McCalldadc5752010-08-24 06:29:42 +00001573StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001574 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001575
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001576 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001577 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001578 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001579 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001580 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001581 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001582 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001583 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001584 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001585 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001586 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001587 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001588
Chris Lattner0ef13522007-10-09 17:51:17 +00001589 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001590 // At this point, we need to lookahead to determine if this @ is the start
1591 // of an @catch or @finally. We don't want to consume the @ token if this
1592 // is an @try or @encode or something else.
1593 Token AfterAt = GetLookAheadToken(1);
1594 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1595 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1596 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001597
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001598 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001599 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001600 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001601 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001602 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001603 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001604 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001605 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001606 DeclSpec DS;
1607 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001608 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001609 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001610 // we must accept either 'declarator' or 'abstract-declarator' here.
1611 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1612 ParseDeclarator(ParmDecl);
1613
Douglas Gregore11ee112010-04-23 23:01:43 +00001614 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001615 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001616 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001617 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001618 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001619
Steve Naroff65a00892009-04-07 22:56:58 +00001620 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001621
Steve Naroff65a00892009-04-07 22:56:58 +00001622 if (Tok.is(tok::r_paren))
1623 RParenLoc = ConsumeParen();
1624 else // Skip over garbage, until we get to ')'. Eat the ')'.
1625 SkipUntil(tok::r_paren, true, false);
1626
John McCalldadc5752010-08-24 06:29:42 +00001627 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001628 if (Tok.is(tok::l_brace))
1629 CatchBody = ParseCompoundStatementBody();
1630 else
1631 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001632 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001633 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001634
John McCalldadc5752010-08-24 06:29:42 +00001635 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001636 RParenLoc,
1637 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001638 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001639 if (!Catch.isInvalid())
1640 CatchStmts.push_back(Catch.release());
1641
Steve Naroffe6016792008-02-05 21:27:35 +00001642 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001643 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1644 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001645 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001646 }
1647 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001648 } else {
1649 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001650 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001651 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001652
John McCalldadc5752010-08-24 06:29:42 +00001653 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001654 if (Tok.is(tok::l_brace))
1655 FinallyBody = ParseCompoundStatementBody();
1656 else
1657 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001658 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001659 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001660 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001661 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001662 catch_or_finally_seen = true;
1663 break;
1664 }
1665 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001666 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001667 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001668 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001669 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001670
John McCallb268a282010-08-23 23:25:46 +00001671 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001672 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001673 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001674}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001675
Steve Naroff09bf8152007-09-06 21:24:23 +00001676/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001677///
John McCall48871652010-08-21 09:40:31 +00001678Decl *Parser::ParseObjCMethodDefinition() {
1679 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001680
John McCallfaf5fb42010-08-26 23:41:50 +00001681 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1682 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001683
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001684 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001685 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001686 if (ObjCImpDecl) {
1687 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001688 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001689 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001690 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001691 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001692
Steve Naroffbb875722007-11-11 19:54:21 +00001693 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001694 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001695 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001696
Steve Naroffbb875722007-11-11 19:54:21 +00001697 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1698 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001699
Steve Naroffbb875722007-11-11 19:54:21 +00001700 // If we didn't find the '{', bail out.
1701 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001702 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001703 }
Steve Naroffbb875722007-11-11 19:54:21 +00001704 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001705
Steve Naroffbb875722007-11-11 19:54:21 +00001706 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001707 ParseScope BodyScope(this,
1708 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001709
Steve Naroffbb875722007-11-11 19:54:21 +00001710 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001711 // specified Declarator for the method.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001712 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001713
John McCalldadc5752010-08-24 06:29:42 +00001714 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001715
Steve Naroffbb875722007-11-11 19:54:21 +00001716 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001717 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001718 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1719 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001720
Steve Naroffb94d7f62009-03-02 22:00:56 +00001721 // TODO: Pass argument information.
John McCallb268a282010-08-23 23:25:46 +00001722 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump11289f42009-09-09 15:08:12 +00001723
Steve Naroffbb875722007-11-11 19:54:21 +00001724 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001725 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001726
Steve Naroff7b8fa472007-11-13 23:01:27 +00001727 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001728}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001729
John McCalldadc5752010-08-24 06:29:42 +00001730StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001731 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001732 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001733 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001734 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001735 }
1736
1737 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001738 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001739
1740 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001741 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001742
1743 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001744 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001745
John McCalldadc5752010-08-24 06:29:42 +00001746 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001747 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001748 // If the expression is invalid, skip ahead to the next semicolon. Not
1749 // doing this opens us up to the possibility of infinite loops if
1750 // ParseExpression does not consume any tokens.
1751 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001752 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001753 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001754
Steve Naroffe6016792008-02-05 21:27:35 +00001755 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001756 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001757 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001758}
1759
John McCalldadc5752010-08-24 06:29:42 +00001760ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001761 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001762 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001763 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001764 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001765 return ExprError();
1766
Chris Lattnere002fbe2007-12-12 01:04:12 +00001767 case tok::string_literal: // primary-expression: string-literal
1768 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001769 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001770 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001771 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001772 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001773
Chris Lattner197a3012008-08-05 06:19:09 +00001774 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1775 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001776 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001777 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001778 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001779 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001780 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001781 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001782 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001783 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001784 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001785}
1786
Douglas Gregor8d4de672010-04-21 22:36:40 +00001787/// \brirg Parse the receiver of an Objective-C++ message send.
1788///
1789/// This routine parses the receiver of a message send in
1790/// Objective-C++ either as a type or as an expression. Note that this
1791/// routine must not be called to parse a send to 'super', since it
1792/// has no way to return such a result.
1793///
1794/// \param IsExpr Whether the receiver was parsed as an expression.
1795///
1796/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1797/// IsExpr is true), the parsed expression. If the receiver was parsed
1798/// as a type (\c IsExpr is false), the parsed type.
1799///
1800/// \returns True if an error occurred during parsing or semantic
1801/// analysis, in which case the arguments do not have valid
1802/// values. Otherwise, returns false for a successful parse.
1803///
1804/// objc-receiver: [C++]
1805/// 'super' [not parsed here]
1806/// expression
1807/// simple-type-specifier
1808/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001809bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001810 InMessageExpressionRAIIObject InMessage(*this, true);
1811
Douglas Gregor8d4de672010-04-21 22:36:40 +00001812 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1813 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1814 TryAnnotateTypeOrScopeToken();
1815
1816 if (!isCXXSimpleTypeSpecifier()) {
1817 // objc-receiver:
1818 // expression
John McCalldadc5752010-08-24 06:29:42 +00001819 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001820 if (Receiver.isInvalid())
1821 return true;
1822
1823 IsExpr = true;
1824 TypeOrExpr = Receiver.take();
1825 return false;
1826 }
1827
1828 // objc-receiver:
1829 // typename-specifier
1830 // simple-type-specifier
1831 // expression (that starts with one of the above)
1832 DeclSpec DS;
1833 ParseCXXSimpleTypeSpecifier(DS);
1834
1835 if (Tok.is(tok::l_paren)) {
1836 // If we see an opening parentheses at this point, we are
1837 // actually parsing an expression that starts with a
1838 // function-style cast, e.g.,
1839 //
1840 // postfix-expression:
1841 // simple-type-specifier ( expression-list [opt] )
1842 // typename-specifier ( expression-list [opt] )
1843 //
1844 // Parse the remainder of this case, then the (optional)
1845 // postfix-expression suffix, followed by the (optional)
1846 // right-hand side of the binary expression. We have an
1847 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001848 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001849 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001850 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001851 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001852 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001853 if (Receiver.isInvalid())
1854 return true;
1855
1856 IsExpr = true;
1857 TypeOrExpr = Receiver.take();
1858 return false;
1859 }
1860
1861 // We have a class message. Turn the simple-type-specifier or
1862 // typename-specifier we parsed into a type and parse the
1863 // remainder of the class message.
1864 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001865 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001866 if (Type.isInvalid())
1867 return true;
1868
1869 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001870 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001871 return false;
1872}
1873
Douglas Gregor990ccac2010-05-31 14:40:22 +00001874/// \brief Determine whether the parser is currently referring to a an
1875/// Objective-C message send, using a simplified heuristic to avoid overhead.
1876///
1877/// This routine will only return true for a subset of valid message-send
1878/// expressions.
1879bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001880 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001881 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001882 return GetLookAheadToken(1).is(tok::identifier) &&
1883 GetLookAheadToken(2).is(tok::identifier);
1884}
1885
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001886bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1887 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1888 InMessageExpression)
1889 return false;
1890
1891
1892 ParsedType Type;
1893
1894 if (Tok.is(tok::annot_typename))
1895 Type = getTypeAnnotation(Tok);
1896 else if (Tok.is(tok::identifier))
1897 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1898 getCurScope());
1899 else
1900 return false;
1901
1902 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1903 const Token &AfterNext = GetLookAheadToken(2);
1904 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1905 if (Tok.is(tok::identifier))
1906 TryAnnotateTypeOrScopeToken();
1907
1908 return Tok.is(tok::annot_typename);
1909 }
1910 }
1911
1912 return false;
1913}
1914
Mike Stump11289f42009-09-09 15:08:12 +00001915/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001916/// '[' objc-receiver objc-message-args ']'
1917///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001918/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00001919/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001920/// expression
1921/// class-name
1922/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00001923///
John McCalldadc5752010-08-24 06:29:42 +00001924ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001925 assert(Tok.is(tok::l_square) && "'[' expected");
1926 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1927
Douglas Gregora817a192010-05-27 23:06:34 +00001928 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001929 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregora817a192010-05-27 23:06:34 +00001930 ConsumeCodeCompletionToken();
1931 SkipUntil(tok::r_square);
1932 return ExprError();
1933 }
1934
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001935 InMessageExpressionRAIIObject InMessage(*this, true);
1936
Douglas Gregor8d4de672010-04-21 22:36:40 +00001937 if (getLang().CPlusPlus) {
1938 // We completely separate the C and C++ cases because C++ requires
1939 // more complicated (read: slower) parsing.
1940
1941 // Handle send to super.
1942 // FIXME: This doesn't benefit from the same typo-correction we
1943 // get in Objective-C.
1944 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001945 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00001946 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1947 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001948
1949 // Parse the receiver, which is either a type or an expression.
1950 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00001951 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00001952 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1953 SkipUntil(tok::r_square);
1954 return ExprError();
1955 }
1956
1957 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00001958 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1959 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00001960 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00001961
1962 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00001963 ParsedType::getFromOpaquePtr(TypeOrExpr),
1964 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00001965 }
1966
1967 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00001968 IdentifierInfo *Name = Tok.getIdentifierInfo();
1969 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00001970 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001971 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00001972 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00001973 NextToken().is(tok::period),
1974 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001975 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00001976 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1977 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001978
John McCallfaf5fb42010-08-26 23:41:50 +00001979 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00001980 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001981 SkipUntil(tok::r_square);
1982 return ExprError();
1983 }
1984
Douglas Gregore5798dc2010-04-21 20:38:13 +00001985 ConsumeToken(); // the type name
1986
1987 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00001988 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00001989
John McCallfaf5fb42010-08-26 23:41:50 +00001990 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001991 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00001992 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001993 }
Chris Lattner8f697062008-01-25 18:59:06 +00001994 }
Chris Lattnera36ec422010-04-11 08:28:14 +00001995
1996 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00001997 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001998 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001999 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002000 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002001 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002002
John McCallba7bf592010-08-24 05:47:05 +00002003 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2004 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002005}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002006
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002007/// \brief Parse the remainder of an Objective-C message following the
2008/// '[' objc-receiver.
2009///
2010/// This routine handles sends to super, class messages (sent to a
2011/// class name), and instance messages (sent to an object), and the
2012/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2013/// ReceiverExpr, respectively. Only one of these parameters may have
2014/// a valid value.
2015///
2016/// \param LBracLoc The location of the opening '['.
2017///
2018/// \param SuperLoc If this is a send to 'super', the location of the
2019/// 'super' keyword that indicates a send to the superclass.
2020///
2021/// \param ReceiverType If this is a class message, the type of the
2022/// class we are sending a message to.
2023///
2024/// \param ReceiverExpr If this is an instance message, the expression
2025/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002026///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002027/// objc-message-args:
2028/// objc-selector
2029/// objc-keywordarg-list
2030///
2031/// objc-keywordarg-list:
2032/// objc-keywordarg
2033/// objc-keywordarg-list objc-keywordarg
2034///
Mike Stump11289f42009-09-09 15:08:12 +00002035/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002036/// selector-name[opt] ':' objc-keywordexpr
2037///
2038/// objc-keywordexpr:
2039/// nonempty-expr-list
2040///
2041/// nonempty-expr-list:
2042/// assignment-expression
2043/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002044///
John McCalldadc5752010-08-24 06:29:42 +00002045ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002046Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002047 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002048 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002049 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002050 InMessageExpressionRAIIObject InMessage(*this, true);
2051
Steve Naroffeae65032009-11-07 02:08:14 +00002052 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002053 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002054 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2055 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002056 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002057 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2058 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002059 else
John McCallb268a282010-08-23 23:25:46 +00002060 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002061 0, 0, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002062 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00002063 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002064
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002065 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002066 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002067 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002068
Anders Carlsson978f08d2009-02-14 18:21:46 +00002069 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002070
Steve Narofff73590d2007-09-27 14:38:14 +00002071 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002072 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002073
Chris Lattner0ef13522007-10-09 17:51:17 +00002074 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002075 while (1) {
2076 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002077 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002078
Chris Lattner0ef13522007-10-09 17:51:17 +00002079 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002080 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002081 // We must manually skip to a ']', otherwise the expression skipper will
2082 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2083 // the enclosing expression.
2084 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002085 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002086 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002087
Steve Narofff73590d2007-09-27 14:38:14 +00002088 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002089 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002090
2091 if (Tok.is(tok::code_completion)) {
2092 if (SuperLoc.isValid())
2093 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2094 KeyIdents.data(),
2095 KeyIdents.size(),
2096 /*AtArgumentEpression=*/true);
2097 else if (ReceiverType)
2098 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2099 KeyIdents.data(),
2100 KeyIdents.size(),
2101 /*AtArgumentEpression=*/true);
2102 else
2103 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2104 KeyIdents.data(),
2105 KeyIdents.size(),
2106 /*AtArgumentEpression=*/true);
2107
2108 ConsumeCodeCompletionToken();
2109 SkipUntil(tok::r_square);
2110 return ExprError();
2111 }
2112
John McCalldadc5752010-08-24 06:29:42 +00002113 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002114 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002115 // We must manually skip to a ']', otherwise the expression skipper will
2116 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2117 // the enclosing expression.
2118 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002119 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002120 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002121
Steve Naroff486760a2007-09-17 20:25:27 +00002122 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002123 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002124
Douglas Gregor1b605f72009-11-19 01:08:35 +00002125 // Code completion after each argument.
2126 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002127 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002128 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002129 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002130 KeyIdents.size(),
2131 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002132 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002133 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002134 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002135 KeyIdents.size(),
2136 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002137 else
John McCallb268a282010-08-23 23:25:46 +00002138 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002139 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002140 KeyIdents.size(),
2141 /*AtArgumentEpression=*/false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002142 ConsumeCodeCompletionToken();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002143 SkipUntil(tok::r_square);
2144 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002145 }
2146
Steve Naroff486760a2007-09-17 20:25:27 +00002147 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002148 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002149 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002150 break;
2151 // We have a selector or a colon, continue parsing.
2152 }
2153 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002154 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002155 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002156 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002157 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002158 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002159 // We must manually skip to a ']', otherwise the expression skipper will
2160 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2161 // the enclosing expression.
2162 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002163 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002164 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002165
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002166 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002167 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002168 }
2169 } else if (!selIdent) {
2170 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002171
Chris Lattner197a3012008-08-05 06:19:09 +00002172 // We must manually skip to a ']', otherwise the expression skipper will
2173 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2174 // the enclosing expression.
2175 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002176 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002177 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002178
Chris Lattner0ef13522007-10-09 17:51:17 +00002179 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002180 if (Tok.is(tok::identifier))
2181 Diag(Tok, diag::err_expected_colon);
2182 else
2183 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002184 // We must manually skip to a ']', otherwise the expression skipper will
2185 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2186 // the enclosing expression.
2187 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002188 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002189 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002190
Chris Lattner8f697062008-01-25 18:59:06 +00002191 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002192
Steve Naroffe61bfa82007-10-05 18:42:47 +00002193 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002194 if (nKeys == 0)
2195 KeyIdents.push_back(selIdent);
2196 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002197
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002198 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002199 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002200 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002201 MultiExprArg(Actions,
2202 KeyExprs.take(),
2203 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002204 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002205 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002206 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002207 MultiExprArg(Actions,
2208 KeyExprs.take(),
2209 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002210 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002211 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002212 MultiExprArg(Actions,
2213 KeyExprs.take(),
2214 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002215}
2216
John McCalldadc5752010-08-24 06:29:42 +00002217ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2218 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002219 if (Res.isInvalid()) return move(Res);
2220
Chris Lattnere002fbe2007-12-12 01:04:12 +00002221 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2222 // expressions. At this point, we know that the only valid thing that starts
2223 // with '@' is an @"".
2224 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002225 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002226 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002227 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002228
Chris Lattnere002fbe2007-12-12 01:04:12 +00002229 while (Tok.is(tok::at)) {
2230 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002231
Sebastian Redlc13f2682008-12-09 20:22:58 +00002232 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002233 if (!isTokenStringLiteral())
2234 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002235
John McCalldadc5752010-08-24 06:29:42 +00002236 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002237 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002238 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002239
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002240 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002241 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002242
2243 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2244 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002245}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002246
2247/// objc-encode-expression:
2248/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002249ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002250Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002251 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002252
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002253 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002254
Chris Lattner197a3012008-08-05 06:19:09 +00002255 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002256 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2257
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002258 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002259
Douglas Gregor220cac52009-02-18 17:45:20 +00002260 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002261
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002262 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002263
Douglas Gregor220cac52009-02-18 17:45:20 +00002264 if (Ty.isInvalid())
2265 return ExprError();
2266
Mike Stump11289f42009-09-09 15:08:12 +00002267 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002268 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002269}
Anders Carlssone01493d2007-08-23 15:25:28 +00002270
2271/// objc-protocol-expression
2272/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002273ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002274Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002275 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002276
Chris Lattner197a3012008-08-05 06:19:09 +00002277 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002278 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2279
Anders Carlssone01493d2007-08-23 15:25:28 +00002280 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002281
Chris Lattner197a3012008-08-05 06:19:09 +00002282 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002283 return ExprError(Diag(Tok, diag::err_expected_ident));
2284
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002285 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002286 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002287
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002288 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002289
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002290 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2291 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002292}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002293
2294/// objc-selector-expression
2295/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002296ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002297 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002298
Chris Lattner197a3012008-08-05 06:19:09 +00002299 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002300 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2301
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002302 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002303 SourceLocation LParenLoc = ConsumeParen();
2304 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002305
2306 if (Tok.is(tok::code_completion)) {
2307 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2308 KeyIdents.size());
2309 ConsumeCodeCompletionToken();
2310 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2311 return ExprError();
2312 }
2313
Chris Lattner4f472a32009-04-11 18:13:45 +00002314 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002315 if (!SelIdent && // missing selector name.
2316 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002317 return ExprError(Diag(Tok, diag::err_expected_ident));
2318
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002319 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002320 unsigned nColons = 0;
2321 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002322 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002323 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2324 ++nColons;
2325 KeyIdents.push_back(0);
2326 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002327 return ExprError(Diag(Tok, diag::err_expected_colon));
2328
Chris Lattner1ba64452010-08-27 22:32:41 +00002329 ++nColons;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002330 ConsumeToken(); // Eat the ':'.
2331 if (Tok.is(tok::r_paren))
2332 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002333
2334 if (Tok.is(tok::code_completion)) {
2335 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2336 KeyIdents.size());
2337 ConsumeCodeCompletionToken();
2338 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2339 return ExprError();
2340 }
2341
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002342 // Check for another keyword selector.
2343 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002344 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002345 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002346 if (!SelIdent && Tok.isNot(tok::colon))
2347 break;
2348 }
Steve Naroff152dd812007-12-05 22:21:29 +00002349 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002350 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002351 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002352 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2353 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002354 }