blob: 10c74ffdfdbed1d84e102d0cc3d4039cfa00bd19 [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.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000167
168 BalancedDelimiterTracker T(*this, tok::l_paren);
169 T.consumeOpen();
170
171 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000172 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000173 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000174 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000175 cutOffParsing();
176 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000177 }
178
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000179 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000180 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000181 categoryId = Tok.getIdentifierInfo();
182 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000183 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000184 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000185 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000186 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000187 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000188
189 T.consumeClose();
190 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000191 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000192
193 if (!attrs.empty()) { // categories don't support attributes.
194 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
195 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000196 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000197
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000198 // Next, we need to check for any protocol references.
199 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000200 SmallVector<Decl *, 8> ProtocolRefs;
201 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000202 if (Tok.is(tok::less) &&
203 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000204 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000205 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000206
John McCall48871652010-08-21 09:40:31 +0000207 Decl *CategoryType =
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000208 Actions.ActOnStartCategoryInterface(atLoc,
209 nameId, nameLoc,
210 categoryId, categoryLoc,
211 ProtocolRefs.data(),
212 ProtocolRefs.size(),
213 ProtocolLocs.data(),
214 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000215
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000216 if (Tok.is(tok::l_brace))
217 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
218
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000219 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000220 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 }
222 // Parse a class interface.
223 IdentifierInfo *superClassId = 0;
224 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000225
Chris Lattner0ef13522007-10-09 17:51:17 +0000226 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000227 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000228
229 // Code completion of superclass names.
230 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000231 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000232 cutOffParsing();
233 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000234 }
235
Chris Lattner0ef13522007-10-09 17:51:17 +0000236 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000237 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000238 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000239 }
240 superClassId = Tok.getIdentifierInfo();
241 superClassLoc = ConsumeToken();
242 }
243 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000244 SmallVector<Decl *, 8> ProtocolRefs;
245 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000246 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000247 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000248 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
249 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000250 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000251
John McCall48871652010-08-21 09:40:31 +0000252 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000253 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000254 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000255 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000256 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000257 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000258
Chris Lattner0ef13522007-10-09 17:51:17 +0000259 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000260 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000261
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000262 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000263 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000264}
265
John McCall064d77b2009-12-03 22:31:13 +0000266/// The Objective-C property callback. This should be defined where
267/// it's used, but instead it's been lifted to here to support VS2005.
268struct Parser::ObjCPropertyCallback : FieldCallback {
269 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000270 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000271 ObjCDeclSpec &OCDS;
272 SourceLocation AtLoc;
273 tok::ObjCKeywordKind MethodImplKind;
274
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000275 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000276 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000277 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
278 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000279 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCall064d77b2009-12-03 22:31:13 +0000280 MethodImplKind(MethodImplKind) {
281 }
282
John McCall48871652010-08-21 09:40:31 +0000283 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000284 if (FD.D.getIdentifier() == 0) {
285 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
286 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000287 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000288 }
289 if (FD.BitfieldSize) {
290 P.Diag(AtLoc, diag::err_objc_property_bitfield)
291 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000292 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000293 }
294
295 // Install the property declarator into interfaceDecl.
296 IdentifierInfo *SelName =
297 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
298
299 Selector GetterSel =
300 P.PP.getSelectorTable().getNullarySelector(SelName);
301 IdentifierInfo *SetterName = OCDS.getSetterName();
302 Selector SetterSel;
303 if (SetterName)
304 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
305 else
306 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
307 P.PP.getSelectorTable(),
308 FD.D.getIdentifier());
309 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000310 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000311 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000312 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000313 &isOverridingProperty,
314 MethodImplKind);
315 if (!isOverridingProperty)
316 Props.push_back(Property);
317
318 return Property;
319 }
320};
321
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000322/// objc-interface-decl-list:
323/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000324/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000325/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000326/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000327/// objc-interface-decl-list declaration
328/// objc-interface-decl-list ';'
329///
Steve Naroff99264b42007-08-22 16:35:03 +0000330/// objc-method-requirement: [OBJC2]
331/// @required
332/// @optional
333///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000334void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
335 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000336 SmallVector<Decl *, 32> allMethods;
337 SmallVector<Decl *, 16> allProperties;
338 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000339 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenekc7c64312010-01-07 01:20:12 +0000341 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000342
Steve Naroff99264b42007-08-22 16:35:03 +0000343 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000344 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000345 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000346 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000347 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000348 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000349 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
350 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000351 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
352 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000353 continue;
354 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000355 if (Tok.is(tok::l_paren)) {
356 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000357 ParseObjCMethodDecl(Tok.getLocation(),
358 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000359 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000360 continue;
361 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000362 // Ignore excess semicolons.
363 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000364 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000365 continue;
366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnerda9fb152008-10-20 06:10:06 +0000368 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000369 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000370 break;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Douglas Gregorf1934162010-01-13 21:24:21 +0000372 // Code completion within an Objective-C interface.
373 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000374 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000375 ObjCImpDecl? Sema::PCC_ObjCImplementation
376 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000377 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000378 }
379
Chris Lattner038a3e32008-10-20 05:46:22 +0000380 // If we don't have an @ directive, parse it as a function definition.
381 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000382 // The code below does not consume '}'s because it is afraid of eating the
383 // end of a namespace. Because of the way this code is structured, an
384 // erroneous r_brace would cause an infinite loop if not handled here.
385 if (Tok.is(tok::r_brace))
386 break;
John McCall084e83d2011-03-24 11:26:52 +0000387 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000388 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000389 continue;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattner038a3e32008-10-20 05:46:22 +0000392 // Otherwise, we have an @ directive, eat the @.
393 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000394 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000395 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000396 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000397 break;
398 }
399
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000400 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000402 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000403 AtEnd.setBegin(AtLoc);
404 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000405 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000406 } else if (DirectiveKind == tok::objc_not_keyword) {
407 Diag(Tok, diag::err_objc_unknown_at);
408 SkipUntil(tok::semi);
409 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattnerda9fb152008-10-20 06:10:06 +0000412 // Eat the identifier.
413 ConsumeToken();
414
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000415 switch (DirectiveKind) {
416 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000417 // FIXME: If someone forgets an @end on a protocol, this loop will
418 // continue to eat up tons of stuff and spew lots of nonsense errors. It
419 // would probably be better to bail out if we saw an @class or @interface
420 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000421 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000422 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000423 SkipUntil(tok::r_brace, tok::at);
424 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000425
426 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000427 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000428 Diag(Tok, diag::err_objc_missing_end);
429 ConsumeToken();
430 break;
431
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000432 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000433 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000434 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000435 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000436 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000437 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000438 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000439 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000440 break;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000442 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000443 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000444 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000445
Chris Lattner038a3e32008-10-20 05:46:22 +0000446 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000447 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000448 if (Tok.is(tok::l_paren))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000449 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000450
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000451 ObjCPropertyCallback Callback(*this, allProperties,
John McCall064d77b2009-12-03 22:31:13 +0000452 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000453
Chris Lattner038a3e32008-10-20 05:46:22 +0000454 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000455 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000456 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000457
John McCall405988b2011-03-26 01:53:26 +0000458 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000460 }
Steve Naroff99264b42007-08-22 16:35:03 +0000461 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000462
463 // We break out of the big loop in two cases: when we see @end or when we see
464 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000465 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000466 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000467 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000468 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000469 ConsumeToken(); // the "end" identifier
470 else
471 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000473 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000474 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000475 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000476 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000477 allProperties.data(), allProperties.size(),
478 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000479}
480
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000481/// Parse property attribute declarations.
482///
483/// property-attr-decl: '(' property-attrlist ')'
484/// property-attrlist:
485/// property-attribute
486/// property-attrlist ',' property-attribute
487/// property-attribute:
488/// getter '=' identifier
489/// setter '=' identifier ':'
490/// readonly
491/// readwrite
492/// assign
493/// retain
494/// copy
495/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000496/// atomic
497/// strong
498/// weak
499/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000500///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000501void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000502 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000503 BalancedDelimiterTracker T(*this, tok::l_paren);
504 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000506 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000507 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000508 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000509 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000510 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000511 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattner76619232008-10-20 07:22:18 +0000513 // If this is not an identifier at all, bail out early.
514 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000515 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000516 return;
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner1db33542008-10-20 07:37:22 +0000519 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000520
Chris Lattner68e48682008-11-20 04:42:34 +0000521 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000523 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000525 else if (II->isStr("unsafe_unretained"))
526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000527 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000528 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000529 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000530 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000531 else if (II->isStr("strong"))
532 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000533 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000534 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000535 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000537 else if (II->isStr("atomic"))
538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000539 else if (II->isStr("weak"))
540 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000541 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000542 bool IsSetter = II->getNameStart()[0] == 's';
543
Chris Lattner825bca12008-10-20 07:39:53 +0000544 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000545 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
546 diag::err_objc_expected_equal_for_getter;
547
548 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000549 return;
Mike Stump11289f42009-09-09 15:08:12 +0000550
Douglas Gregorc8537c52009-11-19 07:41:15 +0000551 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000552 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000553 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000554 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000555 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000556 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000557 }
558
Anders Carlssonfe15a782010-10-02 17:45:21 +0000559
560 SourceLocation SelLoc;
561 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
562
563 if (!SelIdent) {
564 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
565 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000566 SkipUntil(tok::r_paren);
567 return;
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Anders Carlssonfe15a782010-10-02 17:45:21 +0000570 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000572 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000573
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000574 if (ExpectAndConsume(tok::colon,
575 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000576 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000577 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000578 } else {
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000580 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000581 }
Chris Lattner825bca12008-10-20 07:39:53 +0000582 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000583 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000584 SkipUntil(tok::r_paren);
585 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner1db33542008-10-20 07:37:22 +0000588 if (Tok.isNot(tok::comma))
589 break;
Mike Stump11289f42009-09-09 15:08:12 +0000590
Chris Lattner1db33542008-10-20 07:37:22 +0000591 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000592 }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000594 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000595}
596
Steve Naroff09bf8152007-09-06 21:24:23 +0000597/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000598/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000599/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000600///
601/// objc-instance-method: '-'
602/// objc-class-method: '+'
603///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000604/// objc-method-attributes: [OBJC2]
605/// __attribute__((deprecated))
606///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000607Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000608 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000609 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000610
Mike Stump11289f42009-09-09 15:08:12 +0000611 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000612 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000613 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000614 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000615 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000616 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000617 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000618}
619
620/// objc-selector:
621/// identifier
622/// one of
623/// enum struct union if else while do for switch case default
624/// break continue return goto asm sizeof typeof __alignof
625/// unsigned long const short volatile signed restrict _Complex
626/// in out inout bycopy byref oneway int char float double void _Bool
627///
Chris Lattner4f472a32009-04-11 18:13:45 +0000628IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000629
Chris Lattner5700fab2007-10-07 02:00:24 +0000630 switch (Tok.getKind()) {
631 default:
632 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000633 case tok::ampamp:
634 case tok::ampequal:
635 case tok::amp:
636 case tok::pipe:
637 case tok::tilde:
638 case tok::exclaim:
639 case tok::exclaimequal:
640 case tok::pipepipe:
641 case tok::pipeequal:
642 case tok::caret:
643 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000644 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000645 if (isalpha(ThisTok[0])) {
646 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
647 Tok.setKind(tok::identifier);
648 SelectorLoc = ConsumeToken();
649 return II;
650 }
651 return 0;
652 }
653
Chris Lattner5700fab2007-10-07 02:00:24 +0000654 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000655 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000656 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000657 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000658 case tok::kw_break:
659 case tok::kw_case:
660 case tok::kw_catch:
661 case tok::kw_char:
662 case tok::kw_class:
663 case tok::kw_const:
664 case tok::kw_const_cast:
665 case tok::kw_continue:
666 case tok::kw_default:
667 case tok::kw_delete:
668 case tok::kw_do:
669 case tok::kw_double:
670 case tok::kw_dynamic_cast:
671 case tok::kw_else:
672 case tok::kw_enum:
673 case tok::kw_explicit:
674 case tok::kw_export:
675 case tok::kw_extern:
676 case tok::kw_false:
677 case tok::kw_float:
678 case tok::kw_for:
679 case tok::kw_friend:
680 case tok::kw_goto:
681 case tok::kw_if:
682 case tok::kw_inline:
683 case tok::kw_int:
684 case tok::kw_long:
685 case tok::kw_mutable:
686 case tok::kw_namespace:
687 case tok::kw_new:
688 case tok::kw_operator:
689 case tok::kw_private:
690 case tok::kw_protected:
691 case tok::kw_public:
692 case tok::kw_register:
693 case tok::kw_reinterpret_cast:
694 case tok::kw_restrict:
695 case tok::kw_return:
696 case tok::kw_short:
697 case tok::kw_signed:
698 case tok::kw_sizeof:
699 case tok::kw_static:
700 case tok::kw_static_cast:
701 case tok::kw_struct:
702 case tok::kw_switch:
703 case tok::kw_template:
704 case tok::kw_this:
705 case tok::kw_throw:
706 case tok::kw_true:
707 case tok::kw_try:
708 case tok::kw_typedef:
709 case tok::kw_typeid:
710 case tok::kw_typename:
711 case tok::kw_typeof:
712 case tok::kw_union:
713 case tok::kw_unsigned:
714 case tok::kw_using:
715 case tok::kw_virtual:
716 case tok::kw_void:
717 case tok::kw_volatile:
718 case tok::kw_wchar_t:
719 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000720 case tok::kw__Bool:
721 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000722 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000723 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000724 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000725 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000726 }
Steve Naroff99264b42007-08-22 16:35:03 +0000727}
728
Fariborz Jahanian83615522008-01-02 22:54:34 +0000729/// objc-for-collection-in: 'in'
730///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000731bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000732 // FIXME: May have to do additional look-ahead to only allow for
733 // valid tokens following an 'in'; such as an identifier, unary operators,
734 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000735 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000736 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000737}
738
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000739/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000740/// qualifier list and builds their bitmask representation in the input
741/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000742///
743/// objc-type-qualifiers:
744/// objc-type-qualifier
745/// objc-type-qualifiers objc-type-qualifier
746///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000747void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000748 Declarator::TheContext Context) {
749 assert(Context == Declarator::ObjCParameterContext ||
750 Context == Declarator::ObjCResultContext);
751
Chris Lattner61511e12007-12-12 06:56:32 +0000752 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000753 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000754 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000755 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000756 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000757 }
758
Chris Lattner5e530bc2007-12-27 19:57:00 +0000759 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000760 return;
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattner61511e12007-12-12 06:56:32 +0000762 const IdentifierInfo *II = Tok.getIdentifierInfo();
763 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000764 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000765 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000766
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000767 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000768 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000769 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000770 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
771 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
772 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
773 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
774 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
775 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000776 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000777 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000778 ConsumeToken();
779 II = 0;
780 break;
781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner61511e12007-12-12 06:56:32 +0000783 // If this wasn't a recognized qualifier, bail out.
784 if (II) return;
785 }
786}
787
John McCalla55902b2011-10-01 09:56:14 +0000788/// Take all the decl attributes out of the given list and add
789/// them to the given attribute set.
790static void takeDeclAttributes(ParsedAttributes &attrs,
791 AttributeList *list) {
792 while (list) {
793 AttributeList *cur = list;
794 list = cur->getNext();
795
796 if (!cur->isUsedAsTypeAttr()) {
797 // Clear out the next pointer. We're really completely
798 // destroying the internal invariants of the declarator here,
799 // but it doesn't matter because we're done with it.
800 cur->setNext(0);
801 attrs.add(cur);
802 }
803 }
804}
805
806/// takeDeclAttributes - Take all the decl attributes from the given
807/// declarator and add them to the given list.
808static void takeDeclAttributes(ParsedAttributes &attrs,
809 Declarator &D) {
810 // First, take ownership of all attributes.
811 attrs.getPool().takeAllFrom(D.getAttributePool());
812 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
813
814 // Now actually move the attributes over.
815 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
816 takeDeclAttributes(attrs, D.getAttributes());
817 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
818 takeDeclAttributes(attrs,
819 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
820}
821
Chris Lattner61511e12007-12-12 06:56:32 +0000822/// objc-type-name:
823/// '(' objc-type-qualifiers[opt] type-name ')'
824/// '(' objc-type-qualifiers[opt] ')'
825///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000826ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000827 Declarator::TheContext context,
828 ParsedAttributes *paramAttrs) {
829 assert(context == Declarator::ObjCParameterContext ||
830 context == Declarator::ObjCResultContext);
831 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
832
Chris Lattner0ef13522007-10-09 17:51:17 +0000833 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000834
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000835 BalancedDelimiterTracker T(*this, tok::l_paren);
836 T.consumeOpen();
837
Chris Lattner2ebb1782008-08-23 01:48:03 +0000838 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000839 ObjCDeclContextSwitch ObjCDC(*this);
840
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000841 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000842 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000843
John McCallba7bf592010-08-24 05:47:05 +0000844 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000845 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000846 // Parse an abstract declarator.
847 DeclSpec declSpec(AttrFactory);
848 declSpec.setObjCQualifiers(&DS);
849 ParseSpecifierQualifierList(declSpec);
850 Declarator declarator(declSpec, context);
851 ParseDeclarator(declarator);
852
853 // If that's not invalid, extract a type.
854 if (!declarator.isInvalidType()) {
855 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
856 if (!type.isInvalid())
857 Ty = type.get();
858
859 // If we're parsing a parameter, steal all the decl attributes
860 // and add them to the decl spec.
861 if (context == Declarator::ObjCParameterContext)
862 takeDeclAttributes(*paramAttrs, declarator);
863 }
864 } else if (context == Declarator::ObjCResultContext &&
865 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000866 if (!Ident_instancetype)
867 Ident_instancetype = PP.getIdentifierInfo("instancetype");
868
869 if (Tok.getIdentifierInfo() == Ident_instancetype) {
870 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
871 ConsumeToken();
872 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000873 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000874
Steve Naroff90255b42008-10-21 14:15:04 +0000875 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000876 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000877 else if (Tok.getLocation() == TypeStartLoc) {
878 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000879 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000880 SkipUntil(tok::r_paren);
881 } else {
882 // Otherwise, we found *something*, but didn't get a ')' in the right
883 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000884 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000885 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000886 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000887}
888
889/// objc-method-decl:
890/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000891/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000892/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000893/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000894///
895/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000896/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000897/// objc-keyword-selector objc-keyword-decl
898///
899/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000900/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
901/// objc-selector ':' objc-keyword-attributes[opt] identifier
902/// ':' objc-type-name objc-keyword-attributes[opt] identifier
903/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000904///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000905/// objc-parmlist:
906/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000907///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000908/// objc-parms:
909/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000910///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000911/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000912/// , ...
913///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000914/// objc-keyword-attributes: [OBJC2]
915/// __attribute__((unused))
916///
John McCall48871652010-08-21 09:40:31 +0000917Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000918 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000919 tok::ObjCKeywordKind MethodImplKind,
920 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000921 ParsingDeclRAIIObject PD(*this);
922
Douglas Gregor636a61e2010-04-07 00:21:17 +0000923 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000924 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000925 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000926 cutOffParsing();
927 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000928 }
929
Chris Lattner2ebb1782008-08-23 01:48:03 +0000930 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000931 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000932 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000933 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000934 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000935
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000936 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000937 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000938 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000939 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000940
Douglas Gregor636a61e2010-04-07 00:21:17 +0000941 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000942 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000943 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000944 cutOffParsing();
945 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000946 }
947
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000948 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000949 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000950 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000951
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000952 // An unnamed colon is valid.
953 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000954 Diag(Tok, diag::err_expected_selector_for_method)
955 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000956 // Skip until we get a ; or {}.
957 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000958 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000959 }
Mike Stump11289f42009-09-09 15:08:12 +0000960
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000961 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000963 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000964 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000965 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattner5700fab2007-10-07 02:00:24 +0000967 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000968 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000969 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000970 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +0000971 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000972 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +0000973 methodAttrs.getList(), MethodImplKind,
974 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +0000975 PD.complete(Result);
976 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000977 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000978
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000979 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +0000980 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000981 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000982 ParseScope PrototypeScope(this,
983 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +0000984
985 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000986
Chris Lattner5700fab2007-10-07 02:00:24 +0000987 while (1) {
John McCall084e83d2011-03-24 11:26:52 +0000988 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +0000989 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000990
Chris Lattner5700fab2007-10-07 02:00:24 +0000991 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000992 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000993 Diag(Tok, diag::err_expected_colon);
994 break;
995 }
996 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000997
John McCallba7bf592010-08-24 05:47:05 +0000998 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000999 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001000 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1001 Declarator::ObjCParameterContext,
1002 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001003
Chris Lattner5700fab2007-10-07 02:00:24 +00001004 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001005 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001006 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +00001007 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001008 MaybeParseGNUAttributes(paramAttrs);
1009 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001010 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001011
Douglas Gregor45879692010-07-08 23:37:41 +00001012 // Code completion for the next piece of the selector.
1013 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001014 KeyIdents.push_back(SelIdent);
1015 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1016 mType == tok::minus,
1017 /*AtParameterName=*/true,
1018 ReturnType,
1019 KeyIdents.data(),
1020 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001021 cutOffParsing();
1022 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001023 }
1024
Chris Lattner0ef13522007-10-09 17:51:17 +00001025 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001026 Diag(Tok, diag::err_expected_ident); // missing argument name.
1027 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001030 ArgInfo.Name = Tok.getIdentifierInfo();
1031 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001032 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001033
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001034 ArgInfos.push_back(ArgInfo);
1035 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001036 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001037
John McCall084e83d2011-03-24 11:26:52 +00001038 // Make sure the attributes persist.
1039 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1040
Douglas Gregor95887f92010-07-08 23:20:03 +00001041 // Code completion for the next piece of the selector.
1042 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001043 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1044 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001045 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +00001046 ReturnType,
1047 KeyIdents.data(),
1048 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001049 cutOffParsing();
1050 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001051 }
1052
Chris Lattner5700fab2007-10-07 02:00:24 +00001053 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001054 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001055 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001056 break;
1057 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001060 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +00001061
Chris Lattner5700fab2007-10-07 02:00:24 +00001062 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001063 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001064 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001065 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001066 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001067 ConsumeToken();
1068 break;
1069 }
John McCall084e83d2011-03-24 11:26:52 +00001070 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001071 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001072 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001073 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1074 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001075 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001076 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001077 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1078 ParmDecl.getIdentifierLoc(),
1079 Param,
1080 0));
1081
Chris Lattner5700fab2007-10-07 02:00:24 +00001082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001084 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001085 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001086 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001087 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001088
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001089 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001090 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001091
Chris Lattner5700fab2007-10-07 02:00:24 +00001092 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1093 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001094 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001095 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001096 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001097 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001098 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001099 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001100 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001101
John McCall28a6aea2009-11-04 02:18:39 +00001102 PD.complete(Result);
1103 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001104}
1105
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001106/// objc-protocol-refs:
1107/// '<' identifier-list '>'
1108///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001109bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001110ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1111 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001112 bool WarnOnDeclarations,
1113 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001114 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001115
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001116 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001118 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattner3bbae002008-07-26 04:03:38 +00001120 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001121 if (Tok.is(tok::code_completion)) {
1122 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1123 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001124 cutOffParsing();
1125 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001126 }
1127
Chris Lattner3bbae002008-07-26 04:03:38 +00001128 if (Tok.isNot(tok::identifier)) {
1129 Diag(Tok, diag::err_expected_ident);
1130 SkipUntil(tok::greater);
1131 return true;
1132 }
1133 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1134 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001135 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001136 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001137
Chris Lattner3bbae002008-07-26 04:03:38 +00001138 if (Tok.isNot(tok::comma))
1139 break;
1140 ConsumeToken();
1141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
Chris Lattner3bbae002008-07-26 04:03:38 +00001143 // Consume the '>'.
1144 if (Tok.isNot(tok::greater)) {
1145 Diag(Tok, diag::err_expected_greater);
1146 return true;
1147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattner3bbae002008-07-26 04:03:38 +00001149 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattner3bbae002008-07-26 04:03:38 +00001151 // Convert the list of protocols identifiers into a list of protocol decls.
1152 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1153 &ProtocolIdents[0], ProtocolIdents.size(),
1154 Protocols);
1155 return false;
1156}
1157
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001158/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1159/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001160bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001161 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1162 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1163 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 SmallVector<Decl *, 8> ProtocolDecl;
1165 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001166 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1167 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001168 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1169 ProtocolLocs.data(), LAngleLoc);
1170 if (EndProtoLoc.isValid())
1171 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001172 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001173}
1174
1175
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001176/// objc-class-instance-variables:
1177/// '{' objc-instance-variable-decl-list[opt] '}'
1178///
1179/// objc-instance-variable-decl-list:
1180/// objc-visibility-spec
1181/// objc-instance-variable-decl ';'
1182/// ';'
1183/// objc-instance-variable-decl-list objc-visibility-spec
1184/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1185/// objc-instance-variable-decl-list ';'
1186///
1187/// objc-visibility-spec:
1188/// @private
1189/// @protected
1190/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001191/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001192///
1193/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001194/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001195///
John McCall48871652010-08-21 09:40:31 +00001196void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001197 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001198 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001199 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001200 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001201
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001202 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001203 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001204
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001205 BalancedDelimiterTracker T(*this, tok::l_brace);
1206 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001207
Steve Naroff00433d32007-08-21 21:17:12 +00001208 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001209 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001210 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001211
Steve Naroff00433d32007-08-21 21:17:12 +00001212 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001213 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001214 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001215 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001216 ConsumeToken();
1217 continue;
1218 }
Mike Stump11289f42009-09-09 15:08:12 +00001219
Steve Naroff00433d32007-08-21 21:17:12 +00001220 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001221 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001222 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001223
1224 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001225 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001226 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001227 }
1228
Steve Naroff7c348172007-08-23 18:16:40 +00001229 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001230 case tok::objc_private:
1231 case tok::objc_public:
1232 case tok::objc_protected:
1233 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001234 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001235 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001236 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001237 default:
1238 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001239 continue;
1240 }
1241 }
Mike Stump11289f42009-09-09 15:08:12 +00001242
Douglas Gregor48d46252010-01-13 21:54:15 +00001243 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001244 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001245 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001246 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001247 }
1248
John McCallcfefb6d2009-11-03 02:38:08 +00001249 struct ObjCIvarCallback : FieldCallback {
1250 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001251 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001252 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001253 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001254
John McCall48871652010-08-21 09:40:31 +00001255 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001256 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001257 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1258 }
1259
John McCall48871652010-08-21 09:40:31 +00001260 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001261 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001262 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001263 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001264 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001265 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001266 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001267 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001268 if (Field)
1269 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001270 return Field;
1271 }
1272 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001273
Chris Lattnera12405b2008-04-10 06:46:29 +00001274 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001275 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001276 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattner0ef13522007-10-09 17:51:17 +00001278 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001279 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001280 } else {
1281 Diag(Tok, diag::err_expected_semi_decl_list);
1282 // Skip to end of block or statement
1283 SkipUntil(tok::r_brace, true, true);
1284 }
1285 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001286 T.consumeClose();
1287
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001288 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001289 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001290 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001291 // Call ActOnFields() even if we don't have any decls. This is useful
1292 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001293 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie751c5582011-09-22 02:58:26 +00001294 AllIvarDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001295 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001296 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001297}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001298
1299/// objc-protocol-declaration:
1300/// objc-protocol-definition
1301/// objc-protocol-forward-reference
1302///
1303/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001304/// @protocol identifier
1305/// objc-protocol-refs[opt]
1306/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001307/// @end
1308///
1309/// objc-protocol-forward-reference:
1310/// @protocol identifier-list ';'
1311///
1312/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001313/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001314/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001315Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001316 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001317 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001318 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1319 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001320
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001321 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001322 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001323 cutOffParsing();
1324 return 0;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001325 }
1326
Chris Lattner0ef13522007-10-09 17:51:17 +00001327 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001328 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001329 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001330 }
1331 // Save the protocol name, then consume it.
1332 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1333 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001334
Chris Lattner0ef13522007-10-09 17:51:17 +00001335 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001336 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001337 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001338 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001339 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001340 }
Mike Stump11289f42009-09-09 15:08:12 +00001341
Chris Lattner0ef13522007-10-09 17:51:17 +00001342 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001343 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001344 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1345
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001346 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001347 while (1) {
1348 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001349 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001350 Diag(Tok, diag::err_expected_ident);
1351 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001352 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001353 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001354 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1355 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001356 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001357
Chris Lattner0ef13522007-10-09 17:51:17 +00001358 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001359 break;
1360 }
1361 // Consume the ';'.
1362 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001363 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001364
Steve Naroff93eb5f12007-10-10 17:32:04 +00001365 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001366 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001367 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001368 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001369 }
Mike Stump11289f42009-09-09 15:08:12 +00001370
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001371 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001372 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001373
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001374 SmallVector<Decl *, 8> ProtocolRefs;
1375 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001376 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001377 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1378 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001379 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001380
John McCall48871652010-08-21 09:40:31 +00001381 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001382 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001383 ProtocolRefs.data(),
1384 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001385 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001386 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001387
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001388 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001389 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001390}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001391
1392/// objc-implementation:
1393/// objc-class-implementation-prologue
1394/// objc-category-implementation-prologue
1395///
1396/// objc-class-implementation-prologue:
1397/// @implementation identifier objc-superclass[opt]
1398/// objc-class-instance-variables[opt]
1399///
1400/// objc-category-implementation-prologue:
1401/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001402Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001403 SourceLocation atLoc) {
1404 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1405 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1406 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001407
Douglas Gregor49c22a72009-11-18 16:26:39 +00001408 // Code completion after '@implementation'.
1409 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001410 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001411 cutOffParsing();
1412 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +00001413 }
1414
Chris Lattner0ef13522007-10-09 17:51:17 +00001415 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001416 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001417 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001418 }
1419 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001420 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001421 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001422
1423 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001424 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001425 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001426 SourceLocation categoryLoc, rparenLoc;
1427 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001429 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001430 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001431 cutOffParsing();
1432 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001433 }
1434
Chris Lattner0ef13522007-10-09 17:51:17 +00001435 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001436 categoryId = Tok.getIdentifierInfo();
1437 categoryLoc = ConsumeToken();
1438 } else {
1439 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001440 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001441 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001442 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001443 Diag(Tok, diag::err_expected_rparen);
1444 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001445 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001446 }
1447 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001448 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001449 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001450 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001451
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001452 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001453 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001454 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001455 }
1456 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001457 SourceLocation superClassLoc;
1458 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001459 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001460 // We have a super class
1461 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001462 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001463 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001464 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001465 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001466 superClassId = Tok.getIdentifierInfo();
1467 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001468 }
John McCall48871652010-08-21 09:40:31 +00001469 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001470 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001471 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001472
Steve Naroff33a1e802007-10-29 21:38:07 +00001473 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001474 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1475
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001476 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001477 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001478 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001479}
Steve Naroff33a1e802007-10-29 21:38:07 +00001480
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001481Parser::DeclGroupPtrTy
1482Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001483 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1484 "ParseObjCAtEndDeclaration(): Expected @end");
1485 ConsumeToken(); // the "end" identifier
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001486 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001487 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001488 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1489 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis516eec22011-11-16 02:35:10 +00001490 if (D)
1491 DeclsInGroup.push_back(D);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001492 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001493 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001494
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001495 if (ObjCImpDecl) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001496 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001497 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001498 }
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001499 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001500 // missing @implementation
Fariborz Jahanianc0577942011-04-22 22:02:28 +00001501 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001502
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001503 clearLateParsedObjCMethods();
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001504 ObjCImpDecl = 0;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001505 return Actions.BuildDeclaratorGroup(
1506 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001507}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001508
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001509Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1510 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001511 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001512 return Actions.ConvertDeclToDeclGroup(0);
1513 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001514 Actions.ActOnAtEnd(getCurScope(), SourceRange());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001515 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1516}
1517
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001518void Parser::clearLateParsedObjCMethods() {
1519 for (LateParsedObjCMethodContainer::iterator
1520 I = LateParsedObjCMethods.begin(),
1521 E = LateParsedObjCMethods.end(); I != E; ++I)
1522 delete *I;
1523 LateParsedObjCMethods.clear();
1524}
1525
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001526/// compatibility-alias-decl:
1527/// @compatibility_alias alias-name class-name ';'
1528///
John McCall48871652010-08-21 09:40:31 +00001529Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001530 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1531 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1532 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001533 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001534 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001535 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001536 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001537 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1538 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001539 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001540 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001541 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001542 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001543 IdentifierInfo *classId = Tok.getIdentifierInfo();
1544 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001545 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1546 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001547 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1548 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001549}
1550
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001551/// property-synthesis:
1552/// @synthesize property-ivar-list ';'
1553///
1554/// property-ivar-list:
1555/// property-ivar
1556/// property-ivar-list ',' property-ivar
1557///
1558/// property-ivar:
1559/// identifier
1560/// identifier '=' identifier
1561///
John McCall48871652010-08-21 09:40:31 +00001562Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001563 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1564 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001565 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001566
Douglas Gregor88e72a02009-11-18 19:45:45 +00001567 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001568 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001569 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001570 cutOffParsing();
1571 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001572 }
1573
Douglas Gregor88e72a02009-11-18 19:45:45 +00001574 if (Tok.isNot(tok::identifier)) {
1575 Diag(Tok, diag::err_synthesized_property_name);
1576 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001577 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001578 }
1579
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001580 IdentifierInfo *propertyIvar = 0;
1581 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1582 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001583 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001584 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001585 // property '=' ivar-name
1586 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001587
1588 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001589 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001590 cutOffParsing();
1591 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001592 }
1593
Chris Lattner0ef13522007-10-09 17:51:17 +00001594 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001595 Diag(Tok, diag::err_expected_ident);
1596 break;
1597 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001598 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001599 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001600 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001601 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001602 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001603 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001604 break;
1605 ConsumeToken(); // consume ','
1606 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001607 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001608 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001609}
1610
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001611/// property-dynamic:
1612/// @dynamic property-list
1613///
1614/// property-list:
1615/// identifier
1616/// property-list ',' identifier
1617///
John McCall48871652010-08-21 09:40:31 +00001618Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001619 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1620 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001621 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001622 while (true) {
1623 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001624 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001625 cutOffParsing();
1626 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001627 }
1628
1629 if (Tok.isNot(tok::identifier)) {
1630 Diag(Tok, diag::err_expected_ident);
1631 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001632 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001633 }
1634
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001635 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1636 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001637 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001638 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001639
Chris Lattner0ef13522007-10-09 17:51:17 +00001640 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001641 break;
1642 ConsumeToken(); // consume ','
1643 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001644 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001645 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001646}
Mike Stump11289f42009-09-09 15:08:12 +00001647
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001648/// objc-throw-statement:
1649/// throw expression[opt];
1650///
John McCalldadc5752010-08-24 06:29:42 +00001651StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1652 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001653 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001654 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001655 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001656 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001657 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001658 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001659 }
1660 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001661 // consume ';'
1662 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001663 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001664}
1665
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001666/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001667/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001668///
John McCalldadc5752010-08-24 06:29:42 +00001669StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001670Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001671 ConsumeToken(); // consume synchronized
1672 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001673 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001674 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001675 }
John McCalld9bb7432011-07-27 21:50:02 +00001676
1677 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001678 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001679 ExprResult operand(ParseExpression());
1680
1681 if (Tok.is(tok::r_paren)) {
1682 ConsumeParen(); // ')'
1683 } else {
1684 if (!operand.isInvalid())
1685 Diag(Tok, diag::err_expected_rparen);
1686
1687 // Skip forward until we see a left brace, but don't consume it.
1688 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001689 }
John McCalld9bb7432011-07-27 21:50:02 +00001690
1691 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001692 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001693 if (!operand.isInvalid())
1694 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001695 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001696 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001697
John McCalld9bb7432011-07-27 21:50:02 +00001698 // Check the @synchronized operand now.
1699 if (!operand.isInvalid())
1700 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001701
John McCalld9bb7432011-07-27 21:50:02 +00001702 // Parse the compound statement within a new scope.
1703 ParseScope bodyScope(this, Scope::DeclScope);
1704 StmtResult body(ParseCompoundStatementBody());
1705 bodyScope.Exit();
1706
1707 // If there was a semantic or parse error earlier with the
1708 // operand, fail now.
1709 if (operand.isInvalid())
1710 return StmtError();
1711
1712 if (body.isInvalid())
1713 body = Actions.ActOnNullStmt(Tok.getLocation());
1714
1715 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001716}
1717
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001718/// objc-try-catch-statement:
1719/// @try compound-statement objc-catch-list[opt]
1720/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1721///
1722/// objc-catch-list:
1723/// @catch ( parameter-declaration ) compound-statement
1724/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1725/// catch-parameter-declaration:
1726/// parameter-declaration
1727/// '...' [OBJC2]
1728///
John McCalldadc5752010-08-24 06:29:42 +00001729StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001730 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001731
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001732 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001733 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001734 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001735 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001736 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001737 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001738 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001739 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001740 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001741 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001742 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001743 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001744
Chris Lattner0ef13522007-10-09 17:51:17 +00001745 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001746 // At this point, we need to lookahead to determine if this @ is the start
1747 // of an @catch or @finally. We don't want to consume the @ token if this
1748 // is an @try or @encode or something else.
1749 Token AfterAt = GetLookAheadToken(1);
1750 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1751 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1752 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001753
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001754 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001755 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001756 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001757 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001758 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001759 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001760 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001761 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001762 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001763 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001764 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001765 ParseDeclarator(ParmDecl);
1766
Douglas Gregore11ee112010-04-23 23:01:43 +00001767 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001768 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001769 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001770 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001771 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001772
Steve Naroff65a00892009-04-07 22:56:58 +00001773 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001774
Steve Naroff65a00892009-04-07 22:56:58 +00001775 if (Tok.is(tok::r_paren))
1776 RParenLoc = ConsumeParen();
1777 else // Skip over garbage, until we get to ')'. Eat the ')'.
1778 SkipUntil(tok::r_paren, true, false);
1779
John McCalldadc5752010-08-24 06:29:42 +00001780 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001781 if (Tok.is(tok::l_brace))
1782 CatchBody = ParseCompoundStatementBody();
1783 else
1784 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001785 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001786 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001787
John McCalldadc5752010-08-24 06:29:42 +00001788 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001789 RParenLoc,
1790 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001791 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001792 if (!Catch.isInvalid())
1793 CatchStmts.push_back(Catch.release());
1794
Steve Naroffe6016792008-02-05 21:27:35 +00001795 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001796 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1797 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001798 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001799 }
1800 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001801 } else {
1802 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001803 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001804 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001805
John McCalldadc5752010-08-24 06:29:42 +00001806 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001807 if (Tok.is(tok::l_brace))
1808 FinallyBody = ParseCompoundStatementBody();
1809 else
1810 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001811 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001812 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001813 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001814 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001815 catch_or_finally_seen = true;
1816 break;
1817 }
1818 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001819 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001820 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001821 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001822 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001823
John McCallb268a282010-08-23 23:25:46 +00001824 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001825 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001826 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001827}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001828
John McCall31168b02011-06-15 23:02:42 +00001829/// objc-autoreleasepool-statement:
1830/// @autoreleasepool compound-statement
1831///
1832StmtResult
1833Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1834 ConsumeToken(); // consume autoreleasepool
1835 if (Tok.isNot(tok::l_brace)) {
1836 Diag(Tok, diag::err_expected_lbrace);
1837 return StmtError();
1838 }
1839 // Enter a scope to hold everything within the compound stmt. Compound
1840 // statements can always hold declarations.
1841 ParseScope BodyScope(this, Scope::DeclScope);
1842
1843 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1844
1845 BodyScope.Exit();
1846 if (AutoreleasePoolBody.isInvalid())
1847 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1848 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1849 AutoreleasePoolBody.take());
1850}
1851
Steve Naroff09bf8152007-09-06 21:24:23 +00001852/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001853///
John McCall48871652010-08-21 09:40:31 +00001854Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001855 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001856
John McCallfaf5fb42010-08-26 23:41:50 +00001857 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1858 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001859
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001860 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001861 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001862 if (ObjCImpDecl) {
1863 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001864 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001865 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001866 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001867 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001868
Steve Naroffbb875722007-11-11 19:54:21 +00001869 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001870 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001871 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001872
Steve Naroffbb875722007-11-11 19:54:21 +00001873 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1874 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001875
Steve Naroffbb875722007-11-11 19:54:21 +00001876 // If we didn't find the '{', bail out.
1877 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001878 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001879 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001880 // Allow the rest of sema to find private method decl implementations.
1881 if (MDecl)
1882 Actions.AddAnyMethodToGlobalPool(MDecl);
1883
1884 // Consume the tokens and store them for later parsing.
1885 LexedMethod* LM = new LexedMethod(this, MDecl);
1886 LateParsedObjCMethods.push_back(LM);
1887 CachedTokens &Toks = LM->Toks;
1888 // Begin by storing the '{' token.
1889 Toks.push_back(Tok);
1890 ConsumeBrace();
1891 // Consume everything up to (and including) the matching right brace.
1892 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff7b8fa472007-11-13 23:01:27 +00001893 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001894}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001895
John McCalldadc5752010-08-24 06:29:42 +00001896StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001897 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001899 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001900 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001901 }
1902
1903 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001904 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001905
1906 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001907 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001908
1909 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001910 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001911
1912 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1913 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001914
John McCalldadc5752010-08-24 06:29:42 +00001915 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001916 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001917 // If the expression is invalid, skip ahead to the next semicolon. Not
1918 // doing this opens us up to the possibility of infinite loops if
1919 // ParseExpression does not consume any tokens.
1920 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001921 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001922 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001923
Steve Naroffe6016792008-02-05 21:27:35 +00001924 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001925 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001926 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001927}
1928
John McCalldadc5752010-08-24 06:29:42 +00001929ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001930 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001931 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001932 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001933 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001934 return ExprError();
1935
Chris Lattnere002fbe2007-12-12 01:04:12 +00001936 case tok::string_literal: // primary-expression: string-literal
1937 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001938 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001939 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001940 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001941 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001942
Chris Lattner197a3012008-08-05 06:19:09 +00001943 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1944 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001945 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001946 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001947 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001948 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001949 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001950 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001951 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001952 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001953 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001954}
1955
Douglas Gregor8d4de672010-04-21 22:36:40 +00001956/// \brirg Parse the receiver of an Objective-C++ message send.
1957///
1958/// This routine parses the receiver of a message send in
1959/// Objective-C++ either as a type or as an expression. Note that this
1960/// routine must not be called to parse a send to 'super', since it
1961/// has no way to return such a result.
1962///
1963/// \param IsExpr Whether the receiver was parsed as an expression.
1964///
1965/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1966/// IsExpr is true), the parsed expression. If the receiver was parsed
1967/// as a type (\c IsExpr is false), the parsed type.
1968///
1969/// \returns True if an error occurred during parsing or semantic
1970/// analysis, in which case the arguments do not have valid
1971/// values. Otherwise, returns false for a successful parse.
1972///
1973/// objc-receiver: [C++]
1974/// 'super' [not parsed here]
1975/// expression
1976/// simple-type-specifier
1977/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001978bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001979 InMessageExpressionRAIIObject InMessage(*this, true);
1980
Douglas Gregor8d4de672010-04-21 22:36:40 +00001981 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1982 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1983 TryAnnotateTypeOrScopeToken();
1984
1985 if (!isCXXSimpleTypeSpecifier()) {
1986 // objc-receiver:
1987 // expression
John McCalldadc5752010-08-24 06:29:42 +00001988 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001989 if (Receiver.isInvalid())
1990 return true;
1991
1992 IsExpr = true;
1993 TypeOrExpr = Receiver.take();
1994 return false;
1995 }
1996
1997 // objc-receiver:
1998 // typename-specifier
1999 // simple-type-specifier
2000 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002001 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002002 ParseCXXSimpleTypeSpecifier(DS);
2003
2004 if (Tok.is(tok::l_paren)) {
2005 // If we see an opening parentheses at this point, we are
2006 // actually parsing an expression that starts with a
2007 // function-style cast, e.g.,
2008 //
2009 // postfix-expression:
2010 // simple-type-specifier ( expression-list [opt] )
2011 // typename-specifier ( expression-list [opt] )
2012 //
2013 // Parse the remainder of this case, then the (optional)
2014 // postfix-expression suffix, followed by the (optional)
2015 // right-hand side of the binary expression. We have an
2016 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002017 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002018 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002019 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002020 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002021 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002022 if (Receiver.isInvalid())
2023 return true;
2024
2025 IsExpr = true;
2026 TypeOrExpr = Receiver.take();
2027 return false;
2028 }
2029
2030 // We have a class message. Turn the simple-type-specifier or
2031 // typename-specifier we parsed into a type and parse the
2032 // remainder of the class message.
2033 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002034 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002035 if (Type.isInvalid())
2036 return true;
2037
2038 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002039 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002040 return false;
2041}
2042
Douglas Gregor990ccac2010-05-31 14:40:22 +00002043/// \brief Determine whether the parser is currently referring to a an
2044/// Objective-C message send, using a simplified heuristic to avoid overhead.
2045///
2046/// This routine will only return true for a subset of valid message-send
2047/// expressions.
2048bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00002049 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002050 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002051 return GetLookAheadToken(1).is(tok::identifier) &&
2052 GetLookAheadToken(2).is(tok::identifier);
2053}
2054
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002055bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2056 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2057 InMessageExpression)
2058 return false;
2059
2060
2061 ParsedType Type;
2062
2063 if (Tok.is(tok::annot_typename))
2064 Type = getTypeAnnotation(Tok);
2065 else if (Tok.is(tok::identifier))
2066 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2067 getCurScope());
2068 else
2069 return false;
2070
2071 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2072 const Token &AfterNext = GetLookAheadToken(2);
2073 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2074 if (Tok.is(tok::identifier))
2075 TryAnnotateTypeOrScopeToken();
2076
2077 return Tok.is(tok::annot_typename);
2078 }
2079 }
2080
2081 return false;
2082}
2083
Mike Stump11289f42009-09-09 15:08:12 +00002084/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002085/// '[' objc-receiver objc-message-args ']'
2086///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002087/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002088/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002089/// expression
2090/// class-name
2091/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002092///
John McCalldadc5752010-08-24 06:29:42 +00002093ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002094 assert(Tok.is(tok::l_square) && "'[' expected");
2095 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2096
Douglas Gregora817a192010-05-27 23:06:34 +00002097 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002098 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002099 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002100 return ExprError();
2101 }
2102
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002103 InMessageExpressionRAIIObject InMessage(*this, true);
2104
Douglas Gregor8d4de672010-04-21 22:36:40 +00002105 if (getLang().CPlusPlus) {
2106 // We completely separate the C and C++ cases because C++ requires
2107 // more complicated (read: slower) parsing.
2108
2109 // Handle send to super.
2110 // FIXME: This doesn't benefit from the same typo-correction we
2111 // get in Objective-C.
2112 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002113 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002114 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2115 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002116
2117 // Parse the receiver, which is either a type or an expression.
2118 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002119 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002120 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2121 SkipUntil(tok::r_square);
2122 return ExprError();
2123 }
2124
2125 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002126 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2127 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002128 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002129
2130 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002131 ParsedType::getFromOpaquePtr(TypeOrExpr),
2132 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002133 }
2134
2135 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002136 IdentifierInfo *Name = Tok.getIdentifierInfo();
2137 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002138 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002139 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002140 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002141 NextToken().is(tok::period),
2142 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002143 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002144 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2145 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002146
John McCallfaf5fb42010-08-26 23:41:50 +00002147 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002148 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002149 SkipUntil(tok::r_square);
2150 return ExprError();
2151 }
2152
Douglas Gregore5798dc2010-04-21 20:38:13 +00002153 ConsumeToken(); // the type name
2154
2155 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002156 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002157
John McCallfaf5fb42010-08-26 23:41:50 +00002158 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002159 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002160 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002161 }
Chris Lattner8f697062008-01-25 18:59:06 +00002162 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002163
2164 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002165 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002166 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002167 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002168 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002169 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002170
John McCallba7bf592010-08-24 05:47:05 +00002171 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2172 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002173}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002174
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002175/// \brief Parse the remainder of an Objective-C message following the
2176/// '[' objc-receiver.
2177///
2178/// This routine handles sends to super, class messages (sent to a
2179/// class name), and instance messages (sent to an object), and the
2180/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2181/// ReceiverExpr, respectively. Only one of these parameters may have
2182/// a valid value.
2183///
2184/// \param LBracLoc The location of the opening '['.
2185///
2186/// \param SuperLoc If this is a send to 'super', the location of the
2187/// 'super' keyword that indicates a send to the superclass.
2188///
2189/// \param ReceiverType If this is a class message, the type of the
2190/// class we are sending a message to.
2191///
2192/// \param ReceiverExpr If this is an instance message, the expression
2193/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002194///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002195/// objc-message-args:
2196/// objc-selector
2197/// objc-keywordarg-list
2198///
2199/// objc-keywordarg-list:
2200/// objc-keywordarg
2201/// objc-keywordarg-list objc-keywordarg
2202///
Mike Stump11289f42009-09-09 15:08:12 +00002203/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002204/// selector-name[opt] ':' objc-keywordexpr
2205///
2206/// objc-keywordexpr:
2207/// nonempty-expr-list
2208///
2209/// nonempty-expr-list:
2210/// assignment-expression
2211/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002212///
John McCalldadc5752010-08-24 06:29:42 +00002213ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002214Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002215 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002216 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002217 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002218 InMessageExpressionRAIIObject InMessage(*this, true);
2219
Steve Naroffeae65032009-11-07 02:08:14 +00002220 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002221 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002222 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2223 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002224 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002225 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2226 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002227 else
John McCallb268a282010-08-23 23:25:46 +00002228 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002229 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002230 cutOffParsing();
2231 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002232 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002233
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002234 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002235 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002236 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002237
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002238 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002239 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002240 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002241
Chris Lattner0ef13522007-10-09 17:51:17 +00002242 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002243 while (1) {
2244 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002245 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002246 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002247
Chris Lattner0ef13522007-10-09 17:51:17 +00002248 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002249 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002250 // We must manually skip to a ']', otherwise the expression skipper will
2251 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2252 // the enclosing expression.
2253 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002254 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002255 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002256
Steve Narofff73590d2007-09-27 14:38:14 +00002257 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002258 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002259
2260 if (Tok.is(tok::code_completion)) {
2261 if (SuperLoc.isValid())
2262 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2263 KeyIdents.data(),
2264 KeyIdents.size(),
2265 /*AtArgumentEpression=*/true);
2266 else if (ReceiverType)
2267 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2268 KeyIdents.data(),
2269 KeyIdents.size(),
2270 /*AtArgumentEpression=*/true);
2271 else
2272 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2273 KeyIdents.data(),
2274 KeyIdents.size(),
2275 /*AtArgumentEpression=*/true);
2276
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002277 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002278 return ExprError();
2279 }
2280
John McCalldadc5752010-08-24 06:29:42 +00002281 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002282 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002283 // We must manually skip to a ']', otherwise the expression skipper will
2284 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2285 // the enclosing expression.
2286 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002287 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002288 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002289
Steve Naroff486760a2007-09-17 20:25:27 +00002290 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002291 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002292
Douglas Gregor1b605f72009-11-19 01:08:35 +00002293 // Code completion after each argument.
2294 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002295 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002296 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002297 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002298 KeyIdents.size(),
2299 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002300 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002301 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002302 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002303 KeyIdents.size(),
2304 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002305 else
John McCallb268a282010-08-23 23:25:46 +00002306 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002307 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002308 KeyIdents.size(),
2309 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002310 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002311 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002312 }
2313
Steve Naroff486760a2007-09-17 20:25:27 +00002314 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002315 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002316 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002317 break;
2318 // We have a selector or a colon, continue parsing.
2319 }
2320 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002321 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002322 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002323 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002324 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002325 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002326 // We must manually skip to a ']', otherwise the expression skipper will
2327 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2328 // the enclosing expression.
2329 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002330 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002331 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002332
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002333 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002334 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002335 }
2336 } else if (!selIdent) {
2337 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002338
Chris Lattner197a3012008-08-05 06:19:09 +00002339 // We must manually skip to a ']', otherwise the expression skipper will
2340 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2341 // the enclosing expression.
2342 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002343 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002344 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002345
Chris Lattner0ef13522007-10-09 17:51:17 +00002346 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002347 if (Tok.is(tok::identifier))
2348 Diag(Tok, diag::err_expected_colon);
2349 else
2350 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002351 // We must manually skip to a ']', otherwise the expression skipper will
2352 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2353 // the enclosing expression.
2354 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002355 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002356 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002357
Chris Lattner8f697062008-01-25 18:59:06 +00002358 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002359
Steve Naroffe61bfa82007-10-05 18:42:47 +00002360 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002361 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002362 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002363 KeyLocs.push_back(Loc);
2364 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002365 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002366
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002367 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002368 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002369 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002370 MultiExprArg(Actions,
2371 KeyExprs.take(),
2372 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002373 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002374 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002375 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002376 MultiExprArg(Actions,
2377 KeyExprs.take(),
2378 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002379 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002380 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002381 MultiExprArg(Actions,
2382 KeyExprs.take(),
2383 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002384}
2385
John McCalldadc5752010-08-24 06:29:42 +00002386ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2387 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002388 if (Res.isInvalid()) return move(Res);
2389
Chris Lattnere002fbe2007-12-12 01:04:12 +00002390 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2391 // expressions. At this point, we know that the only valid thing that starts
2392 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002393 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002394 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002395 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002396 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002397
Chris Lattnere002fbe2007-12-12 01:04:12 +00002398 while (Tok.is(tok::at)) {
2399 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002400
Sebastian Redlc13f2682008-12-09 20:22:58 +00002401 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002402 if (!isTokenStringLiteral())
2403 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002404
John McCalldadc5752010-08-24 06:29:42 +00002405 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002406 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002407 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002408
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002409 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002410 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002411
2412 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2413 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002414}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002415
2416/// objc-encode-expression:
2417/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002418ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002419Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002420 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002421
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002422 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002423
Chris Lattner197a3012008-08-05 06:19:09 +00002424 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002425 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2426
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002427 BalancedDelimiterTracker T(*this, tok::l_paren);
2428 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002429
Douglas Gregor220cac52009-02-18 17:45:20 +00002430 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002431
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002432 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002433
Douglas Gregor220cac52009-02-18 17:45:20 +00002434 if (Ty.isInvalid())
2435 return ExprError();
2436
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002437 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2438 T.getOpenLocation(), Ty.get(),
2439 T.getCloseLocation()));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002440}
Anders Carlssone01493d2007-08-23 15:25:28 +00002441
2442/// objc-protocol-expression
2443/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002444ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002445Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002446 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002447
Chris Lattner197a3012008-08-05 06:19:09 +00002448 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002449 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2450
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002451 BalancedDelimiterTracker T(*this, tok::l_paren);
2452 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002453
Chris Lattner197a3012008-08-05 06:19:09 +00002454 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002455 return ExprError(Diag(Tok, diag::err_expected_ident));
2456
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002457 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002458 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002459
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002460 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002461
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002462 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002463 T.getOpenLocation(),
2464 T.getCloseLocation()));
Anders Carlssone01493d2007-08-23 15:25:28 +00002465}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002466
2467/// objc-selector-expression
2468/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002469ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002470 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002471
Chris Lattner197a3012008-08-05 06:19:09 +00002472 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002473 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2474
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002475 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002476 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002477
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002478 BalancedDelimiterTracker T(*this, tok::l_paren);
2479 T.consumeOpen();
2480
Douglas Gregor67c692c2010-08-26 15:07:07 +00002481 if (Tok.is(tok::code_completion)) {
2482 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2483 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002484 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002485 return ExprError();
2486 }
2487
Chris Lattner4f472a32009-04-11 18:13:45 +00002488 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002489 if (!SelIdent && // missing selector name.
2490 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002491 return ExprError(Diag(Tok, diag::err_expected_ident));
2492
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002493 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002494 unsigned nColons = 0;
2495 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002496 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002497 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2498 ++nColons;
2499 KeyIdents.push_back(0);
2500 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002501 return ExprError(Diag(Tok, diag::err_expected_colon));
2502
Chris Lattner1ba64452010-08-27 22:32:41 +00002503 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002504 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002505 if (Tok.is(tok::r_paren))
2506 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002507
2508 if (Tok.is(tok::code_completion)) {
2509 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2510 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002511 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002512 return ExprError();
2513 }
2514
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002515 // Check for another keyword selector.
2516 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002517 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002518 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002519 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002520 break;
2521 }
Steve Naroff152dd812007-12-05 22:21:29 +00002522 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002523 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002524 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002525 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002526 T.getOpenLocation(),
2527 T.getCloseLocation()));
Gabor Greif24032f12007-10-19 15:38:32 +00002528 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002529
2530Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2531
2532 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2533 // Append the current token at the end of the new token stream so that it
2534 // doesn't get lost.
2535 LM.Toks.push_back(Tok);
2536 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2537
2538 // MDecl might be null due to error in method prototype, etc.
2539 Decl *MDecl = LM.D;
2540 // Consume the previously pushed token.
2541 ConsumeAnyToken();
2542
2543 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2544 SourceLocation BraceLoc = Tok.getLocation();
2545 // Enter a scope for the method body.
2546 ParseScope BodyScope(this,
2547 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2548
2549 // Tell the actions module that we have entered a method definition with the
2550 // specified Declarator for the method.
2551 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2552
2553 if (PP.isCodeCompletionEnabled()) {
2554 if (trySkippingFunctionBodyForCodeCompletion()) {
2555 BodyScope.Exit();
2556 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2557 }
2558 }
2559
2560 StmtResult FnBody(ParseCompoundStatementBody());
2561
2562 // If the function body could not be parsed, make a bogus compoundstmt.
2563 if (FnBody.isInvalid())
2564 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2565 MultiStmtArg(Actions), false);
2566
2567 // Leave the function body scope.
2568 BodyScope.Exit();
2569
2570 return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2571}