blob: 7512bbc5ff316358caefce321fb5bbe75c695da5 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000020#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000021using namespace clang;
22
23
Chris Lattner3a907162008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
John McCall48871652010-08-21 09:40:31 +000032Decl *Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000034
Douglas Gregorf48706c2009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +000038 }
39
Steve Naroff7c348172007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000043 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000044 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000045 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000048 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000049 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
50 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000051 case tok::objc_implementation:
52 return ParseObjCAtImplementationDeclaration(AtLoc);
53 case tok::objc_end:
54 return ParseObjCAtEndDeclaration(AtLoc);
55 case tok::objc_compatibility_alias:
56 return ParseObjCAtAliasDeclaration(AtLoc);
57 case tok::objc_synthesize:
58 return ParseObjCPropertySynthesize(AtLoc);
59 case tok::objc_dynamic:
60 return ParseObjCPropertyDynamic(AtLoc);
61 default:
62 Diag(AtLoc, diag::err_unexpected_at);
63 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000064 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000065 }
66}
67
68///
Mike Stump11289f42009-09-09 15:08:12 +000069/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000071///
John McCall48871652010-08-21 09:40:31 +000072Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000073 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000074 SmallVector<IdentifierInfo *, 8> ClassNames;
75 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +000076
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000079 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000080 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000081 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000082 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000084 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000085 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner0ef13522007-10-09 17:51:17 +000088 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000089 break;
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerda59c2f2006-11-05 02:08:13 +000091 ConsumeToken();
92 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 // Consume the ';'.
95 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCall48871652010-08-21 09:40:31 +000096 return 0;
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremeneka26da852009-11-17 23:12:20 +000098 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
99 ClassLocs.data(),
100 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000101}
102
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000103///
104/// objc-interface:
105/// objc-class-interface-attributes[opt] objc-class-interface
106/// objc-category-interface
107///
108/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000109/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000110/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000111/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000112/// objc-interface-decl-list
113/// @end
114///
115/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000116/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000117/// objc-protocol-refs[opt]
118/// objc-interface-decl-list
119/// @end
120///
121/// objc-superclass:
122/// ':' identifier
123///
124/// objc-class-interface-attributes:
125/// __attribute__((visibility("default")))
126/// __attribute__((visibility("hidden")))
127/// __attribute__((deprecated))
128/// __attribute__((unavailable))
129/// __attribute__((objc_exception)) - used by NSException on 64-bit
130///
John McCall53fa7142010-12-24 02:08:15 +0000131Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
132 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000133 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000134 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
135 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000136
Douglas Gregor49c22a72009-11-18 16:26:39 +0000137 // Code completion after '@interface'.
138 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000139 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000140 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000141 }
142
Chris Lattner0ef13522007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000145 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000146 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000147
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000148 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000149 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000150 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000151 if (Tok.is(tok::l_paren) &&
152 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000153 // TODO(dgregor): Use the return value from the next line to provide better
154 // recovery.
155 ConsumeParen();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000156 SourceLocation categoryLoc, rparenLoc;
157 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000158 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000159 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000160 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000161 }
162
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000163 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000164 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 categoryId = Tok.getIdentifierInfo();
166 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000167 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000168 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000169 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000170 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000172 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000173 Diag(Tok, diag::err_expected_rparen);
174 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +0000175 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000176 }
177 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000178 // Next, we need to check for any protocol references.
179 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000180 SmallVector<Decl *, 8> ProtocolRefs;
181 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000182 if (Tok.is(tok::less) &&
183 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000184 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000185 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000186
John McCall53fa7142010-12-24 02:08:15 +0000187 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000188 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000189
John McCall48871652010-08-21 09:40:31 +0000190 Decl *CategoryType =
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000191 Actions.ActOnStartCategoryInterface(atLoc,
192 nameId, nameLoc,
193 categoryId, categoryLoc,
194 ProtocolRefs.data(),
195 ProtocolRefs.size(),
196 ProtocolLocs.data(),
197 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000198
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000199 if (Tok.is(tok::l_brace))
200 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
201
202 Actions.ActOnObjCContainerStartDefinition(CategoryType);
203 ParseObjCInterfaceDeclList(tok::objc_not_keyword);
204 Actions.ActOnObjCContainerFinishDefinition(CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000205 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000206 }
207 // Parse a class interface.
208 IdentifierInfo *superClassId = 0;
209 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000210
Chris Lattner0ef13522007-10-09 17:51:17 +0000211 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000212 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000213
214 // Code completion of superclass names.
215 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000216 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000217 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000218 }
219
Chris Lattner0ef13522007-10-09 17:51:17 +0000220 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000222 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000223 }
224 superClassId = Tok.getIdentifierInfo();
225 superClassLoc = ConsumeToken();
226 }
227 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000228 SmallVector<Decl *, 8> ProtocolRefs;
229 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000230 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000231 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000232 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
233 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000234 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000235
John McCall48871652010-08-21 09:40:31 +0000236 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000237 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000238 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000239 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000240 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000241 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000242
Chris Lattner0ef13522007-10-09 17:51:17 +0000243 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000244 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000245
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000246 Actions.ActOnObjCContainerStartDefinition(ClsType);
247 ParseObjCInterfaceDeclList(tok::objc_interface);
248 Actions.ActOnObjCContainerFinishDefinition(ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000249 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000250}
251
John McCall064d77b2009-12-03 22:31:13 +0000252/// The Objective-C property callback. This should be defined where
253/// it's used, but instead it's been lifted to here to support VS2005.
254struct Parser::ObjCPropertyCallback : FieldCallback {
255 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000256 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000257 ObjCDeclSpec &OCDS;
258 SourceLocation AtLoc;
259 tok::ObjCKeywordKind MethodImplKind;
260
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000261 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000262 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000263 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
264 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000265 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCall064d77b2009-12-03 22:31:13 +0000266 MethodImplKind(MethodImplKind) {
267 }
268
John McCall48871652010-08-21 09:40:31 +0000269 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000270 if (FD.D.getIdentifier() == 0) {
271 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
272 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000273 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000274 }
275 if (FD.BitfieldSize) {
276 P.Diag(AtLoc, diag::err_objc_property_bitfield)
277 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000278 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000279 }
280
281 // Install the property declarator into interfaceDecl.
282 IdentifierInfo *SelName =
283 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
284
285 Selector GetterSel =
286 P.PP.getSelectorTable().getNullarySelector(SelName);
287 IdentifierInfo *SetterName = OCDS.getSetterName();
288 Selector SetterSel;
289 if (SetterName)
290 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
291 else
292 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
293 P.PP.getSelectorTable(),
294 FD.D.getIdentifier());
295 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000296 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000297 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000298 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000299 &isOverridingProperty,
300 MethodImplKind);
301 if (!isOverridingProperty)
302 Props.push_back(Property);
303
304 return Property;
305 }
306};
307
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000308/// objc-interface-decl-list:
309/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000310/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000311/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000312/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000313/// objc-interface-decl-list declaration
314/// objc-interface-decl-list ';'
315///
Steve Naroff99264b42007-08-22 16:35:03 +0000316/// objc-method-requirement: [OBJC2]
317/// @required
318/// @optional
319///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000320void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000321 SmallVector<Decl *, 32> allMethods;
322 SmallVector<Decl *, 16> allProperties;
323 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000324 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenekc7c64312010-01-07 01:20:12 +0000326 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000327
Steve Naroff99264b42007-08-22 16:35:03 +0000328 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000329 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000330 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000331 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000332 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000333 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000334 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
335 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000336 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
337 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000338 continue;
339 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000340 if (Tok.is(tok::l_paren)) {
341 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000342 ParseObjCMethodDecl(Tok.getLocation(),
343 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000344 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000345 continue;
346 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000347 // Ignore excess semicolons.
348 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000349 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000350 continue;
351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattnerda9fb152008-10-20 06:10:06 +0000353 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000354 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000355 break;
Mike Stump11289f42009-09-09 15:08:12 +0000356
Douglas Gregorf1934162010-01-13 21:24:21 +0000357 // Code completion within an Objective-C interface.
358 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000359 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000360 ObjCImpDecl? Sema::PCC_ObjCImplementation
361 : Sema::PCC_ObjCInterface);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000362 ConsumeCodeCompletionToken();
Douglas Gregorf1934162010-01-13 21:24:21 +0000363 }
364
Chris Lattner038a3e32008-10-20 05:46:22 +0000365 // If we don't have an @ directive, parse it as a function definition.
366 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000367 // The code below does not consume '}'s because it is afraid of eating the
368 // end of a namespace. Because of the way this code is structured, an
369 // erroneous r_brace would cause an infinite loop if not handled here.
370 if (Tok.is(tok::r_brace))
371 break;
John McCall084e83d2011-03-24 11:26:52 +0000372 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000373 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000374 continue;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattner038a3e32008-10-20 05:46:22 +0000377 // Otherwise, we have an @ directive, eat the @.
378 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000379 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000380 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000381 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000382 break;
383 }
384
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000385 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000387 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000388 AtEnd.setBegin(AtLoc);
389 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000390 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000391 } else if (DirectiveKind == tok::objc_not_keyword) {
392 Diag(Tok, diag::err_objc_unknown_at);
393 SkipUntil(tok::semi);
394 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattnerda9fb152008-10-20 06:10:06 +0000397 // Eat the identifier.
398 ConsumeToken();
399
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000400 switch (DirectiveKind) {
401 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000402 // FIXME: If someone forgets an @end on a protocol, this loop will
403 // continue to eat up tons of stuff and spew lots of nonsense errors. It
404 // would probably be better to bail out if we saw an @class or @interface
405 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000406 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000407 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000408 SkipUntil(tok::r_brace, tok::at);
409 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000410
411 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000412 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000413 Diag(Tok, diag::err_objc_missing_end);
414 ConsumeToken();
415 break;
416
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000417 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000419 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000420 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000421 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000422 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000423 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000424 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000425 break;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000427 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000428 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000429 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000430
Chris Lattner038a3e32008-10-20 05:46:22 +0000431 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000432 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000433 if (Tok.is(tok::l_paren))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000434 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000435
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000436 ObjCPropertyCallback Callback(*this, allProperties,
John McCall064d77b2009-12-03 22:31:13 +0000437 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000438
Chris Lattner038a3e32008-10-20 05:46:22 +0000439 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000440 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000441 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000442
John McCall405988b2011-03-26 01:53:26 +0000443 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000444 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000445 }
Steve Naroff99264b42007-08-22 16:35:03 +0000446 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000447
448 // We break out of the big loop in two cases: when we see @end or when we see
449 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000450 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000451 Actions.CodeCompleteObjCAtDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000452 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000453 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000454 ConsumeToken(); // the "end" identifier
455 else
456 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000458 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000459 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000460 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000461 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000462 allProperties.data(), allProperties.size(),
463 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000464}
465
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000466/// Parse property attribute declarations.
467///
468/// property-attr-decl: '(' property-attrlist ')'
469/// property-attrlist:
470/// property-attribute
471/// property-attrlist ',' property-attribute
472/// property-attribute:
473/// getter '=' identifier
474/// setter '=' identifier ':'
475/// readonly
476/// readwrite
477/// assign
478/// retain
479/// copy
480/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000481/// atomic
482/// strong
483/// weak
484/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000485///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000486void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000487 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000488 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000490 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000491 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000492 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000493 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000494 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000495 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattner76619232008-10-20 07:22:18 +0000497 // If this is not an identifier at all, bail out early.
498 if (II == 0) {
499 MatchRHSPunctuation(tok::r_paren, LHSLoc);
500 return;
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Chris Lattner1db33542008-10-20 07:37:22 +0000503 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner68e48682008-11-20 04:42:34 +0000505 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000506 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000507 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000508 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000509 else if (II->isStr("unsafe_unretained"))
510 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000511 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000513 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000515 else if (II->isStr("strong"))
516 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000517 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000518 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000519 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000521 else if (II->isStr("atomic"))
522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000523 else if (II->isStr("weak"))
524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000525 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000526 bool IsSetter = II->getNameStart()[0] == 's';
527
Chris Lattner825bca12008-10-20 07:39:53 +0000528 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000529 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
530 diag::err_objc_expected_equal_for_getter;
531
532 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000533 return;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregorc8537c52009-11-19 07:41:15 +0000535 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000536 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000537 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000538 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000539 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000540 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000541 }
542
Anders Carlssonfe15a782010-10-02 17:45:21 +0000543
544 SourceLocation SelLoc;
545 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
546
547 if (!SelIdent) {
548 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
549 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000550 SkipUntil(tok::r_paren);
551 return;
552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Anders Carlssonfe15a782010-10-02 17:45:21 +0000554 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000555 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000556 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000557
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000558 if (ExpectAndConsume(tok::colon,
559 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000560 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000561 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000562 } else {
563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000564 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000565 }
Chris Lattner825bca12008-10-20 07:39:53 +0000566 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000567 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000568 SkipUntil(tok::r_paren);
569 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000570 }
Mike Stump11289f42009-09-09 15:08:12 +0000571
Chris Lattner1db33542008-10-20 07:37:22 +0000572 if (Tok.isNot(tok::comma))
573 break;
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner1db33542008-10-20 07:37:22 +0000575 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner1db33542008-10-20 07:37:22 +0000578 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000579}
580
Steve Naroff09bf8152007-09-06 21:24:23 +0000581/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000582/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000583/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000584///
585/// objc-instance-method: '-'
586/// objc-class-method: '+'
587///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000588/// objc-method-attributes: [OBJC2]
589/// __attribute__((deprecated))
590///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000591Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000592 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000593 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000594
Mike Stump11289f42009-09-09 15:08:12 +0000595 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000596 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000597 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000598 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000599 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000600 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000601 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000602}
603
604/// objc-selector:
605/// identifier
606/// one of
607/// enum struct union if else while do for switch case default
608/// break continue return goto asm sizeof typeof __alignof
609/// unsigned long const short volatile signed restrict _Complex
610/// in out inout bycopy byref oneway int char float double void _Bool
611///
Chris Lattner4f472a32009-04-11 18:13:45 +0000612IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000613
Chris Lattner5700fab2007-10-07 02:00:24 +0000614 switch (Tok.getKind()) {
615 default:
616 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000617 case tok::ampamp:
618 case tok::ampequal:
619 case tok::amp:
620 case tok::pipe:
621 case tok::tilde:
622 case tok::exclaim:
623 case tok::exclaimequal:
624 case tok::pipepipe:
625 case tok::pipeequal:
626 case tok::caret:
627 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000628 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000629 if (isalpha(ThisTok[0])) {
630 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
631 Tok.setKind(tok::identifier);
632 SelectorLoc = ConsumeToken();
633 return II;
634 }
635 return 0;
636 }
637
Chris Lattner5700fab2007-10-07 02:00:24 +0000638 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000639 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000640 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000641 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000642 case tok::kw_break:
643 case tok::kw_case:
644 case tok::kw_catch:
645 case tok::kw_char:
646 case tok::kw_class:
647 case tok::kw_const:
648 case tok::kw_const_cast:
649 case tok::kw_continue:
650 case tok::kw_default:
651 case tok::kw_delete:
652 case tok::kw_do:
653 case tok::kw_double:
654 case tok::kw_dynamic_cast:
655 case tok::kw_else:
656 case tok::kw_enum:
657 case tok::kw_explicit:
658 case tok::kw_export:
659 case tok::kw_extern:
660 case tok::kw_false:
661 case tok::kw_float:
662 case tok::kw_for:
663 case tok::kw_friend:
664 case tok::kw_goto:
665 case tok::kw_if:
666 case tok::kw_inline:
667 case tok::kw_int:
668 case tok::kw_long:
669 case tok::kw_mutable:
670 case tok::kw_namespace:
671 case tok::kw_new:
672 case tok::kw_operator:
673 case tok::kw_private:
674 case tok::kw_protected:
675 case tok::kw_public:
676 case tok::kw_register:
677 case tok::kw_reinterpret_cast:
678 case tok::kw_restrict:
679 case tok::kw_return:
680 case tok::kw_short:
681 case tok::kw_signed:
682 case tok::kw_sizeof:
683 case tok::kw_static:
684 case tok::kw_static_cast:
685 case tok::kw_struct:
686 case tok::kw_switch:
687 case tok::kw_template:
688 case tok::kw_this:
689 case tok::kw_throw:
690 case tok::kw_true:
691 case tok::kw_try:
692 case tok::kw_typedef:
693 case tok::kw_typeid:
694 case tok::kw_typename:
695 case tok::kw_typeof:
696 case tok::kw_union:
697 case tok::kw_unsigned:
698 case tok::kw_using:
699 case tok::kw_virtual:
700 case tok::kw_void:
701 case tok::kw_volatile:
702 case tok::kw_wchar_t:
703 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000704 case tok::kw__Bool:
705 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000706 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000707 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000708 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000709 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000710 }
Steve Naroff99264b42007-08-22 16:35:03 +0000711}
712
Fariborz Jahanian83615522008-01-02 22:54:34 +0000713/// objc-for-collection-in: 'in'
714///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000715bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000716 // FIXME: May have to do additional look-ahead to only allow for
717 // valid tokens following an 'in'; such as an identifier, unary operators,
718 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000719 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000720 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000721}
722
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000723/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000724/// qualifier list and builds their bitmask representation in the input
725/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000726///
727/// objc-type-qualifiers:
728/// objc-type-qualifier
729/// objc-type-qualifiers objc-type-qualifier
730///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000731void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
732 ObjCTypeNameContext Context) {
Chris Lattner61511e12007-12-12 06:56:32 +0000733 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000734 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000735 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
736 Context == OTN_ParameterType);
Douglas Gregor99fa2642010-08-24 01:06:58 +0000737 ConsumeCodeCompletionToken();
738 }
739
Chris Lattner5e530bc2007-12-27 19:57:00 +0000740 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000741 return;
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattner61511e12007-12-12 06:56:32 +0000743 const IdentifierInfo *II = Tok.getIdentifierInfo();
744 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000745 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000746 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000747
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000748 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000749 switch (i) {
750 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000751 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
752 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
753 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
754 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
755 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
756 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000757 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000758 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000759 ConsumeToken();
760 II = 0;
761 break;
762 }
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner61511e12007-12-12 06:56:32 +0000764 // If this wasn't a recognized qualifier, bail out.
765 if (II) return;
766 }
767}
768
769/// objc-type-name:
770/// '(' objc-type-qualifiers[opt] type-name ')'
771/// '(' objc-type-qualifiers[opt] ')'
772///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000773ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
774 ObjCTypeNameContext Context) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000775 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnerb7954432008-10-22 03:52:06 +0000777 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000778 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000779 ObjCDeclContextSwitch ObjCDC(*this);
780
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000781 // Parse type qualifiers, in, inout, etc.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000782 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000783
John McCallba7bf592010-08-24 05:47:05 +0000784 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000785 if (isTypeSpecifierQualifier()) {
John McCall31168b02011-06-15 23:02:42 +0000786 TypeResult TypeSpec =
787 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor220cac52009-02-18 17:45:20 +0000788 if (!TypeSpec.isInvalid())
789 Ty = TypeSpec.get();
790 }
John McCall31168b02011-06-15 23:02:42 +0000791
Steve Naroff90255b42008-10-21 14:15:04 +0000792 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000793 ConsumeParen();
794 else if (Tok.getLocation() == TypeStartLoc) {
795 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000796 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000797 SkipUntil(tok::r_paren);
798 } else {
799 // Otherwise, we found *something*, but didn't get a ')' in the right
800 // place. Emit an error then return what we have as the type.
801 MatchRHSPunctuation(tok::r_paren, LParenLoc);
802 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000803 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000804}
805
806/// objc-method-decl:
807/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000808/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000809/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000810/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000811///
812/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000813/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000814/// objc-keyword-selector objc-keyword-decl
815///
816/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000817/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
818/// objc-selector ':' objc-keyword-attributes[opt] identifier
819/// ':' objc-type-name objc-keyword-attributes[opt] identifier
820/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000821///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000822/// objc-parmlist:
823/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000824///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000825/// objc-parms:
826/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000827///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000828/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000829/// , ...
830///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000831/// objc-keyword-attributes: [OBJC2]
832/// __attribute__((unused))
833///
John McCall48871652010-08-21 09:40:31 +0000834Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000835 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000836 tok::ObjCKeywordKind MethodImplKind,
837 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000838 ParsingDeclRAIIObject PD(*this);
839
Douglas Gregor636a61e2010-04-07 00:21:17 +0000840 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000841 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000842 /*ReturnType=*/ ParsedType());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000843 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000844 }
845
Chris Lattner2ebb1782008-08-23 01:48:03 +0000846 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000847 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000848 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000849 if (Tok.is(tok::l_paren))
Douglas Gregor95d3e372011-03-08 19:17:54 +0000850 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump11289f42009-09-09 15:08:12 +0000851
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000852 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000853 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000854 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000855 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000856
Douglas Gregor636a61e2010-04-07 00:21:17 +0000857 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000858 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000859 ReturnType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000860 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000861 }
862
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000863 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000864 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000865 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000866
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000867 // An unnamed colon is valid.
868 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000869 Diag(Tok, diag::err_expected_selector_for_method)
870 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000871 // Skip until we get a ; or {}.
872 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000873 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000876 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000877 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000878 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000879 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000880 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +0000881
Chris Lattner5700fab2007-10-07 02:00:24 +0000882 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000883 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000884 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000885 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +0000886 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000887 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +0000888 methodAttrs.getList(), MethodImplKind,
889 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +0000890 PD.complete(Result);
891 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000892 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000893
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000894 SmallVector<IdentifierInfo *, 12> KeyIdents;
895 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000896 ParseScope PrototypeScope(this,
897 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +0000898
899 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000900
Chris Lattner5700fab2007-10-07 02:00:24 +0000901 while (1) {
John McCall084e83d2011-03-24 11:26:52 +0000902 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +0000903 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000904
Chris Lattner5700fab2007-10-07 02:00:24 +0000905 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000906 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000907 Diag(Tok, diag::err_expected_colon);
908 break;
909 }
910 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000911
John McCallba7bf592010-08-24 05:47:05 +0000912 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000913 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000914 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000915
Chris Lattner5700fab2007-10-07 02:00:24 +0000916 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000917 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +0000918 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +0000919 MaybeParseGNUAttributes(paramAttrs);
920 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +0000921 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000922
Douglas Gregor45879692010-07-08 23:37:41 +0000923 // Code completion for the next piece of the selector.
924 if (Tok.is(tok::code_completion)) {
925 ConsumeCodeCompletionToken();
926 KeyIdents.push_back(SelIdent);
927 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
928 mType == tok::minus,
929 /*AtParameterName=*/true,
930 ReturnType,
931 KeyIdents.data(),
932 KeyIdents.size());
933 KeyIdents.pop_back();
934 break;
935 }
936
Chris Lattner0ef13522007-10-09 17:51:17 +0000937 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000938 Diag(Tok, diag::err_expected_ident); // missing argument name.
939 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000940 }
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000942 ArgInfo.Name = Tok.getIdentifierInfo();
943 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000944 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000946 ArgInfos.push_back(ArgInfo);
947 KeyIdents.push_back(SelIdent);
948
John McCall084e83d2011-03-24 11:26:52 +0000949 // Make sure the attributes persist.
950 allParamAttrs.takeAllFrom(paramAttrs.getPool());
951
Douglas Gregor95887f92010-07-08 23:20:03 +0000952 // Code completion for the next piece of the selector.
953 if (Tok.is(tok::code_completion)) {
954 ConsumeCodeCompletionToken();
955 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
956 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000957 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000958 ReturnType,
959 KeyIdents.data(),
960 KeyIdents.size());
961 break;
962 }
963
Chris Lattner5700fab2007-10-07 02:00:24 +0000964 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000965 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000966 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000967 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000968 break;
969 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000970 }
Mike Stump11289f42009-09-09 15:08:12 +0000971
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000972 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000973
Chris Lattner5700fab2007-10-07 02:00:24 +0000974 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000975 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000976 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000977 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000978 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000979 ConsumeToken();
980 break;
981 }
John McCall084e83d2011-03-24 11:26:52 +0000982 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +0000983 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000984 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000985 Declarator ParmDecl(DS, Declarator::PrototypeContext);
986 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000987 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +0000988 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000989 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
990 ParmDecl.getIdentifierLoc(),
991 Param,
992 0));
993
Chris Lattner5700fab2007-10-07 02:00:24 +0000994 }
Mike Stump11289f42009-09-09 15:08:12 +0000995
Cameron Esfahanif6c73c42010-10-12 00:21:25 +0000996 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000997 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000998 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000999 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001000
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001001 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001002 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001003
Chris Lattner5700fab2007-10-07 02:00:24 +00001004 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1005 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001006 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001007 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001008 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001009 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001010 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001011 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001012 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001013
John McCall28a6aea2009-11-04 02:18:39 +00001014 PD.complete(Result);
1015 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001016}
1017
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001018/// objc-protocol-refs:
1019/// '<' identifier-list '>'
1020///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001021bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001022ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1023 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001024 bool WarnOnDeclarations,
1025 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001026 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001027
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001028 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001030 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001031
Chris Lattner3bbae002008-07-26 04:03:38 +00001032 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001033 if (Tok.is(tok::code_completion)) {
1034 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1035 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001036 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +00001037 }
1038
Chris Lattner3bbae002008-07-26 04:03:38 +00001039 if (Tok.isNot(tok::identifier)) {
1040 Diag(Tok, diag::err_expected_ident);
1041 SkipUntil(tok::greater);
1042 return true;
1043 }
1044 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1045 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001046 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001047 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattner3bbae002008-07-26 04:03:38 +00001049 if (Tok.isNot(tok::comma))
1050 break;
1051 ConsumeToken();
1052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattner3bbae002008-07-26 04:03:38 +00001054 // Consume the '>'.
1055 if (Tok.isNot(tok::greater)) {
1056 Diag(Tok, diag::err_expected_greater);
1057 return true;
1058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Chris Lattner3bbae002008-07-26 04:03:38 +00001060 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001061
Chris Lattner3bbae002008-07-26 04:03:38 +00001062 // Convert the list of protocols identifiers into a list of protocol decls.
1063 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1064 &ProtocolIdents[0], ProtocolIdents.size(),
1065 Protocols);
1066 return false;
1067}
1068
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001069/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1070/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001071bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001072 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1073 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1074 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001075 SmallVector<Decl *, 8> ProtocolDecl;
1076 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001077 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1078 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001079 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1080 ProtocolLocs.data(), LAngleLoc);
1081 if (EndProtoLoc.isValid())
1082 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001083 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001084}
1085
1086
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001087/// objc-class-instance-variables:
1088/// '{' objc-instance-variable-decl-list[opt] '}'
1089///
1090/// objc-instance-variable-decl-list:
1091/// objc-visibility-spec
1092/// objc-instance-variable-decl ';'
1093/// ';'
1094/// objc-instance-variable-decl-list objc-visibility-spec
1095/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1096/// objc-instance-variable-decl-list ';'
1097///
1098/// objc-visibility-spec:
1099/// @private
1100/// @protected
1101/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001102/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001103///
1104/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001105/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001106///
John McCall48871652010-08-21 09:40:31 +00001107void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001108 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001109 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001110 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001111 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001112
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001113 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001114
Steve Naroff00433d32007-08-21 21:17:12 +00001115 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001116
Steve Naroff00433d32007-08-21 21:17:12 +00001117 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001119 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001120
Steve Naroff00433d32007-08-21 21:17:12 +00001121 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001122 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001123 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001124 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001125 ConsumeToken();
1126 continue;
1127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
Steve Naroff00433d32007-08-21 21:17:12 +00001129 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001130 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001131 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001132
1133 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001134 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001135 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001136 }
1137
Steve Naroff7c348172007-08-23 18:16:40 +00001138 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001139 case tok::objc_private:
1140 case tok::objc_public:
1141 case tok::objc_protected:
1142 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001143 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001144 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001145 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001146 default:
1147 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001148 continue;
1149 }
1150 }
Mike Stump11289f42009-09-09 15:08:12 +00001151
Douglas Gregor48d46252010-01-13 21:54:15 +00001152 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001153 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001154 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001155 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001156 }
1157
John McCallcfefb6d2009-11-03 02:38:08 +00001158 struct ObjCIvarCallback : FieldCallback {
1159 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001160 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001161 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001162 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001163
John McCall48871652010-08-21 09:40:31 +00001164 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001165 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001166 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1167 }
1168
John McCall48871652010-08-21 09:40:31 +00001169 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001170 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001171 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001172 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001173 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001174 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001175 FD.D, FD.BitfieldSize, visibility);
1176 P.Actions.ActOnObjCContainerFinishDefinition(IDecl);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001177 if (Field)
1178 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001179 return Field;
1180 }
1181 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001182
Chris Lattnera12405b2008-04-10 06:46:29 +00001183 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001184 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001185 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001186
Chris Lattner0ef13522007-10-09 17:51:17 +00001187 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001188 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001189 } else {
1190 Diag(Tok, diag::err_expected_semi_decl_list);
1191 // Skip to end of block or statement
1192 SkipUntil(tok::r_brace, true, true);
1193 }
1194 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001195 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001196 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1197 Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
1198 Actions.ActOnObjCContainerFinishDefinition(interfaceDecl);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001199 // Call ActOnFields() even if we don't have any decls. This is useful
1200 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001201 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001202 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001203 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001204 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001205}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001206
1207/// objc-protocol-declaration:
1208/// objc-protocol-definition
1209/// objc-protocol-forward-reference
1210///
1211/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001212/// @protocol identifier
1213/// objc-protocol-refs[opt]
1214/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001215/// @end
1216///
1217/// objc-protocol-forward-reference:
1218/// @protocol identifier-list ';'
1219///
1220/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001221/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001222/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001223Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001224 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001225 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001226 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1227 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001228
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001229 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001230 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001231 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001232 }
1233
Chris Lattner0ef13522007-10-09 17:51:17 +00001234 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001235 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001236 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001237 }
1238 // Save the protocol name, then consume it.
1239 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1240 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001241
Chris Lattner0ef13522007-10-09 17:51:17 +00001242 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001243 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001244 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001245 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001246 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001247 }
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattner0ef13522007-10-09 17:51:17 +00001249 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001250 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001251 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1252
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001253 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001254 while (1) {
1255 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001256 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001257 Diag(Tok, diag::err_expected_ident);
1258 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001259 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001260 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001261 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1262 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001263 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001264
Chris Lattner0ef13522007-10-09 17:51:17 +00001265 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001266 break;
1267 }
1268 // Consume the ';'.
1269 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001270 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001271
Steve Naroff93eb5f12007-10-10 17:32:04 +00001272 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001273 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001274 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001275 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001278 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001279 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001280
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001281 SmallVector<Decl *, 8> ProtocolRefs;
1282 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001283 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001284 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1285 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001286 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001287
John McCall48871652010-08-21 09:40:31 +00001288 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001289 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001290 ProtocolRefs.data(),
1291 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001292 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001293 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001294
1295
1296 Actions.ActOnObjCContainerStartDefinition(ProtoType);
1297 ParseObjCInterfaceDeclList(tok::objc_protocol);
1298 Actions.ActOnObjCContainerFinishDefinition(ProtoType);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001299 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001300}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001301
1302/// objc-implementation:
1303/// objc-class-implementation-prologue
1304/// objc-category-implementation-prologue
1305///
1306/// objc-class-implementation-prologue:
1307/// @implementation identifier objc-superclass[opt]
1308/// objc-class-instance-variables[opt]
1309///
1310/// objc-category-implementation-prologue:
1311/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001312Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001313 SourceLocation atLoc) {
1314 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1315 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1316 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001317
Douglas Gregor49c22a72009-11-18 16:26:39 +00001318 // Code completion after '@implementation'.
1319 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001320 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001321 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001322 }
1323
Chris Lattner0ef13522007-10-09 17:51:17 +00001324 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001325 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001326 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001327 }
1328 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001329 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001330 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001331
1332 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001333 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001334 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001335 SourceLocation categoryLoc, rparenLoc;
1336 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001337
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001338 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001339 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001340 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001341 }
1342
Chris Lattner0ef13522007-10-09 17:51:17 +00001343 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001344 categoryId = Tok.getIdentifierInfo();
1345 categoryLoc = ConsumeToken();
1346 } else {
1347 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001348 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001349 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001350 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001351 Diag(Tok, diag::err_expected_rparen);
1352 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001353 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001354 }
1355 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001356 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001357 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001358 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001359
1360 Actions.ActOnObjCContainerStartDefinition(ImplCatType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001361 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001362 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001363 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001364 }
1365 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001366 SourceLocation superClassLoc;
1367 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001368 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001369 // We have a super class
1370 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001371 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001372 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001373 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001374 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001375 superClassId = Tok.getIdentifierInfo();
1376 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001377 }
John McCall48871652010-08-21 09:40:31 +00001378 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001379 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001380 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001381
Steve Naroff33a1e802007-10-29 21:38:07 +00001382 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001383 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1384
1385 Actions.ActOnObjCContainerStartDefinition(ImplClsType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001386 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001387 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001388 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001389}
Steve Naroff33a1e802007-10-29 21:38:07 +00001390
John McCall48871652010-08-21 09:40:31 +00001391Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001392 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1393 "ParseObjCAtEndDeclaration(): Expected @end");
John McCall48871652010-08-21 09:40:31 +00001394 Decl *Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001395 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001396 if (ObjCImpDecl) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001397 Actions.ActOnAtEnd(getCurScope(), atEnd);
1398 Actions.ActOnObjCContainerFinishDefinition(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001399 ObjCImpDecl = 0;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001400 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001401 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001402 else {
1403 // missing @implementation
Fariborz Jahanianc0577942011-04-22 22:02:28 +00001404 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001405 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001406 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001407}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001408
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001409Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1410 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001411 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001412 return Actions.ConvertDeclToDeclGroup(0);
1413 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001414 Actions.ActOnAtEnd(getCurScope(), SourceRange());
1415 Actions.ActOnObjCContainerFinishDefinition(ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001416 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1417}
1418
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001419/// compatibility-alias-decl:
1420/// @compatibility_alias alias-name class-name ';'
1421///
John McCall48871652010-08-21 09:40:31 +00001422Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001423 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1424 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1425 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001426 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001427 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001428 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001429 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001430 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1431 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001432 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001433 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001434 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001435 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001436 IdentifierInfo *classId = Tok.getIdentifierInfo();
1437 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001438 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1439 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001440 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1441 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001442}
1443
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001444/// property-synthesis:
1445/// @synthesize property-ivar-list ';'
1446///
1447/// property-ivar-list:
1448/// property-ivar
1449/// property-ivar-list ',' property-ivar
1450///
1451/// property-ivar:
1452/// identifier
1453/// identifier '=' identifier
1454///
John McCall48871652010-08-21 09:40:31 +00001455Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001456 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1457 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001458 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001459
Douglas Gregor88e72a02009-11-18 19:45:45 +00001460 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001461 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001462 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001463 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001464 }
1465
Douglas Gregor88e72a02009-11-18 19:45:45 +00001466 if (Tok.isNot(tok::identifier)) {
1467 Diag(Tok, diag::err_synthesized_property_name);
1468 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001469 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001470 }
1471
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001472 IdentifierInfo *propertyIvar = 0;
1473 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1474 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001475 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001476 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001477 // property '=' ivar-name
1478 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001479
1480 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001481 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001482 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001483 }
1484
Chris Lattner0ef13522007-10-09 17:51:17 +00001485 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001486 Diag(Tok, diag::err_expected_ident);
1487 break;
1488 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001489 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001490 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001491 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001492 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001493 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001494 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001495 break;
1496 ConsumeToken(); // consume ','
1497 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001498 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001499 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001500}
1501
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001502/// property-dynamic:
1503/// @dynamic property-list
1504///
1505/// property-list:
1506/// identifier
1507/// property-list ',' identifier
1508///
John McCall48871652010-08-21 09:40:31 +00001509Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001510 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1511 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001512 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001513 while (true) {
1514 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001515 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001516 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001517 }
1518
1519 if (Tok.isNot(tok::identifier)) {
1520 Diag(Tok, diag::err_expected_ident);
1521 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001522 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001523 }
1524
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001525 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1526 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001527 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001528 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001529
Chris Lattner0ef13522007-10-09 17:51:17 +00001530 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001531 break;
1532 ConsumeToken(); // consume ','
1533 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001534 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001535 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001536}
Mike Stump11289f42009-09-09 15:08:12 +00001537
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001538/// objc-throw-statement:
1539/// throw expression[opt];
1540///
John McCalldadc5752010-08-24 06:29:42 +00001541StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1542 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001543 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001544 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001545 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001546 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001547 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001548 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001549 }
1550 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001551 // consume ';'
1552 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001553 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001554}
1555
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001556/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001557/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001558///
John McCalldadc5752010-08-24 06:29:42 +00001559StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001560Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001561 ConsumeToken(); // consume synchronized
1562 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001563 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001564 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001565 }
John McCalld9bb7432011-07-27 21:50:02 +00001566
1567 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001568 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001569 ExprResult operand(ParseExpression());
1570
1571 if (Tok.is(tok::r_paren)) {
1572 ConsumeParen(); // ')'
1573 } else {
1574 if (!operand.isInvalid())
1575 Diag(Tok, diag::err_expected_rparen);
1576
1577 // Skip forward until we see a left brace, but don't consume it.
1578 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001579 }
John McCalld9bb7432011-07-27 21:50:02 +00001580
1581 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001582 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001583 if (!operand.isInvalid())
1584 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001585 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001586 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001587
John McCalld9bb7432011-07-27 21:50:02 +00001588 // Check the @synchronized operand now.
1589 if (!operand.isInvalid())
1590 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001591
John McCalld9bb7432011-07-27 21:50:02 +00001592 // Parse the compound statement within a new scope.
1593 ParseScope bodyScope(this, Scope::DeclScope);
1594 StmtResult body(ParseCompoundStatementBody());
1595 bodyScope.Exit();
1596
1597 // If there was a semantic or parse error earlier with the
1598 // operand, fail now.
1599 if (operand.isInvalid())
1600 return StmtError();
1601
1602 if (body.isInvalid())
1603 body = Actions.ActOnNullStmt(Tok.getLocation());
1604
1605 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001606}
1607
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001608/// objc-try-catch-statement:
1609/// @try compound-statement objc-catch-list[opt]
1610/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1611///
1612/// objc-catch-list:
1613/// @catch ( parameter-declaration ) compound-statement
1614/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1615/// catch-parameter-declaration:
1616/// parameter-declaration
1617/// '...' [OBJC2]
1618///
John McCalldadc5752010-08-24 06:29:42 +00001619StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001620 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001621
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001622 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001623 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001624 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001625 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001626 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001627 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001628 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001629 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001630 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001631 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001632 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001633 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001634
Chris Lattner0ef13522007-10-09 17:51:17 +00001635 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001636 // At this point, we need to lookahead to determine if this @ is the start
1637 // of an @catch or @finally. We don't want to consume the @ token if this
1638 // is an @try or @encode or something else.
1639 Token AfterAt = GetLookAheadToken(1);
1640 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1641 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1642 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001643
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001644 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001645 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001646 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001647 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001648 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001649 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001650 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001651 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001652 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001653 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001654 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001655 ParseDeclarator(ParmDecl);
1656
Douglas Gregore11ee112010-04-23 23:01:43 +00001657 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001658 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001659 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001660 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001661 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001662
Steve Naroff65a00892009-04-07 22:56:58 +00001663 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001664
Steve Naroff65a00892009-04-07 22:56:58 +00001665 if (Tok.is(tok::r_paren))
1666 RParenLoc = ConsumeParen();
1667 else // Skip over garbage, until we get to ')'. Eat the ')'.
1668 SkipUntil(tok::r_paren, true, false);
1669
John McCalldadc5752010-08-24 06:29:42 +00001670 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001671 if (Tok.is(tok::l_brace))
1672 CatchBody = ParseCompoundStatementBody();
1673 else
1674 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001675 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001676 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001677
John McCalldadc5752010-08-24 06:29:42 +00001678 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001679 RParenLoc,
1680 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001681 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001682 if (!Catch.isInvalid())
1683 CatchStmts.push_back(Catch.release());
1684
Steve Naroffe6016792008-02-05 21:27:35 +00001685 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001686 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1687 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001688 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001689 }
1690 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001691 } else {
1692 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001693 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001694 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001695
John McCalldadc5752010-08-24 06:29:42 +00001696 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001697 if (Tok.is(tok::l_brace))
1698 FinallyBody = ParseCompoundStatementBody();
1699 else
1700 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001701 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001702 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001703 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001704 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001705 catch_or_finally_seen = true;
1706 break;
1707 }
1708 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001709 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001710 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001711 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001712 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001713
John McCallb268a282010-08-23 23:25:46 +00001714 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001715 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001716 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001717}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001718
John McCall31168b02011-06-15 23:02:42 +00001719/// objc-autoreleasepool-statement:
1720/// @autoreleasepool compound-statement
1721///
1722StmtResult
1723Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1724 ConsumeToken(); // consume autoreleasepool
1725 if (Tok.isNot(tok::l_brace)) {
1726 Diag(Tok, diag::err_expected_lbrace);
1727 return StmtError();
1728 }
1729 // Enter a scope to hold everything within the compound stmt. Compound
1730 // statements can always hold declarations.
1731 ParseScope BodyScope(this, Scope::DeclScope);
1732
1733 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1734
1735 BodyScope.Exit();
1736 if (AutoreleasePoolBody.isInvalid())
1737 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1738 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1739 AutoreleasePoolBody.take());
1740}
1741
Steve Naroff09bf8152007-09-06 21:24:23 +00001742/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001743///
John McCall48871652010-08-21 09:40:31 +00001744Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001745 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001746
John McCallfaf5fb42010-08-26 23:41:50 +00001747 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1748 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001749
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001750 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001751 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001752 if (ObjCImpDecl) {
1753 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001754 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001755 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001756 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001757 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001758
Steve Naroffbb875722007-11-11 19:54:21 +00001759 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001760 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001761 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001762
Steve Naroffbb875722007-11-11 19:54:21 +00001763 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1764 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001765
Steve Naroffbb875722007-11-11 19:54:21 +00001766 // If we didn't find the '{', bail out.
1767 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001768 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001769 }
Steve Naroffbb875722007-11-11 19:54:21 +00001770 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001771
Steve Naroffbb875722007-11-11 19:54:21 +00001772 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001773 ParseScope BodyScope(this,
1774 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001775
Steve Naroffbb875722007-11-11 19:54:21 +00001776 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001777 // specified Declarator for the method.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001778 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001779
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001780 if (PP.isCodeCompletionEnabled()) {
1781 if (trySkippingFunctionBodyForCodeCompletion()) {
1782 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001783 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001784 }
1785 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001786
John McCalldadc5752010-08-24 06:29:42 +00001787 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001788
Steve Naroffbb875722007-11-11 19:54:21 +00001789 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001790 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001791 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1792 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001793
Steve Naroffbb875722007-11-11 19:54:21 +00001794 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001795 BodyScope.Exit();
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001796
1797 // TODO: Pass argument information.
1798 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff7b8fa472007-11-13 23:01:27 +00001799 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001800}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001801
John McCalldadc5752010-08-24 06:29:42 +00001802StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001803 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001804 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001805 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001806 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001807 }
1808
1809 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001810 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001811
1812 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001813 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001814
1815 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001816 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001817
1818 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1819 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001820
John McCalldadc5752010-08-24 06:29:42 +00001821 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001822 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001823 // If the expression is invalid, skip ahead to the next semicolon. Not
1824 // doing this opens us up to the possibility of infinite loops if
1825 // ParseExpression does not consume any tokens.
1826 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001827 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001828 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001829
Steve Naroffe6016792008-02-05 21:27:35 +00001830 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001831 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001832 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001833}
1834
John McCalldadc5752010-08-24 06:29:42 +00001835ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001836 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001837 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001838 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001839 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001840 return ExprError();
1841
Chris Lattnere002fbe2007-12-12 01:04:12 +00001842 case tok::string_literal: // primary-expression: string-literal
1843 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001844 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001845 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001846 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001847 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001848
Chris Lattner197a3012008-08-05 06:19:09 +00001849 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1850 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001851 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001852 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001853 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001854 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001855 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001856 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001857 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001858 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001859 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001860}
1861
Douglas Gregor8d4de672010-04-21 22:36:40 +00001862/// \brirg Parse the receiver of an Objective-C++ message send.
1863///
1864/// This routine parses the receiver of a message send in
1865/// Objective-C++ either as a type or as an expression. Note that this
1866/// routine must not be called to parse a send to 'super', since it
1867/// has no way to return such a result.
1868///
1869/// \param IsExpr Whether the receiver was parsed as an expression.
1870///
1871/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1872/// IsExpr is true), the parsed expression. If the receiver was parsed
1873/// as a type (\c IsExpr is false), the parsed type.
1874///
1875/// \returns True if an error occurred during parsing or semantic
1876/// analysis, in which case the arguments do not have valid
1877/// values. Otherwise, returns false for a successful parse.
1878///
1879/// objc-receiver: [C++]
1880/// 'super' [not parsed here]
1881/// expression
1882/// simple-type-specifier
1883/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001884bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001885 InMessageExpressionRAIIObject InMessage(*this, true);
1886
Douglas Gregor8d4de672010-04-21 22:36:40 +00001887 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1888 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1889 TryAnnotateTypeOrScopeToken();
1890
1891 if (!isCXXSimpleTypeSpecifier()) {
1892 // objc-receiver:
1893 // expression
John McCalldadc5752010-08-24 06:29:42 +00001894 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001895 if (Receiver.isInvalid())
1896 return true;
1897
1898 IsExpr = true;
1899 TypeOrExpr = Receiver.take();
1900 return false;
1901 }
1902
1903 // objc-receiver:
1904 // typename-specifier
1905 // simple-type-specifier
1906 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00001907 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001908 ParseCXXSimpleTypeSpecifier(DS);
1909
1910 if (Tok.is(tok::l_paren)) {
1911 // If we see an opening parentheses at this point, we are
1912 // actually parsing an expression that starts with a
1913 // function-style cast, e.g.,
1914 //
1915 // postfix-expression:
1916 // simple-type-specifier ( expression-list [opt] )
1917 // typename-specifier ( expression-list [opt] )
1918 //
1919 // Parse the remainder of this case, then the (optional)
1920 // postfix-expression suffix, followed by the (optional)
1921 // right-hand side of the binary expression. We have an
1922 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001923 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001924 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001925 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001926 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001927 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001928 if (Receiver.isInvalid())
1929 return true;
1930
1931 IsExpr = true;
1932 TypeOrExpr = Receiver.take();
1933 return false;
1934 }
1935
1936 // We have a class message. Turn the simple-type-specifier or
1937 // typename-specifier we parsed into a type and parse the
1938 // remainder of the class message.
1939 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001940 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001941 if (Type.isInvalid())
1942 return true;
1943
1944 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001945 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001946 return false;
1947}
1948
Douglas Gregor990ccac2010-05-31 14:40:22 +00001949/// \brief Determine whether the parser is currently referring to a an
1950/// Objective-C message send, using a simplified heuristic to avoid overhead.
1951///
1952/// This routine will only return true for a subset of valid message-send
1953/// expressions.
1954bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001955 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001956 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001957 return GetLookAheadToken(1).is(tok::identifier) &&
1958 GetLookAheadToken(2).is(tok::identifier);
1959}
1960
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001961bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1962 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1963 InMessageExpression)
1964 return false;
1965
1966
1967 ParsedType Type;
1968
1969 if (Tok.is(tok::annot_typename))
1970 Type = getTypeAnnotation(Tok);
1971 else if (Tok.is(tok::identifier))
1972 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1973 getCurScope());
1974 else
1975 return false;
1976
1977 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1978 const Token &AfterNext = GetLookAheadToken(2);
1979 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1980 if (Tok.is(tok::identifier))
1981 TryAnnotateTypeOrScopeToken();
1982
1983 return Tok.is(tok::annot_typename);
1984 }
1985 }
1986
1987 return false;
1988}
1989
Mike Stump11289f42009-09-09 15:08:12 +00001990/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001991/// '[' objc-receiver objc-message-args ']'
1992///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001993/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00001994/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001995/// expression
1996/// class-name
1997/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00001998///
John McCalldadc5752010-08-24 06:29:42 +00001999ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002000 assert(Tok.is(tok::l_square) && "'[' expected");
2001 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2002
Douglas Gregora817a192010-05-27 23:06:34 +00002003 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002004 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregora817a192010-05-27 23:06:34 +00002005 ConsumeCodeCompletionToken();
2006 SkipUntil(tok::r_square);
2007 return ExprError();
2008 }
2009
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002010 InMessageExpressionRAIIObject InMessage(*this, true);
2011
Douglas Gregor8d4de672010-04-21 22:36:40 +00002012 if (getLang().CPlusPlus) {
2013 // We completely separate the C and C++ cases because C++ requires
2014 // more complicated (read: slower) parsing.
2015
2016 // Handle send to super.
2017 // FIXME: This doesn't benefit from the same typo-correction we
2018 // get in Objective-C.
2019 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002020 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002021 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2022 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002023
2024 // Parse the receiver, which is either a type or an expression.
2025 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002026 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002027 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2028 SkipUntil(tok::r_square);
2029 return ExprError();
2030 }
2031
2032 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002033 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2034 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002035 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002036
2037 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002038 ParsedType::getFromOpaquePtr(TypeOrExpr),
2039 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002040 }
2041
2042 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002043 IdentifierInfo *Name = Tok.getIdentifierInfo();
2044 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002045 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002046 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002047 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002048 NextToken().is(tok::period),
2049 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002050 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002051 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2052 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002053
John McCallfaf5fb42010-08-26 23:41:50 +00002054 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002055 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002056 SkipUntil(tok::r_square);
2057 return ExprError();
2058 }
2059
Douglas Gregore5798dc2010-04-21 20:38:13 +00002060 ConsumeToken(); // the type name
2061
2062 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002063 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002064
John McCallfaf5fb42010-08-26 23:41:50 +00002065 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002066 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002067 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002068 }
Chris Lattner8f697062008-01-25 18:59:06 +00002069 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002070
2071 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002072 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002073 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002074 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002075 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002076 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002077
John McCallba7bf592010-08-24 05:47:05 +00002078 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2079 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002080}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002081
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002082/// \brief Parse the remainder of an Objective-C message following the
2083/// '[' objc-receiver.
2084///
2085/// This routine handles sends to super, class messages (sent to a
2086/// class name), and instance messages (sent to an object), and the
2087/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2088/// ReceiverExpr, respectively. Only one of these parameters may have
2089/// a valid value.
2090///
2091/// \param LBracLoc The location of the opening '['.
2092///
2093/// \param SuperLoc If this is a send to 'super', the location of the
2094/// 'super' keyword that indicates a send to the superclass.
2095///
2096/// \param ReceiverType If this is a class message, the type of the
2097/// class we are sending a message to.
2098///
2099/// \param ReceiverExpr If this is an instance message, the expression
2100/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002101///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002102/// objc-message-args:
2103/// objc-selector
2104/// objc-keywordarg-list
2105///
2106/// objc-keywordarg-list:
2107/// objc-keywordarg
2108/// objc-keywordarg-list objc-keywordarg
2109///
Mike Stump11289f42009-09-09 15:08:12 +00002110/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002111/// selector-name[opt] ':' objc-keywordexpr
2112///
2113/// objc-keywordexpr:
2114/// nonempty-expr-list
2115///
2116/// nonempty-expr-list:
2117/// assignment-expression
2118/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002119///
John McCalldadc5752010-08-24 06:29:42 +00002120ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002121Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002122 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002123 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002124 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002125 InMessageExpressionRAIIObject InMessage(*this, true);
2126
Steve Naroffeae65032009-11-07 02:08:14 +00002127 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002128 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002129 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2130 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002131 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002132 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2133 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002134 else
John McCallb268a282010-08-23 23:25:46 +00002135 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002136 0, 0, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002137 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00002138 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002139
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002140 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002141 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002142 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002143
Anders Carlsson978f08d2009-02-14 18:21:46 +00002144 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002145
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002146 SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002147 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002148
Chris Lattner0ef13522007-10-09 17:51:17 +00002149 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002150 while (1) {
2151 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002152 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002153
Chris Lattner0ef13522007-10-09 17:51:17 +00002154 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002155 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002156 // We must manually skip to a ']', otherwise the expression skipper will
2157 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2158 // the enclosing expression.
2159 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002160 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002161 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002162
Steve Narofff73590d2007-09-27 14:38:14 +00002163 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002164 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002165
2166 if (Tok.is(tok::code_completion)) {
2167 if (SuperLoc.isValid())
2168 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2169 KeyIdents.data(),
2170 KeyIdents.size(),
2171 /*AtArgumentEpression=*/true);
2172 else if (ReceiverType)
2173 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2174 KeyIdents.data(),
2175 KeyIdents.size(),
2176 /*AtArgumentEpression=*/true);
2177 else
2178 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2179 KeyIdents.data(),
2180 KeyIdents.size(),
2181 /*AtArgumentEpression=*/true);
2182
2183 ConsumeCodeCompletionToken();
2184 SkipUntil(tok::r_square);
2185 return ExprError();
2186 }
2187
John McCalldadc5752010-08-24 06:29:42 +00002188 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002189 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002190 // We must manually skip to a ']', otherwise the expression skipper will
2191 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2192 // the enclosing expression.
2193 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002194 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002195 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002196
Steve Naroff486760a2007-09-17 20:25:27 +00002197 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002198 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002199
Douglas Gregor1b605f72009-11-19 01:08:35 +00002200 // Code completion after each argument.
2201 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002202 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002203 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002204 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002205 KeyIdents.size(),
2206 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002207 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002208 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002209 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002210 KeyIdents.size(),
2211 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002212 else
John McCallb268a282010-08-23 23:25:46 +00002213 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002214 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002215 KeyIdents.size(),
2216 /*AtArgumentEpression=*/false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002217 ConsumeCodeCompletionToken();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002218 SkipUntil(tok::r_square);
2219 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002220 }
2221
Steve Naroff486760a2007-09-17 20:25:27 +00002222 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002223 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002224 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002225 break;
2226 // We have a selector or a colon, continue parsing.
2227 }
2228 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002229 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002230 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002231 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002232 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002233 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002234 // We must manually skip to a ']', otherwise the expression skipper will
2235 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2236 // the enclosing expression.
2237 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002238 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002239 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002240
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002241 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002242 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002243 }
2244 } else if (!selIdent) {
2245 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002246
Chris Lattner197a3012008-08-05 06:19:09 +00002247 // We must manually skip to a ']', otherwise the expression skipper will
2248 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2249 // the enclosing expression.
2250 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002251 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002252 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002253
Chris Lattner0ef13522007-10-09 17:51:17 +00002254 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002255 if (Tok.is(tok::identifier))
2256 Diag(Tok, diag::err_expected_colon);
2257 else
2258 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002259 // We must manually skip to a ']', otherwise the expression skipper will
2260 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2261 // the enclosing expression.
2262 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002263 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002264 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002265
Chris Lattner8f697062008-01-25 18:59:06 +00002266 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002267
Steve Naroffe61bfa82007-10-05 18:42:47 +00002268 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002269 if (nKeys == 0)
2270 KeyIdents.push_back(selIdent);
2271 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002272
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002273 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002274 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002275 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002276 MultiExprArg(Actions,
2277 KeyExprs.take(),
2278 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002279 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002280 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002281 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002282 MultiExprArg(Actions,
2283 KeyExprs.take(),
2284 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002285 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002286 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002287 MultiExprArg(Actions,
2288 KeyExprs.take(),
2289 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002290}
2291
John McCalldadc5752010-08-24 06:29:42 +00002292ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2293 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002294 if (Res.isInvalid()) return move(Res);
2295
Chris Lattnere002fbe2007-12-12 01:04:12 +00002296 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2297 // expressions. At this point, we know that the only valid thing that starts
2298 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002299 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002300 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002301 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002302 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002303
Chris Lattnere002fbe2007-12-12 01:04:12 +00002304 while (Tok.is(tok::at)) {
2305 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002306
Sebastian Redlc13f2682008-12-09 20:22:58 +00002307 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002308 if (!isTokenStringLiteral())
2309 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002310
John McCalldadc5752010-08-24 06:29:42 +00002311 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002312 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002313 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002314
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002315 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002316 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002317
2318 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2319 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002320}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002321
2322/// objc-encode-expression:
2323/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002324ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002325Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002326 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002327
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002328 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002329
Chris Lattner197a3012008-08-05 06:19:09 +00002330 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002331 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2332
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002333 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002334
Douglas Gregor220cac52009-02-18 17:45:20 +00002335 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002336
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002337 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002338
Douglas Gregor220cac52009-02-18 17:45:20 +00002339 if (Ty.isInvalid())
2340 return ExprError();
2341
Mike Stump11289f42009-09-09 15:08:12 +00002342 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002343 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002344}
Anders Carlssone01493d2007-08-23 15:25:28 +00002345
2346/// objc-protocol-expression
2347/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002348ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002349Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002350 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002351
Chris Lattner197a3012008-08-05 06:19:09 +00002352 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002353 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2354
Anders Carlssone01493d2007-08-23 15:25:28 +00002355 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002356
Chris Lattner197a3012008-08-05 06:19:09 +00002357 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002358 return ExprError(Diag(Tok, diag::err_expected_ident));
2359
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002360 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002361 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002362
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002363 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002364
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002365 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2366 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002367}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002368
2369/// objc-selector-expression
2370/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002371ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002372 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002373
Chris Lattner197a3012008-08-05 06:19:09 +00002374 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002375 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002377 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002378 SourceLocation LParenLoc = ConsumeParen();
2379 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002380
2381 if (Tok.is(tok::code_completion)) {
2382 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2383 KeyIdents.size());
2384 ConsumeCodeCompletionToken();
2385 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2386 return ExprError();
2387 }
2388
Chris Lattner4f472a32009-04-11 18:13:45 +00002389 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002390 if (!SelIdent && // missing selector name.
2391 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002392 return ExprError(Diag(Tok, diag::err_expected_ident));
2393
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002394 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002395 unsigned nColons = 0;
2396 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002397 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002398 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2399 ++nColons;
2400 KeyIdents.push_back(0);
2401 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002402 return ExprError(Diag(Tok, diag::err_expected_colon));
2403
Chris Lattner1ba64452010-08-27 22:32:41 +00002404 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002405 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002406 if (Tok.is(tok::r_paren))
2407 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002408
2409 if (Tok.is(tok::code_completion)) {
2410 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2411 KeyIdents.size());
2412 ConsumeCodeCompletionToken();
2413 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2414 return ExprError();
2415 }
2416
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002417 // Check for another keyword selector.
2418 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002419 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002420 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002421 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002422 break;
2423 }
Steve Naroff152dd812007-12-05 22:21:29 +00002424 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002425 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002426 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002427 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2428 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002429 }