blob: c48f6803b01ce3007da5686303b0c31f04e33d22 [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);
John McCall53fa7142010-12-24 02:08:15 +000043 case tok::objc_interface: {
44 ParsedAttributes attrs;
45 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
48 ParsedAttributes attrs;
49 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
50 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000051 case tok::objc_implementation:
52 return ParseObjCAtImplementationDeclaration(AtLoc);
53 case tok::objc_end:
54 return ParseObjCAtEndDeclaration(AtLoc);
55 case tok::objc_compatibility_alias:
56 return ParseObjCAtAliasDeclaration(AtLoc);
57 case tok::objc_synthesize:
58 return ParseObjCPropertySynthesize(AtLoc);
59 case tok::objc_dynamic:
60 return ParseObjCPropertyDynamic(AtLoc);
61 default:
62 Diag(AtLoc, diag::err_unexpected_at);
63 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000064 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000065 }
66}
67
68///
Mike Stump11289f42009-09-09 15:08:12 +000069/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000071///
John McCall48871652010-08-21 09:40:31 +000072Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000073 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000074 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000075 llvm::SmallVector<SourceLocation, 8> ClassLocs;
76
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000079 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000080 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000081 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000082 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000084 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000085 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner0ef13522007-10-09 17:51:17 +000088 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000089 break;
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerda59c2f2006-11-05 02:08:13 +000091 ConsumeToken();
92 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 // Consume the ';'.
95 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCall48871652010-08-21 09:40:31 +000096 return 0;
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremeneka26da852009-11-17 23:12:20 +000098 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
99 ClassLocs.data(),
100 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000101}
102
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000103///
104/// objc-interface:
105/// objc-class-interface-attributes[opt] objc-class-interface
106/// objc-category-interface
107///
108/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000109/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000110/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000111/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000112/// objc-interface-decl-list
113/// @end
114///
115/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000116/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000117/// objc-protocol-refs[opt]
118/// objc-interface-decl-list
119/// @end
120///
121/// objc-superclass:
122/// ':' identifier
123///
124/// objc-class-interface-attributes:
125/// __attribute__((visibility("default")))
126/// __attribute__((visibility("hidden")))
127/// __attribute__((deprecated))
128/// __attribute__((unavailable))
129/// __attribute__((objc_exception)) - used by NSException on 64-bit
130///
John McCall53fa7142010-12-24 02:08:15 +0000131Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
132 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000133 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000134 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
135 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000136
Douglas Gregor49c22a72009-11-18 16:26:39 +0000137 // Code completion after '@interface'.
138 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000139 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000140 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000141 }
142
Chris Lattner0ef13522007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000145 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000146 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000147
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000148 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000149 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000150 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000151 if (Tok.is(tok::l_paren) &&
152 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000153 SourceLocation lparenLoc = ConsumeParen();
154 SourceLocation categoryLoc, rparenLoc;
155 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000156 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000157 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000158 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000159 }
160
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000161 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000162 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163 categoryId = Tok.getIdentifierInfo();
164 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000165 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000166 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000167 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000168 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000169 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000170 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 Diag(Tok, diag::err_expected_rparen);
172 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +0000173 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000174 }
175 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000176 // Next, we need to check for any protocol references.
177 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +0000178 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000179 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
180 if (Tok.is(tok::less) &&
181 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000182 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000183 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000184
John McCall53fa7142010-12-24 02:08:15 +0000185 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000186 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000187
John McCall48871652010-08-21 09:40:31 +0000188 Decl *CategoryType =
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000189 Actions.ActOnStartCategoryInterface(atLoc,
190 nameId, nameLoc,
191 categoryId, categoryLoc,
192 ProtocolRefs.data(),
193 ProtocolRefs.size(),
194 ProtocolLocs.data(),
195 EndProtoLoc);
196 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000197 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
198 atLoc);
199
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000200 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
201 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202 }
203 // Parse a class interface.
204 IdentifierInfo *superClassId = 0;
205 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000206
Chris Lattner0ef13522007-10-09 17:51:17 +0000207 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000208 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000209
210 // Code completion of superclass names.
211 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000212 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000213 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000214 }
215
Chris Lattner0ef13522007-10-09 17:51:17 +0000216 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000217 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000218 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 }
220 superClassId = Tok.getIdentifierInfo();
221 superClassLoc = ConsumeToken();
222 }
223 // Next, we need to check for any protocol references.
John McCall48871652010-08-21 09:40:31 +0000224 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000225 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
226 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000227 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000228 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
229 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000230 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000231
John McCall48871652010-08-21 09:40:31 +0000232 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000233 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000234 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000235 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000236 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000237 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000238
Chris Lattner0ef13522007-10-09 17:51:17 +0000239 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000240 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000241
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000242 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000243 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000244}
245
John McCall064d77b2009-12-03 22:31:13 +0000246/// The Objective-C property callback. This should be defined where
247/// it's used, but instead it's been lifted to here to support VS2005.
248struct Parser::ObjCPropertyCallback : FieldCallback {
249 Parser &P;
John McCall48871652010-08-21 09:40:31 +0000250 Decl *IDecl;
251 llvm::SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000252 ObjCDeclSpec &OCDS;
253 SourceLocation AtLoc;
254 tok::ObjCKeywordKind MethodImplKind;
255
John McCall48871652010-08-21 09:40:31 +0000256 ObjCPropertyCallback(Parser &P, Decl *IDecl,
257 llvm::SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000258 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
259 tok::ObjCKeywordKind MethodImplKind) :
260 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
261 MethodImplKind(MethodImplKind) {
262 }
263
John McCall48871652010-08-21 09:40:31 +0000264 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000265 if (FD.D.getIdentifier() == 0) {
266 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
267 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000268 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000269 }
270 if (FD.BitfieldSize) {
271 P.Diag(AtLoc, diag::err_objc_property_bitfield)
272 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000273 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000274 }
275
276 // Install the property declarator into interfaceDecl.
277 IdentifierInfo *SelName =
278 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
279
280 Selector GetterSel =
281 P.PP.getSelectorTable().getNullarySelector(SelName);
282 IdentifierInfo *SetterName = OCDS.getSetterName();
283 Selector SetterSel;
284 if (SetterName)
285 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
286 else
287 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
288 P.PP.getSelectorTable(),
289 FD.D.getIdentifier());
290 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000291 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000292 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCall064d77b2009-12-03 22:31:13 +0000293 GetterSel, SetterSel, IDecl,
294 &isOverridingProperty,
295 MethodImplKind);
296 if (!isOverridingProperty)
297 Props.push_back(Property);
298
299 return Property;
300 }
301};
302
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000303/// objc-interface-decl-list:
304/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000305/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000306/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000307/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000308/// objc-interface-decl-list declaration
309/// objc-interface-decl-list ';'
310///
Steve Naroff99264b42007-08-22 16:35:03 +0000311/// objc-method-requirement: [OBJC2]
312/// @required
313/// @optional
314///
John McCall48871652010-08-21 09:40:31 +0000315void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000316 tok::ObjCKeywordKind contextKey) {
John McCall48871652010-08-21 09:40:31 +0000317 llvm::SmallVector<Decl *, 32> allMethods;
318 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000319 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000320 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenekc7c64312010-01-07 01:20:12 +0000322 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000323
Steve Naroff99264b42007-08-22 16:35:03 +0000324 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000325 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000326 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000327 Decl *methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000328 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000329 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000330 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
331 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000332 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
333 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000334 continue;
335 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000336 if (Tok.is(tok::l_paren)) {
337 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000338 ParseObjCMethodDecl(Tok.getLocation(),
339 tok::minus,
340 interfaceDecl,
341 MethodImplKind);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000342 continue;
343 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000344 // Ignore excess semicolons.
345 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000346 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000347 continue;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattnerda9fb152008-10-20 06:10:06 +0000350 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000351 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000352 break;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Douglas Gregorf1934162010-01-13 21:24:21 +0000354 // Code completion within an Objective-C interface.
355 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000356 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000357 ObjCImpDecl? Sema::PCC_ObjCImplementation
358 : Sema::PCC_ObjCInterface);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000359 ConsumeCodeCompletionToken();
Douglas Gregorf1934162010-01-13 21:24:21 +0000360 }
361
Chris Lattner038a3e32008-10-20 05:46:22 +0000362 // If we don't have an @ directive, parse it as a function definition.
363 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000364 // The code below does not consume '}'s because it is afraid of eating the
365 // end of a namespace. Because of the way this code is structured, an
366 // erroneous r_brace would cause an infinite loop if not handled here.
367 if (Tok.is(tok::r_brace))
368 break;
Mike Stump11289f42009-09-09 15:08:12 +0000369
Steve Narofff1bc45b2007-08-22 18:35:33 +0000370 // FIXME: as the name implies, this rule allows function definitions.
371 // We could pass a flag or check for functions during semantic analysis.
John McCall53fa7142010-12-24 02:08:15 +0000372 ParsedAttributes attrs;
373 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000374 continue;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattner038a3e32008-10-20 05:46:22 +0000377 // Otherwise, we have an @ directive, eat the @.
378 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000379 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000380 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000381 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000382 break;
383 }
384
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000385 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000387 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000388 AtEnd.setBegin(AtLoc);
389 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000390 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000391 } else if (DirectiveKind == tok::objc_not_keyword) {
392 Diag(Tok, diag::err_objc_unknown_at);
393 SkipUntil(tok::semi);
394 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnerda9fb152008-10-20 06:10:06 +0000397 // Eat the identifier.
398 ConsumeToken();
399
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000400 switch (DirectiveKind) {
401 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000402 // FIXME: If someone forgets an @end on a protocol, this loop will
403 // continue to eat up tons of stuff and spew lots of nonsense errors. It
404 // would probably be better to bail out if we saw an @class or @interface
405 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000406 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000407 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000408 SkipUntil(tok::r_brace, tok::at);
409 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000410
411 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000412 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000413 Diag(Tok, diag::err_objc_missing_end);
414 ConsumeToken();
415 break;
416
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000417 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000419 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000420 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000421 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000422 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000423 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000424 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000425 break;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000427 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000428 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000429 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000430
Chris Lattner038a3e32008-10-20 05:46:22 +0000431 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000432 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000433 if (Tok.is(tok::l_paren))
Douglas Gregor87e92752010-12-21 17:34:17 +0000434 ParseObjCPropertyAttribute(OCDS, interfaceDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000435
John McCall064d77b2009-12-03 22:31:13 +0000436 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
437 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000438
Chris Lattner038a3e32008-10-20 05:46:22 +0000439 // Parse all the comma separated declarators.
440 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000441 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000443 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
444 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000445 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000446 }
Steve Naroff99264b42007-08-22 16:35:03 +0000447 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000448
449 // We break out of the big loop in two cases: when we see @end or when we see
450 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000451 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000452 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000453 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000454 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000455 ConsumeToken(); // the "end" identifier
456 else
457 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000460 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000461 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000462 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000463 allProperties.data(), allProperties.size(),
464 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000465}
466
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000467/// Parse property attribute declarations.
468///
469/// property-attr-decl: '(' property-attrlist ')'
470/// property-attrlist:
471/// property-attribute
472/// property-attrlist ',' property-attribute
473/// property-attribute:
474/// getter '=' identifier
475/// setter '=' identifier ':'
476/// readonly
477/// readwrite
478/// assign
479/// retain
480/// copy
481/// nonatomic
482///
Douglas Gregor87e92752010-12-21 17:34:17 +0000483void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000484 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000485 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000486
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000487 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000488 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000489 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000490 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000491 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000492 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner76619232008-10-20 07:22:18 +0000494 // If this is not an identifier at all, bail out early.
495 if (II == 0) {
496 MatchRHSPunctuation(tok::r_paren, LHSLoc);
497 return;
498 }
Mike Stump11289f42009-09-09 15:08:12 +0000499
Chris Lattner1db33542008-10-20 07:37:22 +0000500 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner68e48682008-11-20 04:42:34 +0000502 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000504 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000506 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000508 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000510 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000512 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000513 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000514 else if (II->isStr("atomic"))
515 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000516 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000517 bool IsSetter = II->getNameStart()[0] == 's';
518
Chris Lattner825bca12008-10-20 07:39:53 +0000519 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000520 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
521 diag::err_objc_expected_equal_for_getter;
522
523 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000524 return;
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregorc8537c52009-11-19 07:41:15 +0000526 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000527 if (IsSetter)
Douglas Gregor87e92752010-12-21 17:34:17 +0000528 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregorc8537c52009-11-19 07:41:15 +0000529 else
Douglas Gregor87e92752010-12-21 17:34:17 +0000530 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000531 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000532 }
533
Anders Carlssonfe15a782010-10-02 17:45:21 +0000534
535 SourceLocation SelLoc;
536 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
537
538 if (!SelIdent) {
539 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
540 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000541 SkipUntil(tok::r_paren);
542 return;
543 }
Mike Stump11289f42009-09-09 15:08:12 +0000544
Anders Carlssonfe15a782010-10-02 17:45:21 +0000545 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000546 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000547 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000548
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000549 if (ExpectAndConsume(tok::colon,
550 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000551 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000552 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000553 } else {
554 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000555 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000556 }
Chris Lattner825bca12008-10-20 07:39:53 +0000557 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000558 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000559 SkipUntil(tok::r_paren);
560 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000561 }
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattner1db33542008-10-20 07:37:22 +0000563 if (Tok.isNot(tok::comma))
564 break;
Mike Stump11289f42009-09-09 15:08:12 +0000565
Chris Lattner1db33542008-10-20 07:37:22 +0000566 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner1db33542008-10-20 07:37:22 +0000569 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000570}
571
Steve Naroff09bf8152007-09-06 21:24:23 +0000572/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000573/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000574/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000575///
576/// objc-instance-method: '-'
577/// objc-class-method: '+'
578///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000579/// objc-method-attributes: [OBJC2]
580/// __attribute__((deprecated))
581///
John McCall48871652010-08-21 09:40:31 +0000582Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
583 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000584 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000585
Mike Stump11289f42009-09-09 15:08:12 +0000586 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000587 SourceLocation mLoc = ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000588 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000589 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000590 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000591 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000592}
593
594/// objc-selector:
595/// identifier
596/// one of
597/// enum struct union if else while do for switch case default
598/// break continue return goto asm sizeof typeof __alignof
599/// unsigned long const short volatile signed restrict _Complex
600/// in out inout bycopy byref oneway int char float double void _Bool
601///
Chris Lattner4f472a32009-04-11 18:13:45 +0000602IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000603
Chris Lattner5700fab2007-10-07 02:00:24 +0000604 switch (Tok.getKind()) {
605 default:
606 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000607 case tok::ampamp:
608 case tok::ampequal:
609 case tok::amp:
610 case tok::pipe:
611 case tok::tilde:
612 case tok::exclaim:
613 case tok::exclaimequal:
614 case tok::pipepipe:
615 case tok::pipeequal:
616 case tok::caret:
617 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000618 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000619 if (isalpha(ThisTok[0])) {
620 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
621 Tok.setKind(tok::identifier);
622 SelectorLoc = ConsumeToken();
623 return II;
624 }
625 return 0;
626 }
627
Chris Lattner5700fab2007-10-07 02:00:24 +0000628 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000629 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000630 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000631 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000632 case tok::kw_break:
633 case tok::kw_case:
634 case tok::kw_catch:
635 case tok::kw_char:
636 case tok::kw_class:
637 case tok::kw_const:
638 case tok::kw_const_cast:
639 case tok::kw_continue:
640 case tok::kw_default:
641 case tok::kw_delete:
642 case tok::kw_do:
643 case tok::kw_double:
644 case tok::kw_dynamic_cast:
645 case tok::kw_else:
646 case tok::kw_enum:
647 case tok::kw_explicit:
648 case tok::kw_export:
649 case tok::kw_extern:
650 case tok::kw_false:
651 case tok::kw_float:
652 case tok::kw_for:
653 case tok::kw_friend:
654 case tok::kw_goto:
655 case tok::kw_if:
656 case tok::kw_inline:
657 case tok::kw_int:
658 case tok::kw_long:
659 case tok::kw_mutable:
660 case tok::kw_namespace:
661 case tok::kw_new:
662 case tok::kw_operator:
663 case tok::kw_private:
664 case tok::kw_protected:
665 case tok::kw_public:
666 case tok::kw_register:
667 case tok::kw_reinterpret_cast:
668 case tok::kw_restrict:
669 case tok::kw_return:
670 case tok::kw_short:
671 case tok::kw_signed:
672 case tok::kw_sizeof:
673 case tok::kw_static:
674 case tok::kw_static_cast:
675 case tok::kw_struct:
676 case tok::kw_switch:
677 case tok::kw_template:
678 case tok::kw_this:
679 case tok::kw_throw:
680 case tok::kw_true:
681 case tok::kw_try:
682 case tok::kw_typedef:
683 case tok::kw_typeid:
684 case tok::kw_typename:
685 case tok::kw_typeof:
686 case tok::kw_union:
687 case tok::kw_unsigned:
688 case tok::kw_using:
689 case tok::kw_virtual:
690 case tok::kw_void:
691 case tok::kw_volatile:
692 case tok::kw_wchar_t:
693 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000694 case tok::kw__Bool:
695 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000696 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000697 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000698 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000699 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000700 }
Steve Naroff99264b42007-08-22 16:35:03 +0000701}
702
Fariborz Jahanian83615522008-01-02 22:54:34 +0000703/// objc-for-collection-in: 'in'
704///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000705bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000706 // FIXME: May have to do additional look-ahead to only allow for
707 // valid tokens following an 'in'; such as an identifier, unary operators,
708 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000709 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000710 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000711}
712
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000713/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000714/// qualifier list and builds their bitmask representation in the input
715/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000716///
717/// objc-type-qualifiers:
718/// objc-type-qualifier
719/// objc-type-qualifiers objc-type-qualifier
720///
Douglas Gregor99fa2642010-08-24 01:06:58 +0000721void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattner61511e12007-12-12 06:56:32 +0000722 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000723 if (Tok.is(tok::code_completion)) {
724 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
725 ConsumeCodeCompletionToken();
726 }
727
Chris Lattner5e530bc2007-12-27 19:57:00 +0000728 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000729 return;
Mike Stump11289f42009-09-09 15:08:12 +0000730
Chris Lattner61511e12007-12-12 06:56:32 +0000731 const IdentifierInfo *II = Tok.getIdentifierInfo();
732 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000733 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000734 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000735
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000736 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000737 switch (i) {
738 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000739 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
740 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
741 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
742 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
743 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
744 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000745 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000746 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000747 ConsumeToken();
748 II = 0;
749 break;
750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattner61511e12007-12-12 06:56:32 +0000752 // If this wasn't a recognized qualifier, bail out.
753 if (II) return;
754 }
755}
756
757/// objc-type-name:
758/// '(' objc-type-qualifiers[opt] type-name ')'
759/// '(' objc-type-qualifiers[opt] ')'
760///
John McCallba7bf592010-08-24 05:47:05 +0000761ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000762 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattnerb7954432008-10-22 03:52:06 +0000764 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000765 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000766
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000767 // Parse type qualifiers, in, inout, etc.
Douglas Gregor99fa2642010-08-24 01:06:58 +0000768 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000769
John McCallba7bf592010-08-24 05:47:05 +0000770 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000771 if (isTypeSpecifierQualifier()) {
772 TypeResult TypeSpec = ParseTypeName();
773 if (!TypeSpec.isInvalid())
774 Ty = TypeSpec.get();
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Steve Naroff90255b42008-10-21 14:15:04 +0000777 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000778 ConsumeParen();
779 else if (Tok.getLocation() == TypeStartLoc) {
780 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000781 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000782 SkipUntil(tok::r_paren);
783 } else {
784 // Otherwise, we found *something*, but didn't get a ')' in the right
785 // place. Emit an error then return what we have as the type.
786 MatchRHSPunctuation(tok::r_paren, LParenLoc);
787 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000788 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000789}
790
791/// objc-method-decl:
792/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000793/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000794/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000795/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000796///
797/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000798/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000799/// objc-keyword-selector objc-keyword-decl
800///
801/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000802/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
803/// objc-selector ':' objc-keyword-attributes[opt] identifier
804/// ':' objc-type-name objc-keyword-attributes[opt] identifier
805/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000806///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000807/// objc-parmlist:
808/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000809///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000810/// objc-parms:
811/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000812///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000813/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000814/// , ...
815///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000816/// objc-keyword-attributes: [OBJC2]
817/// __attribute__((unused))
818///
John McCall48871652010-08-21 09:40:31 +0000819Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000820 tok::TokenKind mType,
821 Decl *IDecl,
822 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000823 ParsingDeclRAIIObject PD(*this);
824
Douglas Gregor636a61e2010-04-07 00:21:17 +0000825 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000826 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallba7bf592010-08-24 05:47:05 +0000827 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000828 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000829 }
830
Chris Lattner2ebb1782008-08-23 01:48:03 +0000831 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000832 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000833 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000834 if (Tok.is(tok::l_paren))
Douglas Gregor99fa2642010-08-24 01:06:58 +0000835 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000837 // If attributes exist before the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000838 ParsedAttributes attrs;
839 if (getLang().ObjC2)
840 MaybeParseGNUAttributes(attrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000841
Douglas Gregor636a61e2010-04-07 00:21:17 +0000842 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000843 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregor636a61e2010-04-07 00:21:17 +0000844 ReturnType, IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000845 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000846 }
847
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000848 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000849 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000850 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000851
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000852 // An unnamed colon is valid.
853 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000854 Diag(Tok, diag::err_expected_selector_for_method)
855 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000856 // Skip until we get a ; or {}.
857 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000858 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Fariborz Jahanian60462092010-04-08 00:30:06 +0000861 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000862 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000863 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000864 if (getLang().ObjC2)
865 MaybeParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000866
Chris Lattner5700fab2007-10-07 02:00:24 +0000867 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000868 Decl *Result
John McCall28a6aea2009-11-04 02:18:39 +0000869 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000870 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000871 0,
872 CParamInfo.data(), CParamInfo.size(),
John McCall53fa7142010-12-24 02:08:15 +0000873 attrs.getList(), MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000874 PD.complete(Result);
875 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000876 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000877
Steve Narofff73590d2007-09-27 14:38:14 +0000878 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallfaf5fb42010-08-26 23:41:50 +0000879 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner5700fab2007-10-07 02:00:24 +0000881 while (1) {
John McCallfaf5fb42010-08-26 23:41:50 +0000882 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner5700fab2007-10-07 02:00:24 +0000884 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000885 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000886 Diag(Tok, diag::err_expected_colon);
887 break;
888 }
889 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000890
John McCallba7bf592010-08-24 05:47:05 +0000891 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000892 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor99fa2642010-08-24 01:06:58 +0000893 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000894
Chris Lattner5700fab2007-10-07 02:00:24 +0000895 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000896 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +0000897 if (getLang().ObjC2) {
898 ParsedAttributes attrs;
899 MaybeParseGNUAttributes(attrs);
900 ArgInfo.ArgAttrs = attrs.getList();
901 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000902
Douglas Gregor45879692010-07-08 23:37:41 +0000903 // Code completion for the next piece of the selector.
904 if (Tok.is(tok::code_completion)) {
905 ConsumeCodeCompletionToken();
906 KeyIdents.push_back(SelIdent);
907 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
908 mType == tok::minus,
909 /*AtParameterName=*/true,
910 ReturnType,
911 KeyIdents.data(),
912 KeyIdents.size());
913 KeyIdents.pop_back();
914 break;
915 }
916
Chris Lattner0ef13522007-10-09 17:51:17 +0000917 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000918 Diag(Tok, diag::err_expected_ident); // missing argument name.
919 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000922 ArgInfo.Name = Tok.getIdentifierInfo();
923 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000924 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000926 ArgInfos.push_back(ArgInfo);
927 KeyIdents.push_back(SelIdent);
928
Douglas Gregor95887f92010-07-08 23:20:03 +0000929 // Code completion for the next piece of the selector.
930 if (Tok.is(tok::code_completion)) {
931 ConsumeCodeCompletionToken();
932 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
933 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000934 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000935 ReturnType,
936 KeyIdents.data(),
937 KeyIdents.size());
938 break;
939 }
940
Chris Lattner5700fab2007-10-07 02:00:24 +0000941 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000942 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000943 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000944 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000945 break;
946 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000949 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000950
Chris Lattner5700fab2007-10-07 02:00:24 +0000951 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000952 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000953 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000954 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000955 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000956 ConsumeToken();
957 break;
958 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000959 DeclSpec DS;
960 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000961 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000962 Declarator ParmDecl(DS, Declarator::PrototypeContext);
963 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000964 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +0000965 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000966 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
967 ParmDecl.getIdentifierLoc(),
968 Param,
969 0));
970
Chris Lattner5700fab2007-10-07 02:00:24 +0000971 }
Mike Stump11289f42009-09-09 15:08:12 +0000972
Cameron Esfahanif6c73c42010-10-12 00:21:25 +0000973 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000974 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000975 if (getLang().ObjC2)
976 MaybeParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000977
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000978 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +0000979 return 0;
Chris Lattner5700fab2007-10-07 02:00:24 +0000980 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
981 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +0000982 Decl *Result
John McCall28a6aea2009-11-04 02:18:39 +0000983 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000984 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000985 &ArgInfos[0],
986 CParamInfo.data(), CParamInfo.size(),
John McCall53fa7142010-12-24 02:08:15 +0000987 attrs.getList(),
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000988 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000989 PD.complete(Result);
990 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000991}
992
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000993/// objc-protocol-refs:
994/// '<' identifier-list '>'
995///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000996bool Parser::
John McCall48871652010-08-21 09:40:31 +0000997ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000998 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
999 bool WarnOnDeclarations,
1000 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001001 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001002
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001003 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001004
Chris Lattner3bbae002008-07-26 04:03:38 +00001005 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001006
Chris Lattner3bbae002008-07-26 04:03:38 +00001007 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001008 if (Tok.is(tok::code_completion)) {
1009 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1010 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001011 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +00001012 }
1013
Chris Lattner3bbae002008-07-26 04:03:38 +00001014 if (Tok.isNot(tok::identifier)) {
1015 Diag(Tok, diag::err_expected_ident);
1016 SkipUntil(tok::greater);
1017 return true;
1018 }
1019 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1020 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001021 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001022 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner3bbae002008-07-26 04:03:38 +00001024 if (Tok.isNot(tok::comma))
1025 break;
1026 ConsumeToken();
1027 }
Mike Stump11289f42009-09-09 15:08:12 +00001028
Chris Lattner3bbae002008-07-26 04:03:38 +00001029 // Consume the '>'.
1030 if (Tok.isNot(tok::greater)) {
1031 Diag(Tok, diag::err_expected_greater);
1032 return true;
1033 }
Mike Stump11289f42009-09-09 15:08:12 +00001034
Chris Lattner3bbae002008-07-26 04:03:38 +00001035 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner3bbae002008-07-26 04:03:38 +00001037 // Convert the list of protocols identifiers into a list of protocol decls.
1038 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1039 &ProtocolIdents[0], ProtocolIdents.size(),
1040 Protocols);
1041 return false;
1042}
1043
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001044/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1045/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001046bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001047 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1048 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1049 SourceLocation LAngleLoc, EndProtoLoc;
1050 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1051 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001052 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1053 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001054 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1055 ProtocolLocs.data(), LAngleLoc);
1056 if (EndProtoLoc.isValid())
1057 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001058 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001059}
1060
1061
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001062/// objc-class-instance-variables:
1063/// '{' objc-instance-variable-decl-list[opt] '}'
1064///
1065/// objc-instance-variable-decl-list:
1066/// objc-visibility-spec
1067/// objc-instance-variable-decl ';'
1068/// ';'
1069/// objc-instance-variable-decl-list objc-visibility-spec
1070/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1071/// objc-instance-variable-decl-list ';'
1072///
1073/// objc-visibility-spec:
1074/// @private
1075/// @protected
1076/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001077/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001078///
1079/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001080/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001081///
John McCall48871652010-08-21 09:40:31 +00001082void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001083 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001084 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001085 assert(Tok.is(tok::l_brace) && "expected {");
John McCall48871652010-08-21 09:40:31 +00001086 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001087
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001088 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001089
Steve Naroff00433d32007-08-21 21:17:12 +00001090 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001091
Steve Naroff00433d32007-08-21 21:17:12 +00001092 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001093 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001094 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001095
Steve Naroff00433d32007-08-21 21:17:12 +00001096 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001097 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001098 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001099 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001100 ConsumeToken();
1101 continue;
1102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Steve Naroff00433d32007-08-21 21:17:12 +00001104 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001105 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001106 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001107
1108 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001109 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001110 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001111 }
1112
Steve Naroff7c348172007-08-23 18:16:40 +00001113 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001114 case tok::objc_private:
1115 case tok::objc_public:
1116 case tok::objc_protected:
1117 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001118 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001119 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001120 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001121 default:
1122 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001123 continue;
1124 }
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Douglas Gregor48d46252010-01-13 21:54:15 +00001127 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001128 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001129 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001130 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001131 }
1132
John McCallcfefb6d2009-11-03 02:38:08 +00001133 struct ObjCIvarCallback : FieldCallback {
1134 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001135 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001136 tok::ObjCKeywordKind visibility;
John McCall48871652010-08-21 09:40:31 +00001137 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001138
John McCall48871652010-08-21 09:40:31 +00001139 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1140 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001141 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1142 }
1143
John McCall48871652010-08-21 09:40:31 +00001144 Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001145 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001146 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001147 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001148 FD.D.getDeclSpec().getSourceRange().getBegin(),
1149 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001150 if (Field)
1151 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001152 return Field;
1153 }
1154 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001155
Chris Lattnera12405b2008-04-10 06:46:29 +00001156 // Parse all the comma separated declarators.
1157 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001158 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001159
Chris Lattner0ef13522007-10-09 17:51:17 +00001160 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001161 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001162 } else {
1163 Diag(Tok, diag::err_expected_semi_decl_list);
1164 // Skip to end of block or statement
1165 SkipUntil(tok::r_brace, true, true);
1166 }
1167 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001168 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001169 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001170 // Call ActOnFields() even if we don't have any decls. This is useful
1171 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001172 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001173 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001174 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001175 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001176}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001177
1178/// objc-protocol-declaration:
1179/// objc-protocol-definition
1180/// objc-protocol-forward-reference
1181///
1182/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001183/// @protocol identifier
1184/// objc-protocol-refs[opt]
1185/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001186/// @end
1187///
1188/// objc-protocol-forward-reference:
1189/// @protocol identifier-list ';'
1190///
1191/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001192/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001193/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001194Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001195 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001196 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001197 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1198 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001199
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001200 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001201 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001202 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001203 }
1204
Chris Lattner0ef13522007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001206 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001207 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001208 }
1209 // Save the protocol name, then consume it.
1210 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1211 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner0ef13522007-10-09 17:51:17 +00001213 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001214 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001215 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001216 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001217 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001218 }
Mike Stump11289f42009-09-09 15:08:12 +00001219
Chris Lattner0ef13522007-10-09 17:51:17 +00001220 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001221 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1222 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1223
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001224 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001225 while (1) {
1226 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001227 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001228 Diag(Tok, diag::err_expected_ident);
1229 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001230 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001231 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001232 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1233 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001234 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001235
Chris Lattner0ef13522007-10-09 17:51:17 +00001236 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001237 break;
1238 }
1239 // Consume the ';'.
1240 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001241 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001242
Steve Naroff93eb5f12007-10-10 17:32:04 +00001243 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001244 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001245 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001246 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001247 }
Mike Stump11289f42009-09-09 15:08:12 +00001248
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001249 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001250 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001251
John McCall48871652010-08-21 09:40:31 +00001252 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001253 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001254 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001255 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1256 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001257 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001258
John McCall48871652010-08-21 09:40:31 +00001259 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001260 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001261 ProtocolRefs.data(),
1262 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001263 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001264 EndProtoLoc, attrs.getList());
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001265 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001266 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001267}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001268
1269/// objc-implementation:
1270/// objc-class-implementation-prologue
1271/// objc-category-implementation-prologue
1272///
1273/// objc-class-implementation-prologue:
1274/// @implementation identifier objc-superclass[opt]
1275/// objc-class-instance-variables[opt]
1276///
1277/// objc-category-implementation-prologue:
1278/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001279Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001280 SourceLocation atLoc) {
1281 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1282 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1283 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001284
Douglas Gregor49c22a72009-11-18 16:26:39 +00001285 // Code completion after '@implementation'.
1286 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001287 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001288 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001289 }
1290
Chris Lattner0ef13522007-10-09 17:51:17 +00001291 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001292 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001293 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001294 }
1295 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001296 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001297 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001298
1299 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001300 // we have a category implementation.
1301 SourceLocation lparenLoc = ConsumeParen();
1302 SourceLocation categoryLoc, rparenLoc;
1303 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001304
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001305 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001306 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001307 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001308 }
1309
Chris Lattner0ef13522007-10-09 17:51:17 +00001310 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001311 categoryId = Tok.getIdentifierInfo();
1312 categoryLoc = ConsumeToken();
1313 } else {
1314 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001315 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001316 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001317 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001318 Diag(Tok, diag::err_expected_rparen);
1319 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001320 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001321 }
1322 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001323 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001324 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001325 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001326 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001327 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001328 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001329 }
1330 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001331 SourceLocation superClassLoc;
1332 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001333 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001334 // We have a super class
1335 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001336 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001337 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001338 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001339 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001340 superClassId = Tok.getIdentifierInfo();
1341 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001342 }
John McCall48871652010-08-21 09:40:31 +00001343 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001344 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001345 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001346
Steve Naroff33a1e802007-10-29 21:38:07 +00001347 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001348 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahaniana9effb52010-03-22 19:04:14 +00001349 tok::objc_private, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001350 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001351 PendingObjCImpDecl.push_back(ObjCImpDecl);
1352
John McCall48871652010-08-21 09:40:31 +00001353 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001354}
Steve Naroff33a1e802007-10-29 21:38:07 +00001355
John McCall48871652010-08-21 09:40:31 +00001356Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001357 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1358 "ParseObjCAtEndDeclaration(): Expected @end");
John McCall48871652010-08-21 09:40:31 +00001359 Decl *Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001360 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001361 if (ObjCImpDecl) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001362 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001363 ObjCImpDecl = 0;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001364 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001365 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001366 else {
1367 // missing @implementation
1368 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1369 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001370 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001371}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001372
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001373Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1374 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001375 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001376 return Actions.ConvertDeclToDeclGroup(0);
1377 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001378 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001379 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1380}
1381
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001382/// compatibility-alias-decl:
1383/// @compatibility_alias alias-name class-name ';'
1384///
John McCall48871652010-08-21 09:40:31 +00001385Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001386 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1387 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1388 ConsumeToken(); // consume compatibility_alias
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 *aliasId = Tok.getIdentifierInfo();
1394 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001395 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001396 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001397 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001398 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001399 IdentifierInfo *classId = Tok.getIdentifierInfo();
1400 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1401 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001402 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCall48871652010-08-21 09:40:31 +00001403 return 0;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001404 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001405 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1406 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001407}
1408
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001409/// property-synthesis:
1410/// @synthesize property-ivar-list ';'
1411///
1412/// property-ivar-list:
1413/// property-ivar
1414/// property-ivar-list ',' property-ivar
1415///
1416/// property-ivar:
1417/// identifier
1418/// identifier '=' identifier
1419///
John McCall48871652010-08-21 09:40:31 +00001420Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001421 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1422 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001423 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001424
Douglas Gregor88e72a02009-11-18 19:45:45 +00001425 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001426 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001427 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001428 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001429 }
1430
Douglas Gregor88e72a02009-11-18 19:45:45 +00001431 if (Tok.isNot(tok::identifier)) {
1432 Diag(Tok, diag::err_synthesized_property_name);
1433 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001434 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001435 }
1436
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001437 IdentifierInfo *propertyIvar = 0;
1438 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1439 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001440 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001441 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001442 // property '=' ivar-name
1443 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001444
1445 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001446 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor5d649882009-11-18 22:32:06 +00001447 ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001448 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001449 }
1450
Chris Lattner0ef13522007-10-09 17:51:17 +00001451 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001452 Diag(Tok, diag::err_expected_ident);
1453 break;
1454 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001455 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001456 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001457 }
Douglas Gregor0be31a22010-07-02 17:43:08 +00001458 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001459 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001460 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001461 break;
1462 ConsumeToken(); // consume ','
1463 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001464 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001465 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001466 SkipUntil(tok::semi);
1467 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001468 else
1469 ConsumeToken(); // consume ';'
John McCall48871652010-08-21 09:40:31 +00001470 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001471}
1472
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001473/// property-dynamic:
1474/// @dynamic property-list
1475///
1476/// property-list:
1477/// identifier
1478/// property-list ',' identifier
1479///
John McCall48871652010-08-21 09:40:31 +00001480Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001481 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1482 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1483 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001484 while (true) {
1485 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001486 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001487 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001488 }
1489
1490 if (Tok.isNot(tok::identifier)) {
1491 Diag(Tok, diag::err_expected_ident);
1492 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001493 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001494 }
1495
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001496 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1497 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor0be31a22010-07-02 17:43:08 +00001498 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001499 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001500
Chris Lattner0ef13522007-10-09 17:51:17 +00001501 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001502 break;
1503 ConsumeToken(); // consume ','
1504 }
Fariborz Jahanianb5795c02010-04-14 20:52:42 +00001505 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001506 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianb5795c02010-04-14 20:52:42 +00001507 SkipUntil(tok::semi);
1508 }
1509 else
1510 ConsumeToken(); // consume ';'
John McCall48871652010-08-21 09:40:31 +00001511 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001512}
Mike Stump11289f42009-09-09 15:08:12 +00001513
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001514/// objc-throw-statement:
1515/// throw expression[opt];
1516///
John McCalldadc5752010-08-24 06:29:42 +00001517StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1518 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001519 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001520 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001521 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001522 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001523 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001524 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001525 }
1526 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001527 // consume ';'
1528 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001529 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001530}
1531
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001532/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001533/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001534///
John McCalldadc5752010-08-24 06:29:42 +00001535StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001536Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001537 ConsumeToken(); // consume synchronized
1538 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001539 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001540 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001541 }
1542 ConsumeParen(); // '('
John McCalldadc5752010-08-24 06:29:42 +00001543 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001544 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001545 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001546 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001547 }
1548 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001549 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001550 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001551 }
1552 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001553 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001554 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001555 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001556 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001557 // Enter a scope to hold everything within the compound stmt. Compound
1558 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001559 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001560
John McCalldadc5752010-08-24 06:29:42 +00001561 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001562
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001563 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001564 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001565 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCallb268a282010-08-23 23:25:46 +00001566 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001567}
1568
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001569/// objc-try-catch-statement:
1570/// @try compound-statement objc-catch-list[opt]
1571/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1572///
1573/// objc-catch-list:
1574/// @catch ( parameter-declaration ) compound-statement
1575/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1576/// catch-parameter-declaration:
1577/// parameter-declaration
1578/// '...' [OBJC2]
1579///
John McCalldadc5752010-08-24 06:29:42 +00001580StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001581 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001582
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001583 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001584 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001585 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001586 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001587 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001588 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001589 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001590 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001591 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001592 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001593 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001594 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001595
Chris Lattner0ef13522007-10-09 17:51:17 +00001596 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001597 // At this point, we need to lookahead to determine if this @ is the start
1598 // of an @catch or @finally. We don't want to consume the @ token if this
1599 // is an @try or @encode or something else.
1600 Token AfterAt = GetLookAheadToken(1);
1601 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1602 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1603 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001604
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001605 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001606 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001607 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001608 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001609 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001610 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001611 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001612 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001613 DeclSpec DS;
1614 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001615 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001616 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001617 // we must accept either 'declarator' or 'abstract-declarator' here.
1618 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1619 ParseDeclarator(ParmDecl);
1620
Douglas Gregore11ee112010-04-23 23:01:43 +00001621 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001622 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001623 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001624 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001625 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001626
Steve Naroff65a00892009-04-07 22:56:58 +00001627 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001628
Steve Naroff65a00892009-04-07 22:56:58 +00001629 if (Tok.is(tok::r_paren))
1630 RParenLoc = ConsumeParen();
1631 else // Skip over garbage, until we get to ')'. Eat the ')'.
1632 SkipUntil(tok::r_paren, true, false);
1633
John McCalldadc5752010-08-24 06:29:42 +00001634 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001635 if (Tok.is(tok::l_brace))
1636 CatchBody = ParseCompoundStatementBody();
1637 else
1638 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001639 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001640 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001641
John McCalldadc5752010-08-24 06:29:42 +00001642 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001643 RParenLoc,
1644 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001645 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001646 if (!Catch.isInvalid())
1647 CatchStmts.push_back(Catch.release());
1648
Steve Naroffe6016792008-02-05 21:27:35 +00001649 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001650 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1651 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001652 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001653 }
1654 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001655 } else {
1656 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001657 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001658 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001659
John McCalldadc5752010-08-24 06:29:42 +00001660 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001661 if (Tok.is(tok::l_brace))
1662 FinallyBody = ParseCompoundStatementBody();
1663 else
1664 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001665 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001666 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001667 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001668 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001669 catch_or_finally_seen = true;
1670 break;
1671 }
1672 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001673 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001674 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001675 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001676 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001677
John McCallb268a282010-08-23 23:25:46 +00001678 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001679 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001680 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001681}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001682
Steve Naroff09bf8152007-09-06 21:24:23 +00001683/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001684///
John McCall48871652010-08-21 09:40:31 +00001685Decl *Parser::ParseObjCMethodDefinition() {
1686 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001687
John McCallfaf5fb42010-08-26 23:41:50 +00001688 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1689 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001690
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001691 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001692 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001693 if (ObjCImpDecl) {
1694 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001695 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001696 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001697 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001698 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001699
Steve Naroffbb875722007-11-11 19:54:21 +00001700 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001701 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001702 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001703
Steve Naroffbb875722007-11-11 19:54:21 +00001704 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1705 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001706
Steve Naroffbb875722007-11-11 19:54:21 +00001707 // If we didn't find the '{', bail out.
1708 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001709 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001710 }
Steve Naroffbb875722007-11-11 19:54:21 +00001711 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001712
Steve Naroffbb875722007-11-11 19:54:21 +00001713 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001714 ParseScope BodyScope(this,
1715 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001716
Steve Naroffbb875722007-11-11 19:54:21 +00001717 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001718 // specified Declarator for the method.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001719 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001720
John McCalldadc5752010-08-24 06:29:42 +00001721 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001722
Steve Naroffbb875722007-11-11 19:54:21 +00001723 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001724 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001725 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1726 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001727
Steve Naroffb94d7f62009-03-02 22:00:56 +00001728 // TODO: Pass argument information.
John McCallb268a282010-08-23 23:25:46 +00001729 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump11289f42009-09-09 15:08:12 +00001730
Steve Naroffbb875722007-11-11 19:54:21 +00001731 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001732 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001733
Steve Naroff7b8fa472007-11-13 23:01:27 +00001734 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001735}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001736
John McCalldadc5752010-08-24 06:29:42 +00001737StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001738 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001739 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001740 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001741 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001742 }
1743
1744 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001745 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001746
1747 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001748 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001749
1750 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001751 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001752
John McCalldadc5752010-08-24 06:29:42 +00001753 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001754 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001755 // If the expression is invalid, skip ahead to the next semicolon. Not
1756 // doing this opens us up to the possibility of infinite loops if
1757 // ParseExpression does not consume any tokens.
1758 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001759 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001760 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001761
Steve Naroffe6016792008-02-05 21:27:35 +00001762 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001763 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001764 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001765}
1766
John McCalldadc5752010-08-24 06:29:42 +00001767ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001768 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001769 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001770 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001771 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001772 return ExprError();
1773
Chris Lattnere002fbe2007-12-12 01:04:12 +00001774 case tok::string_literal: // primary-expression: string-literal
1775 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001776 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001777 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001778 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001779 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001780
Chris Lattner197a3012008-08-05 06:19:09 +00001781 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1782 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001783 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001784 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001785 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001786 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001787 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001788 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001789 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001790 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001791 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001792}
1793
Douglas Gregor8d4de672010-04-21 22:36:40 +00001794/// \brirg Parse the receiver of an Objective-C++ message send.
1795///
1796/// This routine parses the receiver of a message send in
1797/// Objective-C++ either as a type or as an expression. Note that this
1798/// routine must not be called to parse a send to 'super', since it
1799/// has no way to return such a result.
1800///
1801/// \param IsExpr Whether the receiver was parsed as an expression.
1802///
1803/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1804/// IsExpr is true), the parsed expression. If the receiver was parsed
1805/// as a type (\c IsExpr is false), the parsed type.
1806///
1807/// \returns True if an error occurred during parsing or semantic
1808/// analysis, in which case the arguments do not have valid
1809/// values. Otherwise, returns false for a successful parse.
1810///
1811/// objc-receiver: [C++]
1812/// 'super' [not parsed here]
1813/// expression
1814/// simple-type-specifier
1815/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001816bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001817 InMessageExpressionRAIIObject InMessage(*this, true);
1818
Douglas Gregor8d4de672010-04-21 22:36:40 +00001819 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1820 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1821 TryAnnotateTypeOrScopeToken();
1822
1823 if (!isCXXSimpleTypeSpecifier()) {
1824 // objc-receiver:
1825 // expression
John McCalldadc5752010-08-24 06:29:42 +00001826 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001827 if (Receiver.isInvalid())
1828 return true;
1829
1830 IsExpr = true;
1831 TypeOrExpr = Receiver.take();
1832 return false;
1833 }
1834
1835 // objc-receiver:
1836 // typename-specifier
1837 // simple-type-specifier
1838 // expression (that starts with one of the above)
1839 DeclSpec DS;
1840 ParseCXXSimpleTypeSpecifier(DS);
1841
1842 if (Tok.is(tok::l_paren)) {
1843 // If we see an opening parentheses at this point, we are
1844 // actually parsing an expression that starts with a
1845 // function-style cast, e.g.,
1846 //
1847 // postfix-expression:
1848 // simple-type-specifier ( expression-list [opt] )
1849 // typename-specifier ( expression-list [opt] )
1850 //
1851 // Parse the remainder of this case, then the (optional)
1852 // postfix-expression suffix, followed by the (optional)
1853 // right-hand side of the binary expression. We have an
1854 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001855 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001856 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001857 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001858 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001859 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001860 if (Receiver.isInvalid())
1861 return true;
1862
1863 IsExpr = true;
1864 TypeOrExpr = Receiver.take();
1865 return false;
1866 }
1867
1868 // We have a class message. Turn the simple-type-specifier or
1869 // typename-specifier we parsed into a type and parse the
1870 // remainder of the class message.
1871 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001872 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001873 if (Type.isInvalid())
1874 return true;
1875
1876 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001877 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001878 return false;
1879}
1880
Douglas Gregor990ccac2010-05-31 14:40:22 +00001881/// \brief Determine whether the parser is currently referring to a an
1882/// Objective-C message send, using a simplified heuristic to avoid overhead.
1883///
1884/// This routine will only return true for a subset of valid message-send
1885/// expressions.
1886bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001887 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001888 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001889 return GetLookAheadToken(1).is(tok::identifier) &&
1890 GetLookAheadToken(2).is(tok::identifier);
1891}
1892
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001893bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1894 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1895 InMessageExpression)
1896 return false;
1897
1898
1899 ParsedType Type;
1900
1901 if (Tok.is(tok::annot_typename))
1902 Type = getTypeAnnotation(Tok);
1903 else if (Tok.is(tok::identifier))
1904 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1905 getCurScope());
1906 else
1907 return false;
1908
1909 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1910 const Token &AfterNext = GetLookAheadToken(2);
1911 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1912 if (Tok.is(tok::identifier))
1913 TryAnnotateTypeOrScopeToken();
1914
1915 return Tok.is(tok::annot_typename);
1916 }
1917 }
1918
1919 return false;
1920}
1921
Mike Stump11289f42009-09-09 15:08:12 +00001922/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001923/// '[' objc-receiver objc-message-args ']'
1924///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001925/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00001926/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001927/// expression
1928/// class-name
1929/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00001930///
John McCalldadc5752010-08-24 06:29:42 +00001931ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001932 assert(Tok.is(tok::l_square) && "'[' expected");
1933 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1934
Douglas Gregora817a192010-05-27 23:06:34 +00001935 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001936 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregora817a192010-05-27 23:06:34 +00001937 ConsumeCodeCompletionToken();
1938 SkipUntil(tok::r_square);
1939 return ExprError();
1940 }
1941
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001942 InMessageExpressionRAIIObject InMessage(*this, true);
1943
Douglas Gregor8d4de672010-04-21 22:36:40 +00001944 if (getLang().CPlusPlus) {
1945 // We completely separate the C and C++ cases because C++ requires
1946 // more complicated (read: slower) parsing.
1947
1948 // Handle send to super.
1949 // FIXME: This doesn't benefit from the same typo-correction we
1950 // get in Objective-C.
1951 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001952 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00001953 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1954 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001955
1956 // Parse the receiver, which is either a type or an expression.
1957 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00001958 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00001959 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1960 SkipUntil(tok::r_square);
1961 return ExprError();
1962 }
1963
1964 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00001965 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1966 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00001967 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00001968
1969 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00001970 ParsedType::getFromOpaquePtr(TypeOrExpr),
1971 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00001972 }
1973
1974 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00001975 IdentifierInfo *Name = Tok.getIdentifierInfo();
1976 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00001977 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001978 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00001979 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00001980 NextToken().is(tok::period),
1981 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001982 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00001983 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1984 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001985
John McCallfaf5fb42010-08-26 23:41:50 +00001986 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00001987 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001988 SkipUntil(tok::r_square);
1989 return ExprError();
1990 }
1991
Douglas Gregore5798dc2010-04-21 20:38:13 +00001992 ConsumeToken(); // the type name
1993
1994 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00001995 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00001996
John McCallfaf5fb42010-08-26 23:41:50 +00001997 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001998 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00001999 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002000 }
Chris Lattner8f697062008-01-25 18:59:06 +00002001 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002002
2003 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002004 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002005 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002006 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002007 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002008 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002009
John McCallba7bf592010-08-24 05:47:05 +00002010 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2011 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002012}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002013
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002014/// \brief Parse the remainder of an Objective-C message following the
2015/// '[' objc-receiver.
2016///
2017/// This routine handles sends to super, class messages (sent to a
2018/// class name), and instance messages (sent to an object), and the
2019/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2020/// ReceiverExpr, respectively. Only one of these parameters may have
2021/// a valid value.
2022///
2023/// \param LBracLoc The location of the opening '['.
2024///
2025/// \param SuperLoc If this is a send to 'super', the location of the
2026/// 'super' keyword that indicates a send to the superclass.
2027///
2028/// \param ReceiverType If this is a class message, the type of the
2029/// class we are sending a message to.
2030///
2031/// \param ReceiverExpr If this is an instance message, the expression
2032/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002033///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002034/// objc-message-args:
2035/// objc-selector
2036/// objc-keywordarg-list
2037///
2038/// objc-keywordarg-list:
2039/// objc-keywordarg
2040/// objc-keywordarg-list objc-keywordarg
2041///
Mike Stump11289f42009-09-09 15:08:12 +00002042/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002043/// selector-name[opt] ':' objc-keywordexpr
2044///
2045/// objc-keywordexpr:
2046/// nonempty-expr-list
2047///
2048/// nonempty-expr-list:
2049/// assignment-expression
2050/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002051///
John McCalldadc5752010-08-24 06:29:42 +00002052ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002053Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002054 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002055 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002056 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002057 InMessageExpressionRAIIObject InMessage(*this, true);
2058
Steve Naroffeae65032009-11-07 02:08:14 +00002059 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002060 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002061 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2062 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002063 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002064 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2065 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002066 else
John McCallb268a282010-08-23 23:25:46 +00002067 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002068 0, 0, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002069 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00002070 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002071
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002072 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002073 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002074 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002075
Anders Carlsson978f08d2009-02-14 18:21:46 +00002076 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002077
Steve Narofff73590d2007-09-27 14:38:14 +00002078 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002079 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002080
Chris Lattner0ef13522007-10-09 17:51:17 +00002081 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002082 while (1) {
2083 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002084 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002085
Chris Lattner0ef13522007-10-09 17:51:17 +00002086 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002087 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002088 // We must manually skip to a ']', otherwise the expression skipper will
2089 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2090 // the enclosing expression.
2091 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002092 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002093 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002094
Steve Narofff73590d2007-09-27 14:38:14 +00002095 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002096 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002097
2098 if (Tok.is(tok::code_completion)) {
2099 if (SuperLoc.isValid())
2100 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2101 KeyIdents.data(),
2102 KeyIdents.size(),
2103 /*AtArgumentEpression=*/true);
2104 else if (ReceiverType)
2105 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2106 KeyIdents.data(),
2107 KeyIdents.size(),
2108 /*AtArgumentEpression=*/true);
2109 else
2110 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2111 KeyIdents.data(),
2112 KeyIdents.size(),
2113 /*AtArgumentEpression=*/true);
2114
2115 ConsumeCodeCompletionToken();
2116 SkipUntil(tok::r_square);
2117 return ExprError();
2118 }
2119
John McCalldadc5752010-08-24 06:29:42 +00002120 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002121 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002122 // We must manually skip to a ']', otherwise the expression skipper will
2123 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2124 // the enclosing expression.
2125 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002126 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002127 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002128
Steve Naroff486760a2007-09-17 20:25:27 +00002129 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002130 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002131
Douglas Gregor1b605f72009-11-19 01:08:35 +00002132 // Code completion after each argument.
2133 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002134 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002135 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002136 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002137 KeyIdents.size(),
2138 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002139 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002140 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002141 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002142 KeyIdents.size(),
2143 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002144 else
John McCallb268a282010-08-23 23:25:46 +00002145 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002146 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002147 KeyIdents.size(),
2148 /*AtArgumentEpression=*/false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002149 ConsumeCodeCompletionToken();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002150 SkipUntil(tok::r_square);
2151 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002152 }
2153
Steve Naroff486760a2007-09-17 20:25:27 +00002154 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002155 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002156 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002157 break;
2158 // We have a selector or a colon, continue parsing.
2159 }
2160 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002161 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002162 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002163 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002164 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002165 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002166 // We must manually skip to a ']', otherwise the expression skipper will
2167 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2168 // the enclosing expression.
2169 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002170 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002171 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002172
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002173 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002174 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002175 }
2176 } else if (!selIdent) {
2177 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002178
Chris Lattner197a3012008-08-05 06:19:09 +00002179 // We must manually skip to a ']', otherwise the expression skipper will
2180 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2181 // the enclosing expression.
2182 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002183 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002184 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002185
Chris Lattner0ef13522007-10-09 17:51:17 +00002186 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002187 if (Tok.is(tok::identifier))
2188 Diag(Tok, diag::err_expected_colon);
2189 else
2190 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002191 // We must manually skip to a ']', otherwise the expression skipper will
2192 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2193 // the enclosing expression.
2194 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002195 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002196 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002197
Chris Lattner8f697062008-01-25 18:59:06 +00002198 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002199
Steve Naroffe61bfa82007-10-05 18:42:47 +00002200 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002201 if (nKeys == 0)
2202 KeyIdents.push_back(selIdent);
2203 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002204
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002205 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002206 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002207 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002208 MultiExprArg(Actions,
2209 KeyExprs.take(),
2210 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002211 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002212 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002213 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002214 MultiExprArg(Actions,
2215 KeyExprs.take(),
2216 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002217 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002218 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002219 MultiExprArg(Actions,
2220 KeyExprs.take(),
2221 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002222}
2223
John McCalldadc5752010-08-24 06:29:42 +00002224ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2225 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002226 if (Res.isInvalid()) return move(Res);
2227
Chris Lattnere002fbe2007-12-12 01:04:12 +00002228 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2229 // expressions. At this point, we know that the only valid thing that starts
2230 // with '@' is an @"".
2231 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002232 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002233 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002234 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002235
Chris Lattnere002fbe2007-12-12 01:04:12 +00002236 while (Tok.is(tok::at)) {
2237 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002238
Sebastian Redlc13f2682008-12-09 20:22:58 +00002239 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002240 if (!isTokenStringLiteral())
2241 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002242
John McCalldadc5752010-08-24 06:29:42 +00002243 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002244 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002245 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002246
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002247 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002248 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002249
2250 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2251 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002252}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002253
2254/// objc-encode-expression:
2255/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002256ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002257Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002258 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002259
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002260 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002261
Chris Lattner197a3012008-08-05 06:19:09 +00002262 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002263 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2264
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002265 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002266
Douglas Gregor220cac52009-02-18 17:45:20 +00002267 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002268
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002269 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002270
Douglas Gregor220cac52009-02-18 17:45:20 +00002271 if (Ty.isInvalid())
2272 return ExprError();
2273
Mike Stump11289f42009-09-09 15:08:12 +00002274 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002275 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002276}
Anders Carlssone01493d2007-08-23 15:25:28 +00002277
2278/// objc-protocol-expression
2279/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002280ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002281Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002282 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002283
Chris Lattner197a3012008-08-05 06:19:09 +00002284 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002285 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2286
Anders Carlssone01493d2007-08-23 15:25:28 +00002287 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002288
Chris Lattner197a3012008-08-05 06:19:09 +00002289 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002290 return ExprError(Diag(Tok, diag::err_expected_ident));
2291
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002292 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002293 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002294
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002295 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002296
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002297 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2298 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002299}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002300
2301/// objc-selector-expression
2302/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002303ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002304 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002305
Chris Lattner197a3012008-08-05 06:19:09 +00002306 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002307 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2308
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002309 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002310 SourceLocation LParenLoc = ConsumeParen();
2311 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002312
2313 if (Tok.is(tok::code_completion)) {
2314 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2315 KeyIdents.size());
2316 ConsumeCodeCompletionToken();
2317 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2318 return ExprError();
2319 }
2320
Chris Lattner4f472a32009-04-11 18:13:45 +00002321 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002322 if (!SelIdent && // missing selector name.
2323 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002324 return ExprError(Diag(Tok, diag::err_expected_ident));
2325
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002326 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002327 unsigned nColons = 0;
2328 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002329 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002330 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2331 ++nColons;
2332 KeyIdents.push_back(0);
2333 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002334 return ExprError(Diag(Tok, diag::err_expected_colon));
2335
Chris Lattner1ba64452010-08-27 22:32:41 +00002336 ++nColons;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002337 ConsumeToken(); // Eat the ':'.
2338 if (Tok.is(tok::r_paren))
2339 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002340
2341 if (Tok.is(tok::code_completion)) {
2342 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2343 KeyIdents.size());
2344 ConsumeCodeCompletionToken();
2345 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2346 return ExprError();
2347 }
2348
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002349 // Check for another keyword selector.
2350 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002351 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002352 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002353 if (!SelIdent && Tok.isNot(tok::colon))
2354 break;
2355 }
Steve Naroff152dd812007-12-05 22:21:29 +00002356 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002357 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002358 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002359 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2360 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002361 }