blob: a8bf422766e6e5cb587b65aa85230cf46ce5e5e4 [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());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000039 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000045 break;
John McCall53fa7142010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall53fa7142010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000053 SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
54 break;
John McCall53fa7142010-12-24 02:08:15 +000055 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000056 case tok::objc_implementation:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000057 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
58 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000059 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000060 return ParseObjCAtEndDeclaration(AtLoc);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000061 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000062 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
64 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000065 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
67 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000068 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000069 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
70 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 default:
72 Diag(AtLoc, diag::err_unexpected_at);
73 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000074 SingleDecl = 0;
75 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000076 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078}
79
80///
Mike Stump11289f42009-09-09 15:08:12 +000081/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000082/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000083///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000084Parser::DeclGroupPtrTy
85Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000087 SmallVector<IdentifierInfo *, 8> ClassNames;
88 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +000089
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerda59c2f2006-11-05 02:08:13 +000091 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000092 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000093 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000096 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000097 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000098 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000099 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattner0ef13522007-10-09 17:51:17 +0000101 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000102 break;
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000104 ConsumeToken();
105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000107 // Consume the ';'.
108 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000109 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000110
Ted Kremeneka26da852009-11-17 23:12:20 +0000111 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
112 ClassLocs.data(),
113 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000114}
115
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000116///
117/// objc-interface:
118/// objc-class-interface-attributes[opt] objc-class-interface
119/// objc-category-interface
120///
121/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000122/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000123/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000124/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000125/// objc-interface-decl-list
126/// @end
127///
128/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000129/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000130/// objc-protocol-refs[opt]
131/// objc-interface-decl-list
132/// @end
133///
134/// objc-superclass:
135/// ':' identifier
136///
137/// objc-class-interface-attributes:
138/// __attribute__((visibility("default")))
139/// __attribute__((visibility("hidden")))
140/// __attribute__((deprecated))
141/// __attribute__((unavailable))
142/// __attribute__((objc_exception)) - used by NSException on 64-bit
143///
John McCall53fa7142010-12-24 02:08:15 +0000144Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
145 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000146 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
148 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000149
Douglas Gregor49c22a72009-11-18 16:26:39 +0000150 // Code completion after '@interface'.
151 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000152 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000153 cutOffParsing();
154 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000155 }
156
Chris Lattner0ef13522007-10-09 17:51:17 +0000157 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000159 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000160 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000161
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000162 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000163 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000164 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000165 if (Tok.is(tok::l_paren) &&
166 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000167 // TODO(dgregor): Use the return value from the next line to provide better
168 // recovery.
169 ConsumeParen();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000170 SourceLocation categoryLoc, rparenLoc;
171 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000172 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000173 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000174 cutOffParsing();
175 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000176 }
177
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000178 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000179 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 categoryId = Tok.getIdentifierInfo();
181 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000182 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000183 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000184 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000185 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000186 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000187 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000188 Diag(Tok, diag::err_expected_rparen);
189 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +0000190 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000191 }
192 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000193 // Next, we need to check for any protocol references.
194 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000195 SmallVector<Decl *, 8> ProtocolRefs;
196 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000197 if (Tok.is(tok::less) &&
198 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000199 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000200 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000201
John McCall53fa7142010-12-24 02:08:15 +0000202 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000203 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000204
John McCall48871652010-08-21 09:40:31 +0000205 Decl *CategoryType =
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000206 Actions.ActOnStartCategoryInterface(atLoc,
207 nameId, nameLoc,
208 categoryId, categoryLoc,
209 ProtocolRefs.data(),
210 ProtocolRefs.size(),
211 ProtocolLocs.data(),
212 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000213
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000214 if (Tok.is(tok::l_brace))
215 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
216
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000217 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000218 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 }
220 // Parse a class interface.
221 IdentifierInfo *superClassId = 0;
222 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000223
Chris Lattner0ef13522007-10-09 17:51:17 +0000224 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000225 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000226
227 // Code completion of superclass names.
228 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000229 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000230 cutOffParsing();
231 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000232 }
233
Chris Lattner0ef13522007-10-09 17:51:17 +0000234 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000235 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000236 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000237 }
238 superClassId = Tok.getIdentifierInfo();
239 superClassLoc = ConsumeToken();
240 }
241 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242 SmallVector<Decl *, 8> ProtocolRefs;
243 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000244 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000245 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000246 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
247 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000248 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000249
John McCall48871652010-08-21 09:40:31 +0000250 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000251 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000252 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000253 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000254 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000255 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattner0ef13522007-10-09 17:51:17 +0000257 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000258 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000259
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000260 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000261 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000262}
263
John McCall064d77b2009-12-03 22:31:13 +0000264/// The Objective-C property callback. This should be defined where
265/// it's used, but instead it's been lifted to here to support VS2005.
266struct Parser::ObjCPropertyCallback : FieldCallback {
267 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000268 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000269 ObjCDeclSpec &OCDS;
270 SourceLocation AtLoc;
271 tok::ObjCKeywordKind MethodImplKind;
272
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000273 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000274 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000275 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
276 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000277 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCall064d77b2009-12-03 22:31:13 +0000278 MethodImplKind(MethodImplKind) {
279 }
280
John McCall48871652010-08-21 09:40:31 +0000281 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000282 if (FD.D.getIdentifier() == 0) {
283 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
284 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000285 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000286 }
287 if (FD.BitfieldSize) {
288 P.Diag(AtLoc, diag::err_objc_property_bitfield)
289 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000290 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000291 }
292
293 // Install the property declarator into interfaceDecl.
294 IdentifierInfo *SelName =
295 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
296
297 Selector GetterSel =
298 P.PP.getSelectorTable().getNullarySelector(SelName);
299 IdentifierInfo *SetterName = OCDS.getSetterName();
300 Selector SetterSel;
301 if (SetterName)
302 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
303 else
304 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
305 P.PP.getSelectorTable(),
306 FD.D.getIdentifier());
307 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000308 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000309 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000310 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000311 &isOverridingProperty,
312 MethodImplKind);
313 if (!isOverridingProperty)
314 Props.push_back(Property);
315
316 return Property;
317 }
318};
319
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000320/// objc-interface-decl-list:
321/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000322/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000323/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000324/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000325/// objc-interface-decl-list declaration
326/// objc-interface-decl-list ';'
327///
Steve Naroff99264b42007-08-22 16:35:03 +0000328/// objc-method-requirement: [OBJC2]
329/// @required
330/// @optional
331///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000332void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
333 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000334 SmallVector<Decl *, 32> allMethods;
335 SmallVector<Decl *, 16> allProperties;
336 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000337 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Ted Kremenekc7c64312010-01-07 01:20:12 +0000339 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000340 Actions.ActOnObjCContainerStartDefinition(CDecl);
341
Steve Naroff99264b42007-08-22 16:35:03 +0000342 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000343 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000344 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000345 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000346 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000347 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000348 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
349 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000350 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
351 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000352 continue;
353 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000354 if (Tok.is(tok::l_paren)) {
355 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000356 ParseObjCMethodDecl(Tok.getLocation(),
357 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000358 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000359 continue;
360 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000361 // Ignore excess semicolons.
362 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000363 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 continue;
365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattnerda9fb152008-10-20 06:10:06 +0000367 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000368 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000369 break;
Mike Stump11289f42009-09-09 15:08:12 +0000370
Douglas Gregorf1934162010-01-13 21:24:21 +0000371 // Code completion within an Objective-C interface.
372 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000373 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000374 ObjCImpDecl? Sema::PCC_ObjCImplementation
375 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000376 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000377 }
378
Chris Lattner038a3e32008-10-20 05:46:22 +0000379 // If we don't have an @ directive, parse it as a function definition.
380 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000381 // The code below does not consume '}'s because it is afraid of eating the
382 // end of a namespace. Because of the way this code is structured, an
383 // erroneous r_brace would cause an infinite loop if not handled here.
384 if (Tok.is(tok::r_brace))
385 break;
John McCall084e83d2011-03-24 11:26:52 +0000386 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000387 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000388 continue;
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattner038a3e32008-10-20 05:46:22 +0000391 // Otherwise, we have an @ directive, eat the @.
392 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000393 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000394 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000395 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000396 break;
397 }
398
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000399 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000401 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000402 AtEnd.setBegin(AtLoc);
403 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000404 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000405 } else if (DirectiveKind == tok::objc_not_keyword) {
406 Diag(Tok, diag::err_objc_unknown_at);
407 SkipUntil(tok::semi);
408 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000409 }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattnerda9fb152008-10-20 06:10:06 +0000411 // Eat the identifier.
412 ConsumeToken();
413
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000414 switch (DirectiveKind) {
415 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000416 // FIXME: If someone forgets an @end on a protocol, this loop will
417 // continue to eat up tons of stuff and spew lots of nonsense errors. It
418 // would probably be better to bail out if we saw an @class or @interface
419 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000420 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000421 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000422 SkipUntil(tok::r_brace, tok::at);
423 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000424
425 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000426 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000427 Diag(Tok, diag::err_objc_missing_end);
428 ConsumeToken();
429 break;
430
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000431 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000432 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000433 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000434 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000435 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000436 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000437 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000438 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000439 break;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000441 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000442 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000443 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000444
Chris Lattner038a3e32008-10-20 05:46:22 +0000445 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000446 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000447 if (Tok.is(tok::l_paren))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000448 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000449
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000450 ObjCPropertyCallback Callback(*this, allProperties,
John McCall064d77b2009-12-03 22:31:13 +0000451 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000452
Chris Lattner038a3e32008-10-20 05:46:22 +0000453 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000454 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000455 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000456
John McCall405988b2011-03-26 01:53:26 +0000457 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000458 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000459 }
Steve Naroff99264b42007-08-22 16:35:03 +0000460 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000461
462 // We break out of the big loop in two cases: when we see @end or when we see
463 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000464 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000465 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000466 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000467 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000468 ConsumeToken(); // the "end" identifier
469 else
470 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000471
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000472 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000473 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000474 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000475 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000476 allProperties.data(), allProperties.size(),
477 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000478}
479
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000480/// Parse property attribute declarations.
481///
482/// property-attr-decl: '(' property-attrlist ')'
483/// property-attrlist:
484/// property-attribute
485/// property-attrlist ',' property-attribute
486/// property-attribute:
487/// getter '=' identifier
488/// setter '=' identifier ':'
489/// readonly
490/// readwrite
491/// assign
492/// retain
493/// copy
494/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000495/// atomic
496/// strong
497/// weak
498/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000499///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000500void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000501 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000502 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000504 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000505 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000506 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000507 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000508 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000509 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000510
Chris Lattner76619232008-10-20 07:22:18 +0000511 // If this is not an identifier at all, bail out early.
512 if (II == 0) {
513 MatchRHSPunctuation(tok::r_paren, LHSLoc);
514 return;
515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattner1db33542008-10-20 07:37:22 +0000517 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner68e48682008-11-20 04:42:34 +0000519 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000521 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000523 else if (II->isStr("unsafe_unretained"))
524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000525 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000527 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000528 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000529 else if (II->isStr("strong"))
530 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000531 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000532 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000533 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000534 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000535 else if (II->isStr("atomic"))
536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000537 else if (II->isStr("weak"))
538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000539 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000540 bool IsSetter = II->getNameStart()[0] == 's';
541
Chris Lattner825bca12008-10-20 07:39:53 +0000542 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000543 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
544 diag::err_objc_expected_equal_for_getter;
545
546 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000547 return;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregorc8537c52009-11-19 07:41:15 +0000549 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000550 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000551 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000552 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000553 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000554 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000555 }
556
Anders Carlssonfe15a782010-10-02 17:45:21 +0000557
558 SourceLocation SelLoc;
559 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
560
561 if (!SelIdent) {
562 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
563 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000564 SkipUntil(tok::r_paren);
565 return;
566 }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Anders Carlssonfe15a782010-10-02 17:45:21 +0000568 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000570 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000571
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000572 if (ExpectAndConsume(tok::colon,
573 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000574 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000575 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000576 } else {
577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000578 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000579 }
Chris Lattner825bca12008-10-20 07:39:53 +0000580 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000581 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000582 SkipUntil(tok::r_paren);
583 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner1db33542008-10-20 07:37:22 +0000586 if (Tok.isNot(tok::comma))
587 break;
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattner1db33542008-10-20 07:37:22 +0000589 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Chris Lattner1db33542008-10-20 07:37:22 +0000592 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000593}
594
Steve Naroff09bf8152007-09-06 21:24:23 +0000595/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000596/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000597/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000598///
599/// objc-instance-method: '-'
600/// objc-class-method: '+'
601///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000602/// objc-method-attributes: [OBJC2]
603/// __attribute__((deprecated))
604///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000605Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000606 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000607 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000608
Mike Stump11289f42009-09-09 15:08:12 +0000609 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000610 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000611 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000612 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000613 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000614 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000615 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000616}
617
618/// objc-selector:
619/// identifier
620/// one of
621/// enum struct union if else while do for switch case default
622/// break continue return goto asm sizeof typeof __alignof
623/// unsigned long const short volatile signed restrict _Complex
624/// in out inout bycopy byref oneway int char float double void _Bool
625///
Chris Lattner4f472a32009-04-11 18:13:45 +0000626IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000627
Chris Lattner5700fab2007-10-07 02:00:24 +0000628 switch (Tok.getKind()) {
629 default:
630 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000631 case tok::ampamp:
632 case tok::ampequal:
633 case tok::amp:
634 case tok::pipe:
635 case tok::tilde:
636 case tok::exclaim:
637 case tok::exclaimequal:
638 case tok::pipepipe:
639 case tok::pipeequal:
640 case tok::caret:
641 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000642 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000643 if (isalpha(ThisTok[0])) {
644 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
645 Tok.setKind(tok::identifier);
646 SelectorLoc = ConsumeToken();
647 return II;
648 }
649 return 0;
650 }
651
Chris Lattner5700fab2007-10-07 02:00:24 +0000652 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000653 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000654 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000655 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000656 case tok::kw_break:
657 case tok::kw_case:
658 case tok::kw_catch:
659 case tok::kw_char:
660 case tok::kw_class:
661 case tok::kw_const:
662 case tok::kw_const_cast:
663 case tok::kw_continue:
664 case tok::kw_default:
665 case tok::kw_delete:
666 case tok::kw_do:
667 case tok::kw_double:
668 case tok::kw_dynamic_cast:
669 case tok::kw_else:
670 case tok::kw_enum:
671 case tok::kw_explicit:
672 case tok::kw_export:
673 case tok::kw_extern:
674 case tok::kw_false:
675 case tok::kw_float:
676 case tok::kw_for:
677 case tok::kw_friend:
678 case tok::kw_goto:
679 case tok::kw_if:
680 case tok::kw_inline:
681 case tok::kw_int:
682 case tok::kw_long:
683 case tok::kw_mutable:
684 case tok::kw_namespace:
685 case tok::kw_new:
686 case tok::kw_operator:
687 case tok::kw_private:
688 case tok::kw_protected:
689 case tok::kw_public:
690 case tok::kw_register:
691 case tok::kw_reinterpret_cast:
692 case tok::kw_restrict:
693 case tok::kw_return:
694 case tok::kw_short:
695 case tok::kw_signed:
696 case tok::kw_sizeof:
697 case tok::kw_static:
698 case tok::kw_static_cast:
699 case tok::kw_struct:
700 case tok::kw_switch:
701 case tok::kw_template:
702 case tok::kw_this:
703 case tok::kw_throw:
704 case tok::kw_true:
705 case tok::kw_try:
706 case tok::kw_typedef:
707 case tok::kw_typeid:
708 case tok::kw_typename:
709 case tok::kw_typeof:
710 case tok::kw_union:
711 case tok::kw_unsigned:
712 case tok::kw_using:
713 case tok::kw_virtual:
714 case tok::kw_void:
715 case tok::kw_volatile:
716 case tok::kw_wchar_t:
717 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000718 case tok::kw__Bool:
719 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000720 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000721 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000722 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000723 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000724 }
Steve Naroff99264b42007-08-22 16:35:03 +0000725}
726
Fariborz Jahanian83615522008-01-02 22:54:34 +0000727/// objc-for-collection-in: 'in'
728///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000729bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000730 // FIXME: May have to do additional look-ahead to only allow for
731 // valid tokens following an 'in'; such as an identifier, unary operators,
732 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000733 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000734 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000735}
736
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000737/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000738/// qualifier list and builds their bitmask representation in the input
739/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000740///
741/// objc-type-qualifiers:
742/// objc-type-qualifier
743/// objc-type-qualifiers objc-type-qualifier
744///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000745void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
746 ObjCTypeNameContext Context) {
Chris Lattner61511e12007-12-12 06:56:32 +0000747 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000748 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000749 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
750 Context == OTN_ParameterType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000751 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000752 }
753
Chris Lattner5e530bc2007-12-27 19:57:00 +0000754 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000755 return;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattner61511e12007-12-12 06:56:32 +0000757 const IdentifierInfo *II = Tok.getIdentifierInfo();
758 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000759 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000760 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000761
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000762 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000763 switch (i) {
764 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000765 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
766 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
767 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
768 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
769 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
770 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000771 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000772 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000773 ConsumeToken();
774 II = 0;
775 break;
776 }
Mike Stump11289f42009-09-09 15:08:12 +0000777
Chris Lattner61511e12007-12-12 06:56:32 +0000778 // If this wasn't a recognized qualifier, bail out.
779 if (II) return;
780 }
781}
782
783/// objc-type-name:
784/// '(' objc-type-qualifiers[opt] type-name ')'
785/// '(' objc-type-qualifiers[opt] ')'
786///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000787ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
788 ObjCTypeNameContext Context) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000789 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000790
Chris Lattnerb7954432008-10-22 03:52:06 +0000791 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000792 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000793 ObjCDeclContextSwitch ObjCDC(*this);
794
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000795 // Parse type qualifiers, in, inout, etc.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000796 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000797
John McCallba7bf592010-08-24 05:47:05 +0000798 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000799 if (isTypeSpecifierQualifier()) {
John McCall31168b02011-06-15 23:02:42 +0000800 TypeResult TypeSpec =
801 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor220cac52009-02-18 17:45:20 +0000802 if (!TypeSpec.isInvalid())
803 Ty = TypeSpec.get();
Douglas Gregorbab8a962011-09-08 01:46:34 +0000804 } else if (Context == OTN_ResultType && Tok.is(tok::identifier)) {
805 if (!Ident_instancetype)
806 Ident_instancetype = PP.getIdentifierInfo("instancetype");
807
808 if (Tok.getIdentifierInfo() == Ident_instancetype) {
809 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
810 ConsumeToken();
811 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000812 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000813
Steve Naroff90255b42008-10-21 14:15:04 +0000814 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000815 ConsumeParen();
816 else if (Tok.getLocation() == TypeStartLoc) {
817 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000818 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000819 SkipUntil(tok::r_paren);
820 } else {
821 // Otherwise, we found *something*, but didn't get a ')' in the right
822 // place. Emit an error then return what we have as the type.
823 MatchRHSPunctuation(tok::r_paren, LParenLoc);
824 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000825 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000826}
827
828/// objc-method-decl:
829/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000830/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000831/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000832/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000833///
834/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000835/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000836/// objc-keyword-selector objc-keyword-decl
837///
838/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000839/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
840/// objc-selector ':' objc-keyword-attributes[opt] identifier
841/// ':' objc-type-name objc-keyword-attributes[opt] identifier
842/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000843///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000844/// objc-parmlist:
845/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000846///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000847/// objc-parms:
848/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000849///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000850/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000851/// , ...
852///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000853/// objc-keyword-attributes: [OBJC2]
854/// __attribute__((unused))
855///
John McCall48871652010-08-21 09:40:31 +0000856Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000857 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000858 tok::ObjCKeywordKind MethodImplKind,
859 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000860 ParsingDeclRAIIObject PD(*this);
861
Douglas Gregor636a61e2010-04-07 00:21:17 +0000862 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000863 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000864 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000865 cutOffParsing();
866 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000867 }
868
Chris Lattner2ebb1782008-08-23 01:48:03 +0000869 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000870 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000871 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000872 if (Tok.is(tok::l_paren))
Douglas Gregor95d3e372011-03-08 19:17:54 +0000873 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump11289f42009-09-09 15:08:12 +0000874
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000875 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000876 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000877 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000878 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000879
Douglas Gregor636a61e2010-04-07 00:21:17 +0000880 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000881 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000882 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000883 cutOffParsing();
884 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000885 }
886
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000887 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000888 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000889 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000890
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000891 // An unnamed colon is valid.
892 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000893 Diag(Tok, diag::err_expected_selector_for_method)
894 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000895 // Skip until we get a ; or {}.
896 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000897 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000900 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000901 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000902 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000903 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000904 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +0000905
Chris Lattner5700fab2007-10-07 02:00:24 +0000906 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000907 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000908 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000909 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +0000910 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000911 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +0000912 methodAttrs.getList(), MethodImplKind,
913 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +0000914 PD.complete(Result);
915 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000916 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000917
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000918 SmallVector<IdentifierInfo *, 12> KeyIdents;
919 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000920 ParseScope PrototypeScope(this,
921 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +0000922
923 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000924
Chris Lattner5700fab2007-10-07 02:00:24 +0000925 while (1) {
John McCall084e83d2011-03-24 11:26:52 +0000926 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +0000927 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattner5700fab2007-10-07 02:00:24 +0000929 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000930 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000931 Diag(Tok, diag::err_expected_colon);
932 break;
933 }
934 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000935
John McCallba7bf592010-08-24 05:47:05 +0000936 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000937 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000938 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000939
Chris Lattner5700fab2007-10-07 02:00:24 +0000940 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000941 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +0000942 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +0000943 MaybeParseGNUAttributes(paramAttrs);
944 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +0000945 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000946
Douglas Gregor45879692010-07-08 23:37:41 +0000947 // Code completion for the next piece of the selector.
948 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +0000949 KeyIdents.push_back(SelIdent);
950 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
951 mType == tok::minus,
952 /*AtParameterName=*/true,
953 ReturnType,
954 KeyIdents.data(),
955 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000956 cutOffParsing();
957 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +0000958 }
959
Chris Lattner0ef13522007-10-09 17:51:17 +0000960 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000961 Diag(Tok, diag::err_expected_ident); // missing argument name.
962 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000963 }
Mike Stump11289f42009-09-09 15:08:12 +0000964
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000965 ArgInfo.Name = Tok.getIdentifierInfo();
966 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000967 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000968
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000969 ArgInfos.push_back(ArgInfo);
970 KeyIdents.push_back(SelIdent);
971
John McCall084e83d2011-03-24 11:26:52 +0000972 // Make sure the attributes persist.
973 allParamAttrs.takeAllFrom(paramAttrs.getPool());
974
Douglas Gregor95887f92010-07-08 23:20:03 +0000975 // Code completion for the next piece of the selector.
976 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +0000977 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
978 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000979 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000980 ReturnType,
981 KeyIdents.data(),
982 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000983 cutOffParsing();
984 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +0000985 }
986
Chris Lattner5700fab2007-10-07 02:00:24 +0000987 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000988 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000989 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000990 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000991 break;
992 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000995 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattner5700fab2007-10-07 02:00:24 +0000997 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000998 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000999 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001000 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001001 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001002 ConsumeToken();
1003 break;
1004 }
John McCall084e83d2011-03-24 11:26:52 +00001005 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001006 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001007 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001008 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1009 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001010 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001011 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001012 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1013 ParmDecl.getIdentifierLoc(),
1014 Param,
1015 0));
1016
Chris Lattner5700fab2007-10-07 02:00:24 +00001017 }
Mike Stump11289f42009-09-09 15:08:12 +00001018
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001019 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001020 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001021 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001022 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001023
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001024 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001025 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001026
Chris Lattner5700fab2007-10-07 02:00:24 +00001027 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1028 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001029 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001030 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001031 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001032 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001033 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001034 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001035 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001036
John McCall28a6aea2009-11-04 02:18:39 +00001037 PD.complete(Result);
1038 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001039}
1040
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001041/// objc-protocol-refs:
1042/// '<' identifier-list '>'
1043///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001044bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001045ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1046 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001047 bool WarnOnDeclarations,
1048 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001049 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001050
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001051 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001053 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattner3bbae002008-07-26 04:03:38 +00001055 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001056 if (Tok.is(tok::code_completion)) {
1057 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1058 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001059 cutOffParsing();
1060 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001061 }
1062
Chris Lattner3bbae002008-07-26 04:03:38 +00001063 if (Tok.isNot(tok::identifier)) {
1064 Diag(Tok, diag::err_expected_ident);
1065 SkipUntil(tok::greater);
1066 return true;
1067 }
1068 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1069 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001070 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001071 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattner3bbae002008-07-26 04:03:38 +00001073 if (Tok.isNot(tok::comma))
1074 break;
1075 ConsumeToken();
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner3bbae002008-07-26 04:03:38 +00001078 // Consume the '>'.
1079 if (Tok.isNot(tok::greater)) {
1080 Diag(Tok, diag::err_expected_greater);
1081 return true;
1082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattner3bbae002008-07-26 04:03:38 +00001084 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattner3bbae002008-07-26 04:03:38 +00001086 // Convert the list of protocols identifiers into a list of protocol decls.
1087 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1088 &ProtocolIdents[0], ProtocolIdents.size(),
1089 Protocols);
1090 return false;
1091}
1092
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001093/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1094/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001095bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001096 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1097 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1098 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001099 SmallVector<Decl *, 8> ProtocolDecl;
1100 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001101 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1102 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001103 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1104 ProtocolLocs.data(), LAngleLoc);
1105 if (EndProtoLoc.isValid())
1106 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001107 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001108}
1109
1110
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001111/// objc-class-instance-variables:
1112/// '{' objc-instance-variable-decl-list[opt] '}'
1113///
1114/// objc-instance-variable-decl-list:
1115/// objc-visibility-spec
1116/// objc-instance-variable-decl ';'
1117/// ';'
1118/// objc-instance-variable-decl-list objc-visibility-spec
1119/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1120/// objc-instance-variable-decl-list ';'
1121///
1122/// objc-visibility-spec:
1123/// @private
1124/// @protected
1125/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001126/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001127///
1128/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001129/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001130///
John McCall48871652010-08-21 09:40:31 +00001131void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001132 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001133 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001134 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001135 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001136
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001137 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001138
Steve Naroff00433d32007-08-21 21:17:12 +00001139 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001140
Steve Naroff00433d32007-08-21 21:17:12 +00001141 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001142 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001143 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001144
Steve Naroff00433d32007-08-21 21:17:12 +00001145 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001146 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001147 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001148 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001149 ConsumeToken();
1150 continue;
1151 }
Mike Stump11289f42009-09-09 15:08:12 +00001152
Steve Naroff00433d32007-08-21 21:17:12 +00001153 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001154 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001155 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001156
1157 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001158 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001159 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001160 }
1161
Steve Naroff7c348172007-08-23 18:16:40 +00001162 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001163 case tok::objc_private:
1164 case tok::objc_public:
1165 case tok::objc_protected:
1166 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001167 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001168 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001169 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001170 default:
1171 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001172 continue;
1173 }
1174 }
Mike Stump11289f42009-09-09 15:08:12 +00001175
Douglas Gregor48d46252010-01-13 21:54:15 +00001176 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001177 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001178 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001179 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001180 }
1181
John McCallcfefb6d2009-11-03 02:38:08 +00001182 struct ObjCIvarCallback : FieldCallback {
1183 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001184 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001185 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001186 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001187
John McCall48871652010-08-21 09:40:31 +00001188 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001189 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001190 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1191 }
1192
John McCall48871652010-08-21 09:40:31 +00001193 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001194 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001195 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001196 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001197 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001198 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001199 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001200 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001201 if (Field)
1202 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001203 return Field;
1204 }
1205 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001206
Chris Lattnera12405b2008-04-10 06:46:29 +00001207 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001208 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001209 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattner0ef13522007-10-09 17:51:17 +00001211 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001212 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001213 } else {
1214 Diag(Tok, diag::err_expected_semi_decl_list);
1215 // Skip to end of block or statement
1216 SkipUntil(tok::r_brace, true, true);
1217 }
1218 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001219 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001220 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1221 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001222 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001223 // Call ActOnFields() even if we don't have any decls. This is useful
1224 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001225 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie751c5582011-09-22 02:58:26 +00001226 AllIvarDecls,
Daniel Dunbar15619c72008-10-03 02:03:53 +00001227 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001228 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001229}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001230
1231/// objc-protocol-declaration:
1232/// objc-protocol-definition
1233/// objc-protocol-forward-reference
1234///
1235/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001236/// @protocol identifier
1237/// objc-protocol-refs[opt]
1238/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001239/// @end
1240///
1241/// objc-protocol-forward-reference:
1242/// @protocol identifier-list ';'
1243///
1244/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001245/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001246/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001247Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001248 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001249 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001250 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1251 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001252
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001253 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001254 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001255 cutOffParsing();
1256 return 0;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001257 }
1258
Chris Lattner0ef13522007-10-09 17:51:17 +00001259 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001260 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001261 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001262 }
1263 // Save the protocol name, then consume it.
1264 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1265 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattner0ef13522007-10-09 17:51:17 +00001267 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001268 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001269 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001270 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001271 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001272 }
Mike Stump11289f42009-09-09 15:08:12 +00001273
Chris Lattner0ef13522007-10-09 17:51:17 +00001274 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001275 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001276 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1277
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001278 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001279 while (1) {
1280 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001281 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001282 Diag(Tok, diag::err_expected_ident);
1283 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001284 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001285 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001286 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1287 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001288 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001289
Chris Lattner0ef13522007-10-09 17:51:17 +00001290 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001291 break;
1292 }
1293 // Consume the ';'.
1294 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001295 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001296
Steve Naroff93eb5f12007-10-10 17:32:04 +00001297 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001298 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001299 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001300 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001301 }
Mike Stump11289f42009-09-09 15:08:12 +00001302
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001303 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001304 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001305
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001306 SmallVector<Decl *, 8> ProtocolRefs;
1307 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001308 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001309 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1310 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001311 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001312
John McCall48871652010-08-21 09:40:31 +00001313 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001314 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001315 ProtocolRefs.data(),
1316 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001317 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001318 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001319
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001320 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001321 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001322}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001323
1324/// objc-implementation:
1325/// objc-class-implementation-prologue
1326/// objc-category-implementation-prologue
1327///
1328/// objc-class-implementation-prologue:
1329/// @implementation identifier objc-superclass[opt]
1330/// objc-class-instance-variables[opt]
1331///
1332/// objc-category-implementation-prologue:
1333/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001334Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001335 SourceLocation atLoc) {
1336 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1337 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1338 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001339
Douglas Gregor49c22a72009-11-18 16:26:39 +00001340 // Code completion after '@implementation'.
1341 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001342 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001343 cutOffParsing();
1344 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +00001345 }
1346
Chris Lattner0ef13522007-10-09 17:51:17 +00001347 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001348 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001349 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001350 }
1351 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001352 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001353 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001354
1355 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001356 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001357 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001358 SourceLocation categoryLoc, rparenLoc;
1359 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001360
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001361 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001362 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001363 cutOffParsing();
1364 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001365 }
1366
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001368 categoryId = Tok.getIdentifierInfo();
1369 categoryLoc = ConsumeToken();
1370 } else {
1371 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001372 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001373 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001374 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001375 Diag(Tok, diag::err_expected_rparen);
1376 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001377 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001378 }
1379 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001380 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001381 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001382 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001383
1384 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001385 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001386 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001387 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001388 }
1389 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001390 SourceLocation superClassLoc;
1391 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001392 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001393 // We have a super class
1394 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001395 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001396 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001397 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001398 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001399 superClassId = Tok.getIdentifierInfo();
1400 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001401 }
John McCall48871652010-08-21 09:40:31 +00001402 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001403 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001404 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001405
Steve Naroff33a1e802007-10-29 21:38:07 +00001406 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001407 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1408
1409 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001410 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001411 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001412 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001413}
Steve Naroff33a1e802007-10-29 21:38:07 +00001414
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001415Parser::DeclGroupPtrTy
1416Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001417 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1418 "ParseObjCAtEndDeclaration(): Expected @end");
1419 ConsumeToken(); // the "end" identifier
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001420 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001421 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001422 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1423 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1424 DeclsInGroup.push_back(D);
1425 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001426 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001427
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001428 if (ObjCImpDecl) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001429 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001430 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001431 }
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001432 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001433 // missing @implementation
Fariborz Jahanianc0577942011-04-22 22:02:28 +00001434 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001435
1436 LateParsedObjCMethods.clear();
1437 ObjCImpDecl = 0;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001438 return Actions.BuildDeclaratorGroup(
1439 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001440}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001441
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001442Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1443 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001444 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001445 return Actions.ConvertDeclToDeclGroup(0);
1446 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001447 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001448 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1449}
1450
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001451/// compatibility-alias-decl:
1452/// @compatibility_alias alias-name class-name ';'
1453///
John McCall48871652010-08-21 09:40:31 +00001454Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001455 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1456 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1457 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001458 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001459 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001460 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001461 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001462 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1463 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001464 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001465 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001466 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001467 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001468 IdentifierInfo *classId = Tok.getIdentifierInfo();
1469 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001470 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1471 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001472 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1473 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001474}
1475
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001476/// property-synthesis:
1477/// @synthesize property-ivar-list ';'
1478///
1479/// property-ivar-list:
1480/// property-ivar
1481/// property-ivar-list ',' property-ivar
1482///
1483/// property-ivar:
1484/// identifier
1485/// identifier '=' identifier
1486///
John McCall48871652010-08-21 09:40:31 +00001487Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001488 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1489 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001490 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001491
Douglas Gregor88e72a02009-11-18 19:45:45 +00001492 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001493 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001494 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001495 cutOffParsing();
1496 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001497 }
1498
Douglas Gregor88e72a02009-11-18 19:45:45 +00001499 if (Tok.isNot(tok::identifier)) {
1500 Diag(Tok, diag::err_synthesized_property_name);
1501 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001502 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001503 }
1504
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001505 IdentifierInfo *propertyIvar = 0;
1506 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1507 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001508 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001509 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001510 // property '=' ivar-name
1511 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001512
1513 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001514 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001515 cutOffParsing();
1516 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001517 }
1518
Chris Lattner0ef13522007-10-09 17:51:17 +00001519 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001520 Diag(Tok, diag::err_expected_ident);
1521 break;
1522 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001523 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001524 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001525 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001526 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001527 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001528 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001529 break;
1530 ConsumeToken(); // consume ','
1531 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001532 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001533 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001534}
1535
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001536/// property-dynamic:
1537/// @dynamic property-list
1538///
1539/// property-list:
1540/// identifier
1541/// property-list ',' identifier
1542///
John McCall48871652010-08-21 09:40:31 +00001543Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001544 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1545 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001546 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001547 while (true) {
1548 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001549 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001550 cutOffParsing();
1551 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001552 }
1553
1554 if (Tok.isNot(tok::identifier)) {
1555 Diag(Tok, diag::err_expected_ident);
1556 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001557 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001558 }
1559
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001560 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1561 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001562 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001563 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001564
Chris Lattner0ef13522007-10-09 17:51:17 +00001565 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001566 break;
1567 ConsumeToken(); // consume ','
1568 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001569 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001570 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001571}
Mike Stump11289f42009-09-09 15:08:12 +00001572
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001573/// objc-throw-statement:
1574/// throw expression[opt];
1575///
John McCalldadc5752010-08-24 06:29:42 +00001576StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1577 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001578 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001579 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001580 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001581 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001582 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001583 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001584 }
1585 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001586 // consume ';'
1587 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001588 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001589}
1590
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001591/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001592/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001593///
John McCalldadc5752010-08-24 06:29:42 +00001594StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001595Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001596 ConsumeToken(); // consume synchronized
1597 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001598 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001599 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001600 }
John McCalld9bb7432011-07-27 21:50:02 +00001601
1602 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001603 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001604 ExprResult operand(ParseExpression());
1605
1606 if (Tok.is(tok::r_paren)) {
1607 ConsumeParen(); // ')'
1608 } else {
1609 if (!operand.isInvalid())
1610 Diag(Tok, diag::err_expected_rparen);
1611
1612 // Skip forward until we see a left brace, but don't consume it.
1613 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001614 }
John McCalld9bb7432011-07-27 21:50:02 +00001615
1616 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001617 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001618 if (!operand.isInvalid())
1619 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001620 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001621 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001622
John McCalld9bb7432011-07-27 21:50:02 +00001623 // Check the @synchronized operand now.
1624 if (!operand.isInvalid())
1625 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001626
John McCalld9bb7432011-07-27 21:50:02 +00001627 // Parse the compound statement within a new scope.
1628 ParseScope bodyScope(this, Scope::DeclScope);
1629 StmtResult body(ParseCompoundStatementBody());
1630 bodyScope.Exit();
1631
1632 // If there was a semantic or parse error earlier with the
1633 // operand, fail now.
1634 if (operand.isInvalid())
1635 return StmtError();
1636
1637 if (body.isInvalid())
1638 body = Actions.ActOnNullStmt(Tok.getLocation());
1639
1640 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001641}
1642
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001643/// objc-try-catch-statement:
1644/// @try compound-statement objc-catch-list[opt]
1645/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1646///
1647/// objc-catch-list:
1648/// @catch ( parameter-declaration ) compound-statement
1649/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1650/// catch-parameter-declaration:
1651/// parameter-declaration
1652/// '...' [OBJC2]
1653///
John McCalldadc5752010-08-24 06:29:42 +00001654StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001655 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001656
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001657 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001658 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001659 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001660 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001661 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001662 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001663 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001664 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001665 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001666 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001667 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001668 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001669
Chris Lattner0ef13522007-10-09 17:51:17 +00001670 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001671 // At this point, we need to lookahead to determine if this @ is the start
1672 // of an @catch or @finally. We don't want to consume the @ token if this
1673 // is an @try or @encode or something else.
1674 Token AfterAt = GetLookAheadToken(1);
1675 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1676 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1677 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001678
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001679 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001680 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001681 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001682 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001683 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001684 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001685 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001686 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001687 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001688 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001689 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001690 ParseDeclarator(ParmDecl);
1691
Douglas Gregore11ee112010-04-23 23:01:43 +00001692 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001693 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001694 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001695 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001696 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001697
Steve Naroff65a00892009-04-07 22:56:58 +00001698 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001699
Steve Naroff65a00892009-04-07 22:56:58 +00001700 if (Tok.is(tok::r_paren))
1701 RParenLoc = ConsumeParen();
1702 else // Skip over garbage, until we get to ')'. Eat the ')'.
1703 SkipUntil(tok::r_paren, true, false);
1704
John McCalldadc5752010-08-24 06:29:42 +00001705 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001706 if (Tok.is(tok::l_brace))
1707 CatchBody = ParseCompoundStatementBody();
1708 else
1709 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001710 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001711 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001712
John McCalldadc5752010-08-24 06:29:42 +00001713 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001714 RParenLoc,
1715 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001716 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001717 if (!Catch.isInvalid())
1718 CatchStmts.push_back(Catch.release());
1719
Steve Naroffe6016792008-02-05 21:27:35 +00001720 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001721 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1722 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001723 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001724 }
1725 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001726 } else {
1727 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001728 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001729 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001730
John McCalldadc5752010-08-24 06:29:42 +00001731 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001732 if (Tok.is(tok::l_brace))
1733 FinallyBody = ParseCompoundStatementBody();
1734 else
1735 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001736 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001737 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001738 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001739 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001740 catch_or_finally_seen = true;
1741 break;
1742 }
1743 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001744 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001745 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001746 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001747 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001748
John McCallb268a282010-08-23 23:25:46 +00001749 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001750 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001751 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001752}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001753
John McCall31168b02011-06-15 23:02:42 +00001754/// objc-autoreleasepool-statement:
1755/// @autoreleasepool compound-statement
1756///
1757StmtResult
1758Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1759 ConsumeToken(); // consume autoreleasepool
1760 if (Tok.isNot(tok::l_brace)) {
1761 Diag(Tok, diag::err_expected_lbrace);
1762 return StmtError();
1763 }
1764 // Enter a scope to hold everything within the compound stmt. Compound
1765 // statements can always hold declarations.
1766 ParseScope BodyScope(this, Scope::DeclScope);
1767
1768 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1769
1770 BodyScope.Exit();
1771 if (AutoreleasePoolBody.isInvalid())
1772 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1773 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1774 AutoreleasePoolBody.take());
1775}
1776
Steve Naroff09bf8152007-09-06 21:24:23 +00001777/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001778///
John McCall48871652010-08-21 09:40:31 +00001779Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001780 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001781
John McCallfaf5fb42010-08-26 23:41:50 +00001782 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1783 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001784
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001785 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001786 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001787 if (ObjCImpDecl) {
1788 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001789 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001790 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001791 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001792 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001793
Steve Naroffbb875722007-11-11 19:54:21 +00001794 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001795 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001796 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001797
Steve Naroffbb875722007-11-11 19:54:21 +00001798 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1799 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001800
Steve Naroffbb875722007-11-11 19:54:21 +00001801 // If we didn't find the '{', bail out.
1802 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001803 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001804 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001805 // Allow the rest of sema to find private method decl implementations.
1806 if (MDecl)
1807 Actions.AddAnyMethodToGlobalPool(MDecl);
1808
1809 // Consume the tokens and store them for later parsing.
1810 LexedMethod* LM = new LexedMethod(this, MDecl);
1811 LateParsedObjCMethods.push_back(LM);
1812 CachedTokens &Toks = LM->Toks;
1813 // Begin by storing the '{' token.
1814 Toks.push_back(Tok);
1815 ConsumeBrace();
1816 // Consume everything up to (and including) the matching right brace.
1817 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff7b8fa472007-11-13 23:01:27 +00001818 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001819}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001820
John McCalldadc5752010-08-24 06:29:42 +00001821StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001822 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001823 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001824 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001825 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001826 }
1827
1828 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001829 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001830
1831 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001832 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001833
1834 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001835 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001836
1837 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1838 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001839
John McCalldadc5752010-08-24 06:29:42 +00001840 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001841 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001842 // If the expression is invalid, skip ahead to the next semicolon. Not
1843 // doing this opens us up to the possibility of infinite loops if
1844 // ParseExpression does not consume any tokens.
1845 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001846 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001847 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001848
Steve Naroffe6016792008-02-05 21:27:35 +00001849 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001850 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001851 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001852}
1853
John McCalldadc5752010-08-24 06:29:42 +00001854ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001855 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001856 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001857 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001858 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001859 return ExprError();
1860
Chris Lattnere002fbe2007-12-12 01:04:12 +00001861 case tok::string_literal: // primary-expression: string-literal
1862 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001863 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001864 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001865 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001866 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001867
Chris Lattner197a3012008-08-05 06:19:09 +00001868 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1869 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001870 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001871 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001872 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001873 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001874 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001875 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001876 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001877 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001878 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001879}
1880
Douglas Gregor8d4de672010-04-21 22:36:40 +00001881/// \brirg Parse the receiver of an Objective-C++ message send.
1882///
1883/// This routine parses the receiver of a message send in
1884/// Objective-C++ either as a type or as an expression. Note that this
1885/// routine must not be called to parse a send to 'super', since it
1886/// has no way to return such a result.
1887///
1888/// \param IsExpr Whether the receiver was parsed as an expression.
1889///
1890/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1891/// IsExpr is true), the parsed expression. If the receiver was parsed
1892/// as a type (\c IsExpr is false), the parsed type.
1893///
1894/// \returns True if an error occurred during parsing or semantic
1895/// analysis, in which case the arguments do not have valid
1896/// values. Otherwise, returns false for a successful parse.
1897///
1898/// objc-receiver: [C++]
1899/// 'super' [not parsed here]
1900/// expression
1901/// simple-type-specifier
1902/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001903bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001904 InMessageExpressionRAIIObject InMessage(*this, true);
1905
Douglas Gregor8d4de672010-04-21 22:36:40 +00001906 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1907 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1908 TryAnnotateTypeOrScopeToken();
1909
1910 if (!isCXXSimpleTypeSpecifier()) {
1911 // objc-receiver:
1912 // expression
John McCalldadc5752010-08-24 06:29:42 +00001913 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001914 if (Receiver.isInvalid())
1915 return true;
1916
1917 IsExpr = true;
1918 TypeOrExpr = Receiver.take();
1919 return false;
1920 }
1921
1922 // objc-receiver:
1923 // typename-specifier
1924 // simple-type-specifier
1925 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00001926 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001927 ParseCXXSimpleTypeSpecifier(DS);
1928
1929 if (Tok.is(tok::l_paren)) {
1930 // If we see an opening parentheses at this point, we are
1931 // actually parsing an expression that starts with a
1932 // function-style cast, e.g.,
1933 //
1934 // postfix-expression:
1935 // simple-type-specifier ( expression-list [opt] )
1936 // typename-specifier ( expression-list [opt] )
1937 //
1938 // Parse the remainder of this case, then the (optional)
1939 // postfix-expression suffix, followed by the (optional)
1940 // right-hand side of the binary expression. We have an
1941 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001942 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001943 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001944 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001945 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001946 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001947 if (Receiver.isInvalid())
1948 return true;
1949
1950 IsExpr = true;
1951 TypeOrExpr = Receiver.take();
1952 return false;
1953 }
1954
1955 // We have a class message. Turn the simple-type-specifier or
1956 // typename-specifier we parsed into a type and parse the
1957 // remainder of the class message.
1958 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001959 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001960 if (Type.isInvalid())
1961 return true;
1962
1963 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001964 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001965 return false;
1966}
1967
Douglas Gregor990ccac2010-05-31 14:40:22 +00001968/// \brief Determine whether the parser is currently referring to a an
1969/// Objective-C message send, using a simplified heuristic to avoid overhead.
1970///
1971/// This routine will only return true for a subset of valid message-send
1972/// expressions.
1973bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001974 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001975 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001976 return GetLookAheadToken(1).is(tok::identifier) &&
1977 GetLookAheadToken(2).is(tok::identifier);
1978}
1979
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001980bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1981 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1982 InMessageExpression)
1983 return false;
1984
1985
1986 ParsedType Type;
1987
1988 if (Tok.is(tok::annot_typename))
1989 Type = getTypeAnnotation(Tok);
1990 else if (Tok.is(tok::identifier))
1991 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1992 getCurScope());
1993 else
1994 return false;
1995
1996 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1997 const Token &AfterNext = GetLookAheadToken(2);
1998 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1999 if (Tok.is(tok::identifier))
2000 TryAnnotateTypeOrScopeToken();
2001
2002 return Tok.is(tok::annot_typename);
2003 }
2004 }
2005
2006 return false;
2007}
2008
Mike Stump11289f42009-09-09 15:08:12 +00002009/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002010/// '[' objc-receiver objc-message-args ']'
2011///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002012/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002013/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002014/// expression
2015/// class-name
2016/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002017///
John McCalldadc5752010-08-24 06:29:42 +00002018ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002019 assert(Tok.is(tok::l_square) && "'[' expected");
2020 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2021
Douglas Gregora817a192010-05-27 23:06:34 +00002022 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002023 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002024 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002025 return ExprError();
2026 }
2027
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002028 InMessageExpressionRAIIObject InMessage(*this, true);
2029
Douglas Gregor8d4de672010-04-21 22:36:40 +00002030 if (getLang().CPlusPlus) {
2031 // We completely separate the C and C++ cases because C++ requires
2032 // more complicated (read: slower) parsing.
2033
2034 // Handle send to super.
2035 // FIXME: This doesn't benefit from the same typo-correction we
2036 // get in Objective-C.
2037 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002038 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002039 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2040 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002041
2042 // Parse the receiver, which is either a type or an expression.
2043 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002044 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002045 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2046 SkipUntil(tok::r_square);
2047 return ExprError();
2048 }
2049
2050 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002051 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2052 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002053 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002054
2055 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002056 ParsedType::getFromOpaquePtr(TypeOrExpr),
2057 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002058 }
2059
2060 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002061 IdentifierInfo *Name = Tok.getIdentifierInfo();
2062 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002063 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002064 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002065 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002066 NextToken().is(tok::period),
2067 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002068 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002069 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2070 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002071
John McCallfaf5fb42010-08-26 23:41:50 +00002072 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002073 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002074 SkipUntil(tok::r_square);
2075 return ExprError();
2076 }
2077
Douglas Gregore5798dc2010-04-21 20:38:13 +00002078 ConsumeToken(); // the type name
2079
2080 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002081 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002082
John McCallfaf5fb42010-08-26 23:41:50 +00002083 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002084 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002085 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002086 }
Chris Lattner8f697062008-01-25 18:59:06 +00002087 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002088
2089 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002090 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002091 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002092 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002093 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002094 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002095
John McCallba7bf592010-08-24 05:47:05 +00002096 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2097 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002098}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002099
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002100/// \brief Parse the remainder of an Objective-C message following the
2101/// '[' objc-receiver.
2102///
2103/// This routine handles sends to super, class messages (sent to a
2104/// class name), and instance messages (sent to an object), and the
2105/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2106/// ReceiverExpr, respectively. Only one of these parameters may have
2107/// a valid value.
2108///
2109/// \param LBracLoc The location of the opening '['.
2110///
2111/// \param SuperLoc If this is a send to 'super', the location of the
2112/// 'super' keyword that indicates a send to the superclass.
2113///
2114/// \param ReceiverType If this is a class message, the type of the
2115/// class we are sending a message to.
2116///
2117/// \param ReceiverExpr If this is an instance message, the expression
2118/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002119///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002120/// objc-message-args:
2121/// objc-selector
2122/// objc-keywordarg-list
2123///
2124/// objc-keywordarg-list:
2125/// objc-keywordarg
2126/// objc-keywordarg-list objc-keywordarg
2127///
Mike Stump11289f42009-09-09 15:08:12 +00002128/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002129/// selector-name[opt] ':' objc-keywordexpr
2130///
2131/// objc-keywordexpr:
2132/// nonempty-expr-list
2133///
2134/// nonempty-expr-list:
2135/// assignment-expression
2136/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002137///
John McCalldadc5752010-08-24 06:29:42 +00002138ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002139Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002140 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002141 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002142 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002143 InMessageExpressionRAIIObject InMessage(*this, true);
2144
Steve Naroffeae65032009-11-07 02:08:14 +00002145 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002146 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002147 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2148 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002149 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002150 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2151 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002152 else
John McCallb268a282010-08-23 23:25:46 +00002153 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002154 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002155 cutOffParsing();
2156 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002157 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002158
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002159 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002160 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002161 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002162
Anders Carlsson978f08d2009-02-14 18:21:46 +00002163 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002164
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002165 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002166 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002167
Chris Lattner0ef13522007-10-09 17:51:17 +00002168 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002169 while (1) {
2170 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002171 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002172
Chris Lattner0ef13522007-10-09 17:51:17 +00002173 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002174 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002175 // We must manually skip to a ']', otherwise the expression skipper will
2176 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2177 // the enclosing expression.
2178 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002179 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002180 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002181
Steve Narofff73590d2007-09-27 14:38:14 +00002182 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002183 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002184
2185 if (Tok.is(tok::code_completion)) {
2186 if (SuperLoc.isValid())
2187 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2188 KeyIdents.data(),
2189 KeyIdents.size(),
2190 /*AtArgumentEpression=*/true);
2191 else if (ReceiverType)
2192 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2193 KeyIdents.data(),
2194 KeyIdents.size(),
2195 /*AtArgumentEpression=*/true);
2196 else
2197 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2198 KeyIdents.data(),
2199 KeyIdents.size(),
2200 /*AtArgumentEpression=*/true);
2201
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002202 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002203 return ExprError();
2204 }
2205
John McCalldadc5752010-08-24 06:29:42 +00002206 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002207 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002208 // We must manually skip to a ']', otherwise the expression skipper will
2209 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2210 // the enclosing expression.
2211 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002212 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002213 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002214
Steve Naroff486760a2007-09-17 20:25:27 +00002215 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002216 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002217
Douglas Gregor1b605f72009-11-19 01:08:35 +00002218 // Code completion after each argument.
2219 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002220 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002221 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002222 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002223 KeyIdents.size(),
2224 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002225 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002226 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002227 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002228 KeyIdents.size(),
2229 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002230 else
John McCallb268a282010-08-23 23:25:46 +00002231 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002232 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002233 KeyIdents.size(),
2234 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002235 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002236 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002237 }
2238
Steve Naroff486760a2007-09-17 20:25:27 +00002239 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002240 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002241 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002242 break;
2243 // We have a selector or a colon, continue parsing.
2244 }
2245 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002246 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002247 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002248 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002249 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002250 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002251 // We must manually skip to a ']', otherwise the expression skipper will
2252 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2253 // the enclosing expression.
2254 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002255 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002256 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002257
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002258 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002259 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002260 }
2261 } else if (!selIdent) {
2262 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002263
Chris Lattner197a3012008-08-05 06:19:09 +00002264 // We must manually skip to a ']', otherwise the expression skipper will
2265 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2266 // the enclosing expression.
2267 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002268 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002269 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002270
Chris Lattner0ef13522007-10-09 17:51:17 +00002271 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002272 if (Tok.is(tok::identifier))
2273 Diag(Tok, diag::err_expected_colon);
2274 else
2275 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002276 // We must manually skip to a ']', otherwise the expression skipper will
2277 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2278 // the enclosing expression.
2279 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002280 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002281 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002282
Chris Lattner8f697062008-01-25 18:59:06 +00002283 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002284
Steve Naroffe61bfa82007-10-05 18:42:47 +00002285 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002286 if (nKeys == 0)
2287 KeyIdents.push_back(selIdent);
2288 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002289
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002290 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002291 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002292 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002293 MultiExprArg(Actions,
2294 KeyExprs.take(),
2295 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002296 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002297 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002298 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002299 MultiExprArg(Actions,
2300 KeyExprs.take(),
2301 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002302 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002303 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002304 MultiExprArg(Actions,
2305 KeyExprs.take(),
2306 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002307}
2308
John McCalldadc5752010-08-24 06:29:42 +00002309ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2310 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002311 if (Res.isInvalid()) return move(Res);
2312
Chris Lattnere002fbe2007-12-12 01:04:12 +00002313 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2314 // expressions. At this point, we know that the only valid thing that starts
2315 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002316 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002317 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002318 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002319 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002320
Chris Lattnere002fbe2007-12-12 01:04:12 +00002321 while (Tok.is(tok::at)) {
2322 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002323
Sebastian Redlc13f2682008-12-09 20:22:58 +00002324 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002325 if (!isTokenStringLiteral())
2326 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002327
John McCalldadc5752010-08-24 06:29:42 +00002328 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002329 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002330 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002331
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002332 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002333 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002334
2335 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2336 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002337}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002338
2339/// objc-encode-expression:
2340/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002341ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002342Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002343 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002344
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002345 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002346
Chris Lattner197a3012008-08-05 06:19:09 +00002347 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002348 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2349
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002350 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002351
Douglas Gregor220cac52009-02-18 17:45:20 +00002352 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002353
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002354 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002355
Douglas Gregor220cac52009-02-18 17:45:20 +00002356 if (Ty.isInvalid())
2357 return ExprError();
2358
Mike Stump11289f42009-09-09 15:08:12 +00002359 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002360 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002361}
Anders Carlssone01493d2007-08-23 15:25:28 +00002362
2363/// objc-protocol-expression
2364/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002365ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002366Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002367 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002368
Chris Lattner197a3012008-08-05 06:19:09 +00002369 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002370 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2371
Anders Carlssone01493d2007-08-23 15:25:28 +00002372 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002373
Chris Lattner197a3012008-08-05 06:19:09 +00002374 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002375 return ExprError(Diag(Tok, diag::err_expected_ident));
2376
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002377 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002378 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002379
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002380 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002381
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002382 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2383 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002384}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002385
2386/// objc-selector-expression
2387/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002388ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002389 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002390
Chris Lattner197a3012008-08-05 06:19:09 +00002391 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002392 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2393
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002394 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002395 SourceLocation LParenLoc = ConsumeParen();
2396 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002397
2398 if (Tok.is(tok::code_completion)) {
2399 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2400 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002401 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002402 return ExprError();
2403 }
2404
Chris Lattner4f472a32009-04-11 18:13:45 +00002405 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002406 if (!SelIdent && // missing selector name.
2407 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002408 return ExprError(Diag(Tok, diag::err_expected_ident));
2409
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002410 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002411 unsigned nColons = 0;
2412 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002413 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002414 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2415 ++nColons;
2416 KeyIdents.push_back(0);
2417 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002418 return ExprError(Diag(Tok, diag::err_expected_colon));
2419
Chris Lattner1ba64452010-08-27 22:32:41 +00002420 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002421 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002422 if (Tok.is(tok::r_paren))
2423 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002424
2425 if (Tok.is(tok::code_completion)) {
2426 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2427 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002428 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002429 return ExprError();
2430 }
2431
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002432 // Check for another keyword selector.
2433 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002434 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002435 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002436 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002437 break;
2438 }
Steve Naroff152dd812007-12-05 22:21:29 +00002439 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002440 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002441 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002442 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2443 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002444 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002445
2446Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2447
2448 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2449 // Append the current token at the end of the new token stream so that it
2450 // doesn't get lost.
2451 LM.Toks.push_back(Tok);
2452 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2453
2454 // MDecl might be null due to error in method prototype, etc.
2455 Decl *MDecl = LM.D;
2456 // Consume the previously pushed token.
2457 ConsumeAnyToken();
2458
2459 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2460 SourceLocation BraceLoc = Tok.getLocation();
2461 // Enter a scope for the method body.
2462 ParseScope BodyScope(this,
2463 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2464
2465 // Tell the actions module that we have entered a method definition with the
2466 // specified Declarator for the method.
2467 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2468
2469 if (PP.isCodeCompletionEnabled()) {
2470 if (trySkippingFunctionBodyForCodeCompletion()) {
2471 BodyScope.Exit();
2472 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2473 }
2474 }
2475
2476 StmtResult FnBody(ParseCompoundStatementBody());
2477
2478 // If the function body could not be parsed, make a bogus compoundstmt.
2479 if (FnBody.isInvalid())
2480 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2481 MultiStmtArg(Actions), false);
2482
2483 // Leave the function body scope.
2484 BodyScope.Exit();
2485
2486 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2487}