blob: 17c962e2b572585398a1426b7d588f8a8e144501 [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'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy 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)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +000038 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000039
40 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000041 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000042 case tok::objc_class:
43 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000044 break;
John McCall53fa7142010-12-24 02:08:15 +000045 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000046 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000047 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
48 break;
John McCall53fa7142010-12-24 02:08:15 +000049 }
50 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000051 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000052 SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
53 break;
John McCall53fa7142010-12-24 02:08:15 +000054 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000055 case tok::objc_implementation:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000056 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
57 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_end:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000059 SingleDecl = ParseObjCAtEndDeclaration(AtLoc);
60 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000061 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000062 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
63 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000064 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000065 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
66 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000067 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000068 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
69 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000070 default:
71 Diag(AtLoc, diag::err_unexpected_at);
72 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000073 SingleDecl = 0;
74 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000076 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077}
78
79///
Mike Stump11289f42009-09-09 15:08:12 +000080/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000081/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000082///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000083Parser::DeclGroupPtrTy
84Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000085 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000086 SmallVector<IdentifierInfo *, 8> ClassNames;
87 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +000088
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattnerda59c2f2006-11-05 02:08:13 +000090 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000091 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000092 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000093 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000094 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000096 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000097 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000099
Chris Lattner0ef13522007-10-09 17:51:17 +0000100 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000101 break;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000103 ConsumeToken();
104 }
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000106 // Consume the ';'.
107 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000108 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremeneka26da852009-11-17 23:12:20 +0000110 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
111 ClassLocs.data(),
112 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000113}
114
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000115///
116/// objc-interface:
117/// objc-class-interface-attributes[opt] objc-class-interface
118/// objc-category-interface
119///
120/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000121/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000122/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000123/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000124/// objc-interface-decl-list
125/// @end
126///
127/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000128/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000129/// objc-protocol-refs[opt]
130/// objc-interface-decl-list
131/// @end
132///
133/// objc-superclass:
134/// ':' identifier
135///
136/// objc-class-interface-attributes:
137/// __attribute__((visibility("default")))
138/// __attribute__((visibility("hidden")))
139/// __attribute__((deprecated))
140/// __attribute__((unavailable))
141/// __attribute__((objc_exception)) - used by NSException on 64-bit
142///
John McCall53fa7142010-12-24 02:08:15 +0000143Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
144 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000145 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000146 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
147 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000148
Douglas Gregor49c22a72009-11-18 16:26:39 +0000149 // Code completion after '@interface'.
150 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000151 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000152 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000153 }
154
Chris Lattner0ef13522007-10-09 17:51:17 +0000155 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000156 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000157 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000159
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000160 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000161 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000162 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000163 if (Tok.is(tok::l_paren) &&
164 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000165 // TODO(dgregor): Use the return value from the next line to provide better
166 // recovery.
167 ConsumeParen();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 SourceLocation categoryLoc, rparenLoc;
169 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000170 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000171 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000172 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000173 }
174
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000175 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000176 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000177 categoryId = Tok.getIdentifierInfo();
178 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000179 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000180 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000181 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000182 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000184 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000185 Diag(Tok, diag::err_expected_rparen);
186 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +0000187 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000188 }
189 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000190 // Next, we need to check for any protocol references.
191 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000192 SmallVector<Decl *, 8> ProtocolRefs;
193 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000194 if (Tok.is(tok::less) &&
195 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000196 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000197 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000198
John McCall53fa7142010-12-24 02:08:15 +0000199 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000200 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000201
John McCall48871652010-08-21 09:40:31 +0000202 Decl *CategoryType =
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000203 Actions.ActOnStartCategoryInterface(atLoc,
204 nameId, nameLoc,
205 categoryId, categoryLoc,
206 ProtocolRefs.data(),
207 ProtocolRefs.size(),
208 ProtocolLocs.data(),
209 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000210
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000211 if (Tok.is(tok::l_brace))
212 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
213
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000214 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000215 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000216 }
217 // Parse a class interface.
218 IdentifierInfo *superClassId = 0;
219 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000220
Chris Lattner0ef13522007-10-09 17:51:17 +0000221 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000222 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000223
224 // Code completion of superclass names.
225 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000226 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000227 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000228 }
229
Chris Lattner0ef13522007-10-09 17:51:17 +0000230 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000232 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000233 }
234 superClassId = Tok.getIdentifierInfo();
235 superClassLoc = ConsumeToken();
236 }
237 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000238 SmallVector<Decl *, 8> ProtocolRefs;
239 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000240 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000241 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000242 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
243 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000244 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000245
John McCall48871652010-08-21 09:40:31 +0000246 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000247 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000248 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000249 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000250 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000251 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000252
Chris Lattner0ef13522007-10-09 17:51:17 +0000253 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000254 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000255
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000256 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000257 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000258}
259
John McCall064d77b2009-12-03 22:31:13 +0000260/// The Objective-C property callback. This should be defined where
261/// it's used, but instead it's been lifted to here to support VS2005.
262struct Parser::ObjCPropertyCallback : FieldCallback {
263 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000264 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000265 ObjCDeclSpec &OCDS;
266 SourceLocation AtLoc;
267 tok::ObjCKeywordKind MethodImplKind;
268
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000269 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000270 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000271 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
272 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000273 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCall064d77b2009-12-03 22:31:13 +0000274 MethodImplKind(MethodImplKind) {
275 }
276
John McCall48871652010-08-21 09:40:31 +0000277 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000278 if (FD.D.getIdentifier() == 0) {
279 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
280 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000281 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000282 }
283 if (FD.BitfieldSize) {
284 P.Diag(AtLoc, diag::err_objc_property_bitfield)
285 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000286 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000287 }
288
289 // Install the property declarator into interfaceDecl.
290 IdentifierInfo *SelName =
291 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
292
293 Selector GetterSel =
294 P.PP.getSelectorTable().getNullarySelector(SelName);
295 IdentifierInfo *SetterName = OCDS.getSetterName();
296 Selector SetterSel;
297 if (SetterName)
298 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
299 else
300 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
301 P.PP.getSelectorTable(),
302 FD.D.getIdentifier());
303 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000304 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000305 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000306 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000307 &isOverridingProperty,
308 MethodImplKind);
309 if (!isOverridingProperty)
310 Props.push_back(Property);
311
312 return Property;
313 }
314};
315
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000316/// objc-interface-decl-list:
317/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000318/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000319/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000320/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000321/// objc-interface-decl-list declaration
322/// objc-interface-decl-list ';'
323///
Steve Naroff99264b42007-08-22 16:35:03 +0000324/// objc-method-requirement: [OBJC2]
325/// @required
326/// @optional
327///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000328void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
329 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000330 SmallVector<Decl *, 32> allMethods;
331 SmallVector<Decl *, 16> allProperties;
332 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000333 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenekc7c64312010-01-07 01:20:12 +0000335 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000336 Actions.ActOnObjCContainerStartDefinition(CDecl);
337
Steve Naroff99264b42007-08-22 16:35:03 +0000338 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000339 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000340 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000341 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000342 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000343 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000344 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
345 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000346 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
347 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000348 continue;
349 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000350 if (Tok.is(tok::l_paren)) {
351 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000352 ParseObjCMethodDecl(Tok.getLocation(),
353 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000354 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000355 continue;
356 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000357 // Ignore excess semicolons.
358 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000359 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000360 continue;
361 }
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chris Lattnerda9fb152008-10-20 06:10:06 +0000363 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000365 break;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Douglas Gregorf1934162010-01-13 21:24:21 +0000367 // Code completion within an Objective-C interface.
368 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000369 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000370 ObjCImpDecl? Sema::PCC_ObjCImplementation
371 : Sema::PCC_ObjCInterface);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000372 ConsumeCodeCompletionToken();
Douglas Gregorf1934162010-01-13 21:24:21 +0000373 }
374
Chris Lattner038a3e32008-10-20 05:46:22 +0000375 // If we don't have an @ directive, parse it as a function definition.
376 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000377 // The code below does not consume '}'s because it is afraid of eating the
378 // end of a namespace. Because of the way this code is structured, an
379 // erroneous r_brace would cause an infinite loop if not handled here.
380 if (Tok.is(tok::r_brace))
381 break;
John McCall084e83d2011-03-24 11:26:52 +0000382 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000383 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000384 continue;
385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattner038a3e32008-10-20 05:46:22 +0000387 // Otherwise, we have an @ directive, eat the @.
388 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000389 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000390 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000391 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000392 break;
393 }
394
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000395 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000397 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000398 AtEnd.setBegin(AtLoc);
399 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000400 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000401 } else if (DirectiveKind == tok::objc_not_keyword) {
402 Diag(Tok, diag::err_objc_unknown_at);
403 SkipUntil(tok::semi);
404 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattnerda9fb152008-10-20 06:10:06 +0000407 // Eat the identifier.
408 ConsumeToken();
409
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000410 switch (DirectiveKind) {
411 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000412 // FIXME: If someone forgets an @end on a protocol, this loop will
413 // continue to eat up tons of stuff and spew lots of nonsense errors. It
414 // would probably be better to bail out if we saw an @class or @interface
415 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000416 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000417 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 SkipUntil(tok::r_brace, tok::at);
419 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000420
421 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000422 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000423 Diag(Tok, diag::err_objc_missing_end);
424 ConsumeToken();
425 break;
426
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000427 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000428 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000429 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000430 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000431 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000432 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000433 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000434 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000435 break;
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000437 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000438 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000439 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000440
Chris Lattner038a3e32008-10-20 05:46:22 +0000441 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000442 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000443 if (Tok.is(tok::l_paren))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000444 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000445
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000446 ObjCPropertyCallback Callback(*this, allProperties,
John McCall064d77b2009-12-03 22:31:13 +0000447 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000448
Chris Lattner038a3e32008-10-20 05:46:22 +0000449 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000450 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000451 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000452
John McCall405988b2011-03-26 01:53:26 +0000453 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000454 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000455 }
Steve Naroff99264b42007-08-22 16:35:03 +0000456 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000457
458 // We break out of the big loop in two cases: when we see @end or when we see
459 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000460 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000461 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000462 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000463 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000464 ConsumeToken(); // the "end" identifier
465 else
466 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000468 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000469 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000470 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000471 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000472 allProperties.data(), allProperties.size(),
473 allTUVariables.data(), allTUVariables.size());
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000474 Actions.ActOnObjCContainerFinishDefinition(CDecl);
Steve Naroff99264b42007-08-22 16:35:03 +0000475}
476
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000477/// Parse property attribute declarations.
478///
479/// property-attr-decl: '(' property-attrlist ')'
480/// property-attrlist:
481/// property-attribute
482/// property-attrlist ',' property-attribute
483/// property-attribute:
484/// getter '=' identifier
485/// setter '=' identifier ':'
486/// readonly
487/// readwrite
488/// assign
489/// retain
490/// copy
491/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000492/// atomic
493/// strong
494/// weak
495/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000496///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000497void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000498 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000499 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000500
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000501 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000502 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000503 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000504 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000505 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000506 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner76619232008-10-20 07:22:18 +0000508 // If this is not an identifier at all, bail out early.
509 if (II == 0) {
510 MatchRHSPunctuation(tok::r_paren, LHSLoc);
511 return;
512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattner1db33542008-10-20 07:37:22 +0000514 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner68e48682008-11-20 04:42:34 +0000516 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000517 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000518 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000519 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000520 else if (II->isStr("unsafe_unretained"))
521 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000522 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000523 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000524 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000525 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000526 else if (II->isStr("strong"))
527 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000528 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000529 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000530 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000531 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000532 else if (II->isStr("atomic"))
533 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000534 else if (II->isStr("weak"))
535 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000536 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000537 bool IsSetter = II->getNameStart()[0] == 's';
538
Chris Lattner825bca12008-10-20 07:39:53 +0000539 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000540 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
541 diag::err_objc_expected_equal_for_getter;
542
543 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000544 return;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregorc8537c52009-11-19 07:41:15 +0000546 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000547 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000548 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000549 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000550 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000551 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000552 }
553
Anders Carlssonfe15a782010-10-02 17:45:21 +0000554
555 SourceLocation SelLoc;
556 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
557
558 if (!SelIdent) {
559 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
560 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000561 SkipUntil(tok::r_paren);
562 return;
563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Anders Carlssonfe15a782010-10-02 17:45:21 +0000565 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000566 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000567 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000568
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000569 if (ExpectAndConsume(tok::colon,
570 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000571 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000572 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000573 } else {
574 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000575 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000576 }
Chris Lattner825bca12008-10-20 07:39:53 +0000577 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000578 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000579 SkipUntil(tok::r_paren);
580 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000581 }
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner1db33542008-10-20 07:37:22 +0000583 if (Tok.isNot(tok::comma))
584 break;
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner1db33542008-10-20 07:37:22 +0000586 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattner1db33542008-10-20 07:37:22 +0000589 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000590}
591
Steve Naroff09bf8152007-09-06 21:24:23 +0000592/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000593/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000594/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000595///
596/// objc-instance-method: '-'
597/// objc-class-method: '+'
598///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000599/// objc-method-attributes: [OBJC2]
600/// __attribute__((deprecated))
601///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000602Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000603 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000604 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000605
Mike Stump11289f42009-09-09 15:08:12 +0000606 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000607 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000608 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000609 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000610 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000611 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000612 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000613}
614
615/// objc-selector:
616/// identifier
617/// one of
618/// enum struct union if else while do for switch case default
619/// break continue return goto asm sizeof typeof __alignof
620/// unsigned long const short volatile signed restrict _Complex
621/// in out inout bycopy byref oneway int char float double void _Bool
622///
Chris Lattner4f472a32009-04-11 18:13:45 +0000623IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000624
Chris Lattner5700fab2007-10-07 02:00:24 +0000625 switch (Tok.getKind()) {
626 default:
627 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000628 case tok::ampamp:
629 case tok::ampequal:
630 case tok::amp:
631 case tok::pipe:
632 case tok::tilde:
633 case tok::exclaim:
634 case tok::exclaimequal:
635 case tok::pipepipe:
636 case tok::pipeequal:
637 case tok::caret:
638 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000639 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000640 if (isalpha(ThisTok[0])) {
641 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
642 Tok.setKind(tok::identifier);
643 SelectorLoc = ConsumeToken();
644 return II;
645 }
646 return 0;
647 }
648
Chris Lattner5700fab2007-10-07 02:00:24 +0000649 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000650 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000651 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000652 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000653 case tok::kw_break:
654 case tok::kw_case:
655 case tok::kw_catch:
656 case tok::kw_char:
657 case tok::kw_class:
658 case tok::kw_const:
659 case tok::kw_const_cast:
660 case tok::kw_continue:
661 case tok::kw_default:
662 case tok::kw_delete:
663 case tok::kw_do:
664 case tok::kw_double:
665 case tok::kw_dynamic_cast:
666 case tok::kw_else:
667 case tok::kw_enum:
668 case tok::kw_explicit:
669 case tok::kw_export:
670 case tok::kw_extern:
671 case tok::kw_false:
672 case tok::kw_float:
673 case tok::kw_for:
674 case tok::kw_friend:
675 case tok::kw_goto:
676 case tok::kw_if:
677 case tok::kw_inline:
678 case tok::kw_int:
679 case tok::kw_long:
680 case tok::kw_mutable:
681 case tok::kw_namespace:
682 case tok::kw_new:
683 case tok::kw_operator:
684 case tok::kw_private:
685 case tok::kw_protected:
686 case tok::kw_public:
687 case tok::kw_register:
688 case tok::kw_reinterpret_cast:
689 case tok::kw_restrict:
690 case tok::kw_return:
691 case tok::kw_short:
692 case tok::kw_signed:
693 case tok::kw_sizeof:
694 case tok::kw_static:
695 case tok::kw_static_cast:
696 case tok::kw_struct:
697 case tok::kw_switch:
698 case tok::kw_template:
699 case tok::kw_this:
700 case tok::kw_throw:
701 case tok::kw_true:
702 case tok::kw_try:
703 case tok::kw_typedef:
704 case tok::kw_typeid:
705 case tok::kw_typename:
706 case tok::kw_typeof:
707 case tok::kw_union:
708 case tok::kw_unsigned:
709 case tok::kw_using:
710 case tok::kw_virtual:
711 case tok::kw_void:
712 case tok::kw_volatile:
713 case tok::kw_wchar_t:
714 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000715 case tok::kw__Bool:
716 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000717 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000718 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000719 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000720 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000721 }
Steve Naroff99264b42007-08-22 16:35:03 +0000722}
723
Fariborz Jahanian83615522008-01-02 22:54:34 +0000724/// objc-for-collection-in: 'in'
725///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000726bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000727 // FIXME: May have to do additional look-ahead to only allow for
728 // valid tokens following an 'in'; such as an identifier, unary operators,
729 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000730 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000731 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000732}
733
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000734/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000735/// qualifier list and builds their bitmask representation in the input
736/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000737///
738/// objc-type-qualifiers:
739/// objc-type-qualifier
740/// objc-type-qualifiers objc-type-qualifier
741///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000742void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
743 ObjCTypeNameContext Context) {
Chris Lattner61511e12007-12-12 06:56:32 +0000744 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000745 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000746 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
747 Context == OTN_ParameterType);
Douglas Gregor99fa2642010-08-24 01:06:58 +0000748 ConsumeCodeCompletionToken();
749 }
750
Chris Lattner5e530bc2007-12-27 19:57:00 +0000751 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000752 return;
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner61511e12007-12-12 06:56:32 +0000754 const IdentifierInfo *II = Tok.getIdentifierInfo();
755 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000756 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000757 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000758
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000759 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000760 switch (i) {
761 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000762 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
763 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
764 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
765 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
766 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
767 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000768 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000769 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000770 ConsumeToken();
771 II = 0;
772 break;
773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner61511e12007-12-12 06:56:32 +0000775 // If this wasn't a recognized qualifier, bail out.
776 if (II) return;
777 }
778}
779
780/// objc-type-name:
781/// '(' objc-type-qualifiers[opt] type-name ')'
782/// '(' objc-type-qualifiers[opt] ')'
783///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000784ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
785 ObjCTypeNameContext Context) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000786 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattnerb7954432008-10-22 03:52:06 +0000788 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000789 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000790 ObjCDeclContextSwitch ObjCDC(*this);
791
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000792 // Parse type qualifiers, in, inout, etc.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000793 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000794
John McCallba7bf592010-08-24 05:47:05 +0000795 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000796 if (isTypeSpecifierQualifier()) {
John McCall31168b02011-06-15 23:02:42 +0000797 TypeResult TypeSpec =
798 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor220cac52009-02-18 17:45:20 +0000799 if (!TypeSpec.isInvalid())
800 Ty = TypeSpec.get();
801 }
John McCall31168b02011-06-15 23:02:42 +0000802
Steve Naroff90255b42008-10-21 14:15:04 +0000803 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000804 ConsumeParen();
805 else if (Tok.getLocation() == TypeStartLoc) {
806 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000807 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000808 SkipUntil(tok::r_paren);
809 } else {
810 // Otherwise, we found *something*, but didn't get a ')' in the right
811 // place. Emit an error then return what we have as the type.
812 MatchRHSPunctuation(tok::r_paren, LParenLoc);
813 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000814 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000815}
816
817/// objc-method-decl:
818/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000819/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000820/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000821/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000822///
823/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000824/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000825/// objc-keyword-selector objc-keyword-decl
826///
827/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000828/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
829/// objc-selector ':' objc-keyword-attributes[opt] identifier
830/// ':' objc-type-name objc-keyword-attributes[opt] identifier
831/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000832///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000833/// objc-parmlist:
834/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000835///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000836/// objc-parms:
837/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000838///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000839/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000840/// , ...
841///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000842/// objc-keyword-attributes: [OBJC2]
843/// __attribute__((unused))
844///
John McCall48871652010-08-21 09:40:31 +0000845Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000846 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000847 tok::ObjCKeywordKind MethodImplKind,
848 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000849 ParsingDeclRAIIObject PD(*this);
850
Douglas Gregor636a61e2010-04-07 00:21:17 +0000851 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000852 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000853 /*ReturnType=*/ ParsedType());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000854 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000855 }
856
Chris Lattner2ebb1782008-08-23 01:48:03 +0000857 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000858 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000859 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000860 if (Tok.is(tok::l_paren))
Douglas Gregor95d3e372011-03-08 19:17:54 +0000861 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump11289f42009-09-09 15:08:12 +0000862
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000863 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000864 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000865 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000866 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000867
Douglas Gregor636a61e2010-04-07 00:21:17 +0000868 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000869 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000870 ReturnType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000871 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000872 }
873
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000874 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000875 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000876 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000877
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000878 // An unnamed colon is valid.
879 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000880 Diag(Tok, diag::err_expected_selector_for_method)
881 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000882 // Skip until we get a ; or {}.
883 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000884 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000887 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000888 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000889 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000890 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000891 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +0000892
Chris Lattner5700fab2007-10-07 02:00:24 +0000893 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000894 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000895 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000896 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +0000897 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000898 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +0000899 methodAttrs.getList(), MethodImplKind,
900 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +0000901 PD.complete(Result);
902 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000903 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000904
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 SmallVector<IdentifierInfo *, 12> KeyIdents;
906 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000907 ParseScope PrototypeScope(this,
908 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +0000909
910 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000911
Chris Lattner5700fab2007-10-07 02:00:24 +0000912 while (1) {
John McCall084e83d2011-03-24 11:26:52 +0000913 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +0000914 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner5700fab2007-10-07 02:00:24 +0000916 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000917 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000918 Diag(Tok, diag::err_expected_colon);
919 break;
920 }
921 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000922
John McCallba7bf592010-08-24 05:47:05 +0000923 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000924 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000925 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000926
Chris Lattner5700fab2007-10-07 02:00:24 +0000927 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000928 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +0000929 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +0000930 MaybeParseGNUAttributes(paramAttrs);
931 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +0000932 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000933
Douglas Gregor45879692010-07-08 23:37:41 +0000934 // Code completion for the next piece of the selector.
935 if (Tok.is(tok::code_completion)) {
936 ConsumeCodeCompletionToken();
937 KeyIdents.push_back(SelIdent);
938 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
939 mType == tok::minus,
940 /*AtParameterName=*/true,
941 ReturnType,
942 KeyIdents.data(),
943 KeyIdents.size());
944 KeyIdents.pop_back();
945 break;
946 }
947
Chris Lattner0ef13522007-10-09 17:51:17 +0000948 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000949 Diag(Tok, diag::err_expected_ident); // missing argument name.
950 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000951 }
Mike Stump11289f42009-09-09 15:08:12 +0000952
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000953 ArgInfo.Name = Tok.getIdentifierInfo();
954 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000955 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000956
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000957 ArgInfos.push_back(ArgInfo);
958 KeyIdents.push_back(SelIdent);
959
John McCall084e83d2011-03-24 11:26:52 +0000960 // Make sure the attributes persist.
961 allParamAttrs.takeAllFrom(paramAttrs.getPool());
962
Douglas Gregor95887f92010-07-08 23:20:03 +0000963 // Code completion for the next piece of the selector.
964 if (Tok.is(tok::code_completion)) {
965 ConsumeCodeCompletionToken();
966 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
967 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000968 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000969 ReturnType,
970 KeyIdents.data(),
971 KeyIdents.size());
972 break;
973 }
974
Chris Lattner5700fab2007-10-07 02:00:24 +0000975 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000976 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000977 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000978 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000979 break;
980 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000981 }
Mike Stump11289f42009-09-09 15:08:12 +0000982
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000983 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner5700fab2007-10-07 02:00:24 +0000985 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000986 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000987 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000988 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000989 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000990 ConsumeToken();
991 break;
992 }
John McCall084e83d2011-03-24 11:26:52 +0000993 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +0000994 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000995 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000996 Declarator ParmDecl(DS, Declarator::PrototypeContext);
997 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000998 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +0000999 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001000 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1001 ParmDecl.getIdentifierLoc(),
1002 Param,
1003 0));
1004
Chris Lattner5700fab2007-10-07 02:00:24 +00001005 }
Mike Stump11289f42009-09-09 15:08:12 +00001006
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001007 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001008 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001009 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001010 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001011
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001012 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001013 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001014
Chris Lattner5700fab2007-10-07 02:00:24 +00001015 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1016 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001017 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001018 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001019 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001020 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001021 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001022 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001023 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001024
John McCall28a6aea2009-11-04 02:18:39 +00001025 PD.complete(Result);
1026 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001027}
1028
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001029/// objc-protocol-refs:
1030/// '<' identifier-list '>'
1031///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001032bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001033ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1034 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001035 bool WarnOnDeclarations,
1036 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001037 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001038
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001039 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001040
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001041 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001042
Chris Lattner3bbae002008-07-26 04:03:38 +00001043 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001044 if (Tok.is(tok::code_completion)) {
1045 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1046 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001047 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +00001048 }
1049
Chris Lattner3bbae002008-07-26 04:03:38 +00001050 if (Tok.isNot(tok::identifier)) {
1051 Diag(Tok, diag::err_expected_ident);
1052 SkipUntil(tok::greater);
1053 return true;
1054 }
1055 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1056 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001057 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001058 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001059
Chris Lattner3bbae002008-07-26 04:03:38 +00001060 if (Tok.isNot(tok::comma))
1061 break;
1062 ConsumeToken();
1063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattner3bbae002008-07-26 04:03:38 +00001065 // Consume the '>'.
1066 if (Tok.isNot(tok::greater)) {
1067 Diag(Tok, diag::err_expected_greater);
1068 return true;
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattner3bbae002008-07-26 04:03:38 +00001071 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattner3bbae002008-07-26 04:03:38 +00001073 // Convert the list of protocols identifiers into a list of protocol decls.
1074 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1075 &ProtocolIdents[0], ProtocolIdents.size(),
1076 Protocols);
1077 return false;
1078}
1079
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001080/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1081/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001082bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001083 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1084 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1085 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001086 SmallVector<Decl *, 8> ProtocolDecl;
1087 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001088 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1089 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001090 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1091 ProtocolLocs.data(), LAngleLoc);
1092 if (EndProtoLoc.isValid())
1093 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001094 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001095}
1096
1097
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001098/// objc-class-instance-variables:
1099/// '{' objc-instance-variable-decl-list[opt] '}'
1100///
1101/// objc-instance-variable-decl-list:
1102/// objc-visibility-spec
1103/// objc-instance-variable-decl ';'
1104/// ';'
1105/// objc-instance-variable-decl-list objc-visibility-spec
1106/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1107/// objc-instance-variable-decl-list ';'
1108///
1109/// objc-visibility-spec:
1110/// @private
1111/// @protected
1112/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001113/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001114///
1115/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001116/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001117///
John McCall48871652010-08-21 09:40:31 +00001118void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001119 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001120 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001121 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001122 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001123
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001124 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001125
Steve Naroff00433d32007-08-21 21:17:12 +00001126 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001127
Steve Naroff00433d32007-08-21 21:17:12 +00001128 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001129 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001130 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001131
Steve Naroff00433d32007-08-21 21:17:12 +00001132 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001133 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001134 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001135 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001136 ConsumeToken();
1137 continue;
1138 }
Mike Stump11289f42009-09-09 15:08:12 +00001139
Steve Naroff00433d32007-08-21 21:17:12 +00001140 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001141 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001142 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001143
1144 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001145 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001146 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001147 }
1148
Steve Naroff7c348172007-08-23 18:16:40 +00001149 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001150 case tok::objc_private:
1151 case tok::objc_public:
1152 case tok::objc_protected:
1153 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001154 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001155 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001156 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001157 default:
1158 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001159 continue;
1160 }
1161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Douglas Gregor48d46252010-01-13 21:54:15 +00001163 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001164 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001165 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001166 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001167 }
1168
John McCallcfefb6d2009-11-03 02:38:08 +00001169 struct ObjCIvarCallback : FieldCallback {
1170 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001171 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001172 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001174
John McCall48871652010-08-21 09:40:31 +00001175 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001176 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001177 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1178 }
1179
John McCall48871652010-08-21 09:40:31 +00001180 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001181 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001182 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001183 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001184 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001185 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001186 FD.D, FD.BitfieldSize, visibility);
1187 P.Actions.ActOnObjCContainerFinishDefinition(IDecl);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001188 if (Field)
1189 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001190 return Field;
1191 }
1192 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001193
Chris Lattnera12405b2008-04-10 06:46:29 +00001194 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001195 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001196 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner0ef13522007-10-09 17:51:17 +00001198 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001199 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001200 } else {
1201 Diag(Tok, diag::err_expected_semi_decl_list);
1202 // Skip to end of block or statement
1203 SkipUntil(tok::r_brace, true, true);
1204 }
1205 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001206 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001207 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1208 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
1209 Actions.ActOnObjCContainerFinishDefinition(interfaceDecl);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001210 // Call ActOnFields() even if we don't have any decls. This is useful
1211 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001212 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001213 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001214 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001215 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001216}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001217
1218/// objc-protocol-declaration:
1219/// objc-protocol-definition
1220/// objc-protocol-forward-reference
1221///
1222/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001223/// @protocol identifier
1224/// objc-protocol-refs[opt]
1225/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001226/// @end
1227///
1228/// objc-protocol-forward-reference:
1229/// @protocol identifier-list ';'
1230///
1231/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001232/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001233/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001234Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001235 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001236 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001237 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1238 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001240 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001241 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001242 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001243 }
1244
Chris Lattner0ef13522007-10-09 17:51:17 +00001245 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001246 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001247 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001248 }
1249 // Save the protocol name, then consume it.
1250 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1251 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001252
Chris Lattner0ef13522007-10-09 17:51:17 +00001253 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001254 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001255 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001256 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001257 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Chris Lattner0ef13522007-10-09 17:51:17 +00001260 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001261 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001262 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1263
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001264 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001265 while (1) {
1266 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001267 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001268 Diag(Tok, diag::err_expected_ident);
1269 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001270 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001271 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001272 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1273 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001274 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001275
Chris Lattner0ef13522007-10-09 17:51:17 +00001276 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001277 break;
1278 }
1279 // Consume the ';'.
1280 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001281 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001282
Steve Naroff93eb5f12007-10-10 17:32:04 +00001283 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001284 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001285 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001286 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001289 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001290 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001291
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001292 SmallVector<Decl *, 8> ProtocolRefs;
1293 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001294 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001295 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1296 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001297 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001298
John McCall48871652010-08-21 09:40:31 +00001299 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001300 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001301 ProtocolRefs.data(),
1302 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001303 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001304 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001305
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001306 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001307 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001308}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001309
1310/// objc-implementation:
1311/// objc-class-implementation-prologue
1312/// objc-category-implementation-prologue
1313///
1314/// objc-class-implementation-prologue:
1315/// @implementation identifier objc-superclass[opt]
1316/// objc-class-instance-variables[opt]
1317///
1318/// objc-category-implementation-prologue:
1319/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001320Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001321 SourceLocation atLoc) {
1322 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1323 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1324 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001325
Douglas Gregor49c22a72009-11-18 16:26:39 +00001326 // Code completion after '@implementation'.
1327 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001328 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001329 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001330 }
1331
Chris Lattner0ef13522007-10-09 17:51:17 +00001332 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001333 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001334 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001335 }
1336 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001337 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001338 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001339
1340 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001341 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001342 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001343 SourceLocation categoryLoc, rparenLoc;
1344 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001345
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001346 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001347 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001348 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001349 }
1350
Chris Lattner0ef13522007-10-09 17:51:17 +00001351 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001352 categoryId = Tok.getIdentifierInfo();
1353 categoryLoc = ConsumeToken();
1354 } else {
1355 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001356 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001357 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001358 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001359 Diag(Tok, diag::err_expected_rparen);
1360 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001361 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001362 }
1363 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001364 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001365 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001366 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001367
1368 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001369 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001370 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001371 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001372 }
1373 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001374 SourceLocation superClassLoc;
1375 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001376 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001377 // We have a super class
1378 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001379 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001380 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001381 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001382 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001383 superClassId = Tok.getIdentifierInfo();
1384 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001385 }
John McCall48871652010-08-21 09:40:31 +00001386 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001387 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001388 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001389
Steve Naroff33a1e802007-10-29 21:38:07 +00001390 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001391 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1392
1393 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001394 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001395 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001396 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001397}
Steve Naroff33a1e802007-10-29 21:38:07 +00001398
John McCall48871652010-08-21 09:40:31 +00001399Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001400 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1401 "ParseObjCAtEndDeclaration(): Expected @end");
John McCall48871652010-08-21 09:40:31 +00001402 Decl *Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001403 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001404 if (ObjCImpDecl) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001405 Actions.ActOnAtEnd(getCurScope(), atEnd);
1406 Actions.ActOnObjCContainerFinishDefinition(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001407 ObjCImpDecl = 0;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001408 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001409 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001410 else {
1411 // missing @implementation
Fariborz Jahanianc0577942011-04-22 22:02:28 +00001412 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001413 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001414 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001415}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001416
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001417Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1418 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001419 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001420 return Actions.ConvertDeclToDeclGroup(0);
1421 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001422 Actions.ActOnAtEnd(getCurScope(), SourceRange());
1423 Actions.ActOnObjCContainerFinishDefinition(ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001424 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1425}
1426
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001427/// compatibility-alias-decl:
1428/// @compatibility_alias alias-name class-name ';'
1429///
John McCall48871652010-08-21 09:40:31 +00001430Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001431 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1432 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1433 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001434 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001435 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001436 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001437 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001438 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1439 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001440 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001441 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001442 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001443 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001444 IdentifierInfo *classId = Tok.getIdentifierInfo();
1445 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001446 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1447 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001448 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1449 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001450}
1451
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001452/// property-synthesis:
1453/// @synthesize property-ivar-list ';'
1454///
1455/// property-ivar-list:
1456/// property-ivar
1457/// property-ivar-list ',' property-ivar
1458///
1459/// property-ivar:
1460/// identifier
1461/// identifier '=' identifier
1462///
John McCall48871652010-08-21 09:40:31 +00001463Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001464 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1465 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001466 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001467
Douglas Gregor88e72a02009-11-18 19:45:45 +00001468 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001469 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001470 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001471 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001472 }
1473
Douglas Gregor88e72a02009-11-18 19:45:45 +00001474 if (Tok.isNot(tok::identifier)) {
1475 Diag(Tok, diag::err_synthesized_property_name);
1476 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001477 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001478 }
1479
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001480 IdentifierInfo *propertyIvar = 0;
1481 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1482 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001483 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001484 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001485 // property '=' ivar-name
1486 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001487
1488 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001489 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001490 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001491 }
1492
Chris Lattner0ef13522007-10-09 17:51:17 +00001493 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001494 Diag(Tok, diag::err_expected_ident);
1495 break;
1496 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001497 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001498 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001499 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001500 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001501 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001502 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001503 break;
1504 ConsumeToken(); // consume ','
1505 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001506 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001507 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001508}
1509
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001510/// property-dynamic:
1511/// @dynamic property-list
1512///
1513/// property-list:
1514/// identifier
1515/// property-list ',' identifier
1516///
John McCall48871652010-08-21 09:40:31 +00001517Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001518 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1519 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001520 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001521 while (true) {
1522 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001523 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001524 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001525 }
1526
1527 if (Tok.isNot(tok::identifier)) {
1528 Diag(Tok, diag::err_expected_ident);
1529 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001530 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001531 }
1532
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001533 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1534 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001535 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001536 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001537
Chris Lattner0ef13522007-10-09 17:51:17 +00001538 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001539 break;
1540 ConsumeToken(); // consume ','
1541 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001542 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001543 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001544}
Mike Stump11289f42009-09-09 15:08:12 +00001545
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001546/// objc-throw-statement:
1547/// throw expression[opt];
1548///
John McCalldadc5752010-08-24 06:29:42 +00001549StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1550 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001551 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001552 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001553 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001554 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001555 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001556 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001557 }
1558 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001559 // consume ';'
1560 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001561 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001562}
1563
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001564/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001565/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001566///
John McCalldadc5752010-08-24 06:29:42 +00001567StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001568Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001569 ConsumeToken(); // consume synchronized
1570 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001571 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001572 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001573 }
John McCalld9bb7432011-07-27 21:50:02 +00001574
1575 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001576 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001577 ExprResult operand(ParseExpression());
1578
1579 if (Tok.is(tok::r_paren)) {
1580 ConsumeParen(); // ')'
1581 } else {
1582 if (!operand.isInvalid())
1583 Diag(Tok, diag::err_expected_rparen);
1584
1585 // Skip forward until we see a left brace, but don't consume it.
1586 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001587 }
John McCalld9bb7432011-07-27 21:50:02 +00001588
1589 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001590 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001591 if (!operand.isInvalid())
1592 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001593 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001594 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001595
John McCalld9bb7432011-07-27 21:50:02 +00001596 // Check the @synchronized operand now.
1597 if (!operand.isInvalid())
1598 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001599
John McCalld9bb7432011-07-27 21:50:02 +00001600 // Parse the compound statement within a new scope.
1601 ParseScope bodyScope(this, Scope::DeclScope);
1602 StmtResult body(ParseCompoundStatementBody());
1603 bodyScope.Exit();
1604
1605 // If there was a semantic or parse error earlier with the
1606 // operand, fail now.
1607 if (operand.isInvalid())
1608 return StmtError();
1609
1610 if (body.isInvalid())
1611 body = Actions.ActOnNullStmt(Tok.getLocation());
1612
1613 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001614}
1615
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001616/// objc-try-catch-statement:
1617/// @try compound-statement objc-catch-list[opt]
1618/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1619///
1620/// objc-catch-list:
1621/// @catch ( parameter-declaration ) compound-statement
1622/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1623/// catch-parameter-declaration:
1624/// parameter-declaration
1625/// '...' [OBJC2]
1626///
John McCalldadc5752010-08-24 06:29:42 +00001627StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001628 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001629
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001630 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001631 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001632 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001633 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001634 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001635 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001636 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001637 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001638 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001639 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001640 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001641 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001642
Chris Lattner0ef13522007-10-09 17:51:17 +00001643 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001644 // At this point, we need to lookahead to determine if this @ is the start
1645 // of an @catch or @finally. We don't want to consume the @ token if this
1646 // is an @try or @encode or something else.
1647 Token AfterAt = GetLookAheadToken(1);
1648 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1649 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1650 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001651
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001652 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001653 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001654 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001655 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001656 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001657 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001658 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001659 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001660 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001661 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001662 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001663 ParseDeclarator(ParmDecl);
1664
Douglas Gregore11ee112010-04-23 23:01:43 +00001665 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001666 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001667 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001668 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001669 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001670
Steve Naroff65a00892009-04-07 22:56:58 +00001671 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001672
Steve Naroff65a00892009-04-07 22:56:58 +00001673 if (Tok.is(tok::r_paren))
1674 RParenLoc = ConsumeParen();
1675 else // Skip over garbage, until we get to ')'. Eat the ')'.
1676 SkipUntil(tok::r_paren, true, false);
1677
John McCalldadc5752010-08-24 06:29:42 +00001678 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001679 if (Tok.is(tok::l_brace))
1680 CatchBody = ParseCompoundStatementBody();
1681 else
1682 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001683 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001684 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001685
John McCalldadc5752010-08-24 06:29:42 +00001686 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001687 RParenLoc,
1688 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001689 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001690 if (!Catch.isInvalid())
1691 CatchStmts.push_back(Catch.release());
1692
Steve Naroffe6016792008-02-05 21:27:35 +00001693 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001694 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1695 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001696 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001697 }
1698 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001699 } else {
1700 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001701 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001702 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001703
John McCalldadc5752010-08-24 06:29:42 +00001704 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001705 if (Tok.is(tok::l_brace))
1706 FinallyBody = ParseCompoundStatementBody();
1707 else
1708 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001709 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001710 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001711 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001712 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001713 catch_or_finally_seen = true;
1714 break;
1715 }
1716 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001717 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001718 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001719 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001720 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001721
John McCallb268a282010-08-23 23:25:46 +00001722 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001723 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001724 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001725}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001726
John McCall31168b02011-06-15 23:02:42 +00001727/// objc-autoreleasepool-statement:
1728/// @autoreleasepool compound-statement
1729///
1730StmtResult
1731Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1732 ConsumeToken(); // consume autoreleasepool
1733 if (Tok.isNot(tok::l_brace)) {
1734 Diag(Tok, diag::err_expected_lbrace);
1735 return StmtError();
1736 }
1737 // Enter a scope to hold everything within the compound stmt. Compound
1738 // statements can always hold declarations.
1739 ParseScope BodyScope(this, Scope::DeclScope);
1740
1741 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1742
1743 BodyScope.Exit();
1744 if (AutoreleasePoolBody.isInvalid())
1745 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1746 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1747 AutoreleasePoolBody.take());
1748}
1749
Steve Naroff09bf8152007-09-06 21:24:23 +00001750/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001751///
John McCall48871652010-08-21 09:40:31 +00001752Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001753 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001754
John McCallfaf5fb42010-08-26 23:41:50 +00001755 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1756 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001757
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001758 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001759 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001760 if (ObjCImpDecl) {
1761 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001762 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001763 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001764 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001765 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001766
Steve Naroffbb875722007-11-11 19:54:21 +00001767 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001768 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001769 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001770
Steve Naroffbb875722007-11-11 19:54:21 +00001771 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1772 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001773
Steve Naroffbb875722007-11-11 19:54:21 +00001774 // If we didn't find the '{', bail out.
1775 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001776 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001777 }
Steve Naroffbb875722007-11-11 19:54:21 +00001778 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001779
Steve Naroffbb875722007-11-11 19:54:21 +00001780 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001781 ParseScope BodyScope(this,
1782 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001783
Steve Naroffbb875722007-11-11 19:54:21 +00001784 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001785 // specified Declarator for the method.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001786 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001787
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001788 if (PP.isCodeCompletionEnabled()) {
1789 if (trySkippingFunctionBodyForCodeCompletion()) {
1790 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001791 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001792 }
1793 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001794
John McCalldadc5752010-08-24 06:29:42 +00001795 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001796
Steve Naroffbb875722007-11-11 19:54:21 +00001797 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001798 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001799 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1800 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001801
Steve Naroffbb875722007-11-11 19:54:21 +00001802 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001803 BodyScope.Exit();
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001804
1805 // TODO: Pass argument information.
1806 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff7b8fa472007-11-13 23:01:27 +00001807 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001808}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001809
John McCalldadc5752010-08-24 06:29:42 +00001810StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001811 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001812 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001813 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001814 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001815 }
1816
1817 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001818 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001819
1820 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001821 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001822
1823 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001824 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001825
1826 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1827 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001828
John McCalldadc5752010-08-24 06:29:42 +00001829 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001830 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001831 // If the expression is invalid, skip ahead to the next semicolon. Not
1832 // doing this opens us up to the possibility of infinite loops if
1833 // ParseExpression does not consume any tokens.
1834 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001835 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001836 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001837
Steve Naroffe6016792008-02-05 21:27:35 +00001838 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001839 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001840 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001841}
1842
John McCalldadc5752010-08-24 06:29:42 +00001843ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001844 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001845 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001846 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001847 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001848 return ExprError();
1849
Chris Lattnere002fbe2007-12-12 01:04:12 +00001850 case tok::string_literal: // primary-expression: string-literal
1851 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001852 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001853 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001854 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001855 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001856
Chris Lattner197a3012008-08-05 06:19:09 +00001857 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1858 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001859 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001860 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001861 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001862 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001863 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001864 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001866 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001867 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001868}
1869
Douglas Gregor8d4de672010-04-21 22:36:40 +00001870/// \brirg Parse the receiver of an Objective-C++ message send.
1871///
1872/// This routine parses the receiver of a message send in
1873/// Objective-C++ either as a type or as an expression. Note that this
1874/// routine must not be called to parse a send to 'super', since it
1875/// has no way to return such a result.
1876///
1877/// \param IsExpr Whether the receiver was parsed as an expression.
1878///
1879/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1880/// IsExpr is true), the parsed expression. If the receiver was parsed
1881/// as a type (\c IsExpr is false), the parsed type.
1882///
1883/// \returns True if an error occurred during parsing or semantic
1884/// analysis, in which case the arguments do not have valid
1885/// values. Otherwise, returns false for a successful parse.
1886///
1887/// objc-receiver: [C++]
1888/// 'super' [not parsed here]
1889/// expression
1890/// simple-type-specifier
1891/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001892bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001893 InMessageExpressionRAIIObject InMessage(*this, true);
1894
Douglas Gregor8d4de672010-04-21 22:36:40 +00001895 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1896 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1897 TryAnnotateTypeOrScopeToken();
1898
1899 if (!isCXXSimpleTypeSpecifier()) {
1900 // objc-receiver:
1901 // expression
John McCalldadc5752010-08-24 06:29:42 +00001902 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001903 if (Receiver.isInvalid())
1904 return true;
1905
1906 IsExpr = true;
1907 TypeOrExpr = Receiver.take();
1908 return false;
1909 }
1910
1911 // objc-receiver:
1912 // typename-specifier
1913 // simple-type-specifier
1914 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00001915 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001916 ParseCXXSimpleTypeSpecifier(DS);
1917
1918 if (Tok.is(tok::l_paren)) {
1919 // If we see an opening parentheses at this point, we are
1920 // actually parsing an expression that starts with a
1921 // function-style cast, e.g.,
1922 //
1923 // postfix-expression:
1924 // simple-type-specifier ( expression-list [opt] )
1925 // typename-specifier ( expression-list [opt] )
1926 //
1927 // Parse the remainder of this case, then the (optional)
1928 // postfix-expression suffix, followed by the (optional)
1929 // right-hand side of the binary expression. We have an
1930 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001931 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001932 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001933 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001934 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001935 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001936 if (Receiver.isInvalid())
1937 return true;
1938
1939 IsExpr = true;
1940 TypeOrExpr = Receiver.take();
1941 return false;
1942 }
1943
1944 // We have a class message. Turn the simple-type-specifier or
1945 // typename-specifier we parsed into a type and parse the
1946 // remainder of the class message.
1947 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001948 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001949 if (Type.isInvalid())
1950 return true;
1951
1952 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001953 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001954 return false;
1955}
1956
Douglas Gregor990ccac2010-05-31 14:40:22 +00001957/// \brief Determine whether the parser is currently referring to a an
1958/// Objective-C message send, using a simplified heuristic to avoid overhead.
1959///
1960/// This routine will only return true for a subset of valid message-send
1961/// expressions.
1962bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001963 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001964 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001965 return GetLookAheadToken(1).is(tok::identifier) &&
1966 GetLookAheadToken(2).is(tok::identifier);
1967}
1968
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001969bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1970 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1971 InMessageExpression)
1972 return false;
1973
1974
1975 ParsedType Type;
1976
1977 if (Tok.is(tok::annot_typename))
1978 Type = getTypeAnnotation(Tok);
1979 else if (Tok.is(tok::identifier))
1980 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1981 getCurScope());
1982 else
1983 return false;
1984
1985 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1986 const Token &AfterNext = GetLookAheadToken(2);
1987 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1988 if (Tok.is(tok::identifier))
1989 TryAnnotateTypeOrScopeToken();
1990
1991 return Tok.is(tok::annot_typename);
1992 }
1993 }
1994
1995 return false;
1996}
1997
Mike Stump11289f42009-09-09 15:08:12 +00001998/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001999/// '[' objc-receiver objc-message-args ']'
2000///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002001/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002002/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002003/// expression
2004/// class-name
2005/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002006///
John McCalldadc5752010-08-24 06:29:42 +00002007ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002008 assert(Tok.is(tok::l_square) && "'[' expected");
2009 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2010
Douglas Gregora817a192010-05-27 23:06:34 +00002011 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002012 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregora817a192010-05-27 23:06:34 +00002013 ConsumeCodeCompletionToken();
2014 SkipUntil(tok::r_square);
2015 return ExprError();
2016 }
2017
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002018 InMessageExpressionRAIIObject InMessage(*this, true);
2019
Douglas Gregor8d4de672010-04-21 22:36:40 +00002020 if (getLang().CPlusPlus) {
2021 // We completely separate the C and C++ cases because C++ requires
2022 // more complicated (read: slower) parsing.
2023
2024 // Handle send to super.
2025 // FIXME: This doesn't benefit from the same typo-correction we
2026 // get in Objective-C.
2027 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002028 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002029 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2030 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002031
2032 // Parse the receiver, which is either a type or an expression.
2033 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002034 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002035 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2036 SkipUntil(tok::r_square);
2037 return ExprError();
2038 }
2039
2040 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002041 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2042 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002043 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002044
2045 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002046 ParsedType::getFromOpaquePtr(TypeOrExpr),
2047 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002048 }
2049
2050 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002051 IdentifierInfo *Name = Tok.getIdentifierInfo();
2052 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002053 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002054 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002055 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002056 NextToken().is(tok::period),
2057 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002058 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002059 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2060 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002061
John McCallfaf5fb42010-08-26 23:41:50 +00002062 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002063 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002064 SkipUntil(tok::r_square);
2065 return ExprError();
2066 }
2067
Douglas Gregore5798dc2010-04-21 20:38:13 +00002068 ConsumeToken(); // the type name
2069
2070 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002071 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002072
John McCallfaf5fb42010-08-26 23:41:50 +00002073 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002074 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002075 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002076 }
Chris Lattner8f697062008-01-25 18:59:06 +00002077 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002078
2079 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002080 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002081 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002082 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002083 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002084 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002085
John McCallba7bf592010-08-24 05:47:05 +00002086 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2087 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002088}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002089
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002090/// \brief Parse the remainder of an Objective-C message following the
2091/// '[' objc-receiver.
2092///
2093/// This routine handles sends to super, class messages (sent to a
2094/// class name), and instance messages (sent to an object), and the
2095/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2096/// ReceiverExpr, respectively. Only one of these parameters may have
2097/// a valid value.
2098///
2099/// \param LBracLoc The location of the opening '['.
2100///
2101/// \param SuperLoc If this is a send to 'super', the location of the
2102/// 'super' keyword that indicates a send to the superclass.
2103///
2104/// \param ReceiverType If this is a class message, the type of the
2105/// class we are sending a message to.
2106///
2107/// \param ReceiverExpr If this is an instance message, the expression
2108/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002109///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002110/// objc-message-args:
2111/// objc-selector
2112/// objc-keywordarg-list
2113///
2114/// objc-keywordarg-list:
2115/// objc-keywordarg
2116/// objc-keywordarg-list objc-keywordarg
2117///
Mike Stump11289f42009-09-09 15:08:12 +00002118/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002119/// selector-name[opt] ':' objc-keywordexpr
2120///
2121/// objc-keywordexpr:
2122/// nonempty-expr-list
2123///
2124/// nonempty-expr-list:
2125/// assignment-expression
2126/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002127///
John McCalldadc5752010-08-24 06:29:42 +00002128ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002129Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002130 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002131 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002132 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002133 InMessageExpressionRAIIObject InMessage(*this, true);
2134
Steve Naroffeae65032009-11-07 02:08:14 +00002135 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002136 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002137 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2138 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002139 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002140 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2141 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002142 else
John McCallb268a282010-08-23 23:25:46 +00002143 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002144 0, 0, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002145 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00002146 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002147
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002148 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002149 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002150 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002151
Anders Carlsson978f08d2009-02-14 18:21:46 +00002152 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002153
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002154 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002155 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002156
Chris Lattner0ef13522007-10-09 17:51:17 +00002157 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002158 while (1) {
2159 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002160 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002161
Chris Lattner0ef13522007-10-09 17:51:17 +00002162 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002163 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002164 // We must manually skip to a ']', otherwise the expression skipper will
2165 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2166 // the enclosing expression.
2167 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002168 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002169 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002170
Steve Narofff73590d2007-09-27 14:38:14 +00002171 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002172 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002173
2174 if (Tok.is(tok::code_completion)) {
2175 if (SuperLoc.isValid())
2176 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2177 KeyIdents.data(),
2178 KeyIdents.size(),
2179 /*AtArgumentEpression=*/true);
2180 else if (ReceiverType)
2181 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2182 KeyIdents.data(),
2183 KeyIdents.size(),
2184 /*AtArgumentEpression=*/true);
2185 else
2186 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2187 KeyIdents.data(),
2188 KeyIdents.size(),
2189 /*AtArgumentEpression=*/true);
2190
2191 ConsumeCodeCompletionToken();
2192 SkipUntil(tok::r_square);
2193 return ExprError();
2194 }
2195
John McCalldadc5752010-08-24 06:29:42 +00002196 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002197 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002198 // We must manually skip to a ']', otherwise the expression skipper will
2199 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2200 // the enclosing expression.
2201 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002202 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002203 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002204
Steve Naroff486760a2007-09-17 20:25:27 +00002205 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002206 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002207
Douglas Gregor1b605f72009-11-19 01:08:35 +00002208 // Code completion after each argument.
2209 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002210 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002211 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002212 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002213 KeyIdents.size(),
2214 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002215 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002216 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002217 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002218 KeyIdents.size(),
2219 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002220 else
John McCallb268a282010-08-23 23:25:46 +00002221 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002222 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002223 KeyIdents.size(),
2224 /*AtArgumentEpression=*/false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002225 ConsumeCodeCompletionToken();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002226 SkipUntil(tok::r_square);
2227 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002228 }
2229
Steve Naroff486760a2007-09-17 20:25:27 +00002230 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002231 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002232 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002233 break;
2234 // We have a selector or a colon, continue parsing.
2235 }
2236 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002237 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002238 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002239 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002240 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002241 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002242 // We must manually skip to a ']', otherwise the expression skipper will
2243 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2244 // the enclosing expression.
2245 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002246 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002247 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002248
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002249 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002250 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002251 }
2252 } else if (!selIdent) {
2253 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002254
Chris Lattner197a3012008-08-05 06:19:09 +00002255 // We must manually skip to a ']', otherwise the expression skipper will
2256 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2257 // the enclosing expression.
2258 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002259 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002260 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002261
Chris Lattner0ef13522007-10-09 17:51:17 +00002262 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002263 if (Tok.is(tok::identifier))
2264 Diag(Tok, diag::err_expected_colon);
2265 else
2266 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002267 // We must manually skip to a ']', otherwise the expression skipper will
2268 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2269 // the enclosing expression.
2270 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002271 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002272 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002273
Chris Lattner8f697062008-01-25 18:59:06 +00002274 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002275
Steve Naroffe61bfa82007-10-05 18:42:47 +00002276 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002277 if (nKeys == 0)
2278 KeyIdents.push_back(selIdent);
2279 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002280
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002281 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002282 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002283 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002284 MultiExprArg(Actions,
2285 KeyExprs.take(),
2286 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002287 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002288 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002289 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002290 MultiExprArg(Actions,
2291 KeyExprs.take(),
2292 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002293 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002294 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002295 MultiExprArg(Actions,
2296 KeyExprs.take(),
2297 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002298}
2299
John McCalldadc5752010-08-24 06:29:42 +00002300ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2301 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002302 if (Res.isInvalid()) return move(Res);
2303
Chris Lattnere002fbe2007-12-12 01:04:12 +00002304 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2305 // expressions. At this point, we know that the only valid thing that starts
2306 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002307 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002308 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002309 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002310 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002311
Chris Lattnere002fbe2007-12-12 01:04:12 +00002312 while (Tok.is(tok::at)) {
2313 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002314
Sebastian Redlc13f2682008-12-09 20:22:58 +00002315 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002316 if (!isTokenStringLiteral())
2317 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002318
John McCalldadc5752010-08-24 06:29:42 +00002319 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002320 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002321 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002322
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002323 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002324 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002325
2326 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2327 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002328}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002329
2330/// objc-encode-expression:
2331/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002332ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002333Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002334 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002335
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002336 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002337
Chris Lattner197a3012008-08-05 06:19:09 +00002338 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002339 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2340
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002341 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002342
Douglas Gregor220cac52009-02-18 17:45:20 +00002343 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002344
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002345 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002346
Douglas Gregor220cac52009-02-18 17:45:20 +00002347 if (Ty.isInvalid())
2348 return ExprError();
2349
Mike Stump11289f42009-09-09 15:08:12 +00002350 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002351 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002352}
Anders Carlssone01493d2007-08-23 15:25:28 +00002353
2354/// objc-protocol-expression
2355/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002356ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002357Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002358 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002359
Chris Lattner197a3012008-08-05 06:19:09 +00002360 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002361 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2362
Anders Carlssone01493d2007-08-23 15:25:28 +00002363 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002364
Chris Lattner197a3012008-08-05 06:19:09 +00002365 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002366 return ExprError(Diag(Tok, diag::err_expected_ident));
2367
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002368 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002369 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002370
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002371 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002372
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002373 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2374 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002375}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002376
2377/// objc-selector-expression
2378/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002379ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002380 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002381
Chris Lattner197a3012008-08-05 06:19:09 +00002382 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002383 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2384
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002385 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002386 SourceLocation LParenLoc = ConsumeParen();
2387 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002388
2389 if (Tok.is(tok::code_completion)) {
2390 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2391 KeyIdents.size());
2392 ConsumeCodeCompletionToken();
2393 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2394 return ExprError();
2395 }
2396
Chris Lattner4f472a32009-04-11 18:13:45 +00002397 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002398 if (!SelIdent && // missing selector name.
2399 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002400 return ExprError(Diag(Tok, diag::err_expected_ident));
2401
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002402 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002403 unsigned nColons = 0;
2404 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002405 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002406 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2407 ++nColons;
2408 KeyIdents.push_back(0);
2409 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002410 return ExprError(Diag(Tok, diag::err_expected_colon));
2411
Chris Lattner1ba64452010-08-27 22:32:41 +00002412 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002413 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002414 if (Tok.is(tok::r_paren))
2415 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002416
2417 if (Tok.is(tok::code_completion)) {
2418 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2419 KeyIdents.size());
2420 ConsumeCodeCompletionToken();
2421 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2422 return ExprError();
2423 }
2424
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002425 // Check for another keyword selector.
2426 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002427 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002428 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002429 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002430 break;
2431 }
Steve Naroff152dd812007-12-05 22:21:29 +00002432 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002433 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002434 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002435 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2436 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002437 }