blob: 9cfe73456a5f3b04bd68f646337edc03b6538502 [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
14#include "clang/Parse/Parser.h"
Steve Narofff1bc45b2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian9e63b982007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000019using namespace clang;
20
21
Chris Lattner3a907162008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattner83f095c2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000032
Douglas Gregorf48706c2009-12-07 09:27:33 +000033 if (Tok.is(tok::code_completion)) {
34 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +000035 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +000036 }
37
Steve Naroff7c348172007-08-23 18:16:40 +000038 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000039 case tok::objc_class:
40 return ParseObjCAtClassDeclaration(AtLoc);
41 case tok::objc_interface:
42 return ParseObjCAtInterfaceDeclaration(AtLoc);
43 case tok::objc_protocol:
44 return ParseObjCAtProtocolDeclaration(AtLoc);
45 case tok::objc_implementation:
46 return ParseObjCAtImplementationDeclaration(AtLoc);
47 case tok::objc_end:
48 return ParseObjCAtEndDeclaration(AtLoc);
49 case tok::objc_compatibility_alias:
50 return ParseObjCAtAliasDeclaration(AtLoc);
51 case tok::objc_synthesize:
52 return ParseObjCPropertySynthesize(AtLoc);
53 case tok::objc_dynamic:
54 return ParseObjCPropertyDynamic(AtLoc);
55 default:
56 Diag(AtLoc, diag::err_unexpected_at);
57 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000058 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000059 }
60}
61
62///
Mike Stump11289f42009-09-09 15:08:12 +000063/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000064/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000065///
Chris Lattner83f095c2009-03-28 19:18:32 +000066Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000067 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000068 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000069 llvm::SmallVector<SourceLocation, 8> ClassLocs;
70
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerda59c2f2006-11-05 02:08:13 +000072 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000073 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000074 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000076 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000079 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner0ef13522007-10-09 17:51:17 +000082 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 break;
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerda59c2f2006-11-05 02:08:13 +000085 ConsumeToken();
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnerda59c2f2006-11-05 02:08:13 +000088 // Consume the ';'.
89 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000090 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000091
Ted Kremeneka26da852009-11-17 23:12:20 +000092 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93 ClassLocs.data(),
94 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
Steve Naroff1eb1ad62007-08-20 21:31:48 +000097///
98/// objc-interface:
99/// objc-class-interface-attributes[opt] objc-class-interface
100/// objc-category-interface
101///
102/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000103/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000104/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000105/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000106/// objc-interface-decl-list
107/// @end
108///
109/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000110/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000111/// objc-protocol-refs[opt]
112/// objc-interface-decl-list
113/// @end
114///
115/// objc-superclass:
116/// ':' identifier
117///
118/// objc-class-interface-attributes:
119/// __attribute__((visibility("default")))
120/// __attribute__((visibility("hidden")))
121/// __attribute__((deprecated))
122/// __attribute__((unavailable))
123/// __attribute__((objc_exception)) - used by NSException on 64-bit
124///
Chris Lattner83f095c2009-03-28 19:18:32 +0000125Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000126 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000127 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000128 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregor49c22a72009-11-18 16:26:39 +0000131 // Code completion after '@interface'.
132 if (Tok.is(tok::code_completion)) {
133 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000134 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000135 }
136
Chris Lattner0ef13522007-10-09 17:51:17 +0000137 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000139 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000140 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000141
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000143 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000145 if (Tok.is(tok::l_paren) &&
146 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000150 if (Tok.is(tok::code_completion)) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000151 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000152 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000153 }
154
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000155 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000156 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000157 categoryId = Tok.getIdentifierInfo();
158 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000159 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000160 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000161 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000164 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 Diag(Tok, diag::err_expected_rparen);
166 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000167 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 }
169 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000170 // Next, we need to check for any protocol references.
171 SourceLocation LAngleLoc, EndProtoLoc;
172 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
173 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
174 if (Tok.is(tok::less) &&
175 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000176 LAngleLoc, EndProtoLoc))
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000177 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000178
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000179 if (attrList) // categories don't support attributes.
180 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000181
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000182 DeclPtrTy CategoryType =
183 Actions.ActOnStartCategoryInterface(atLoc,
184 nameId, nameLoc,
185 categoryId, categoryLoc,
186 ProtocolRefs.data(),
187 ProtocolRefs.size(),
188 ProtocolLocs.data(),
189 EndProtoLoc);
190 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000191 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
192 atLoc);
193
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000194 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
195 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000196 }
197 // Parse a class interface.
198 IdentifierInfo *superClassId = 0;
199 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000200
Chris Lattner0ef13522007-10-09 17:51:17 +0000201 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000203
204 // Code completion of superclass names.
205 if (Tok.is(tok::code_completion)) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000206 Actions.CodeCompleteObjCSuperclass(CurScope, nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000207 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000208 }
209
Chris Lattner0ef13522007-10-09 17:51:17 +0000210 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000211 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000212 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000213 }
214 superClassId = Tok.getIdentifierInfo();
215 superClassLoc = ConsumeToken();
216 }
217 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000218 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000219 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
220 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000221 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000222 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
223 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000224 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000225
226 DeclPtrTy ClsType =
227 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000228 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000229 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000230 ProtocolLocs.data(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000231 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattner0ef13522007-10-09 17:51:17 +0000233 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000234 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000235
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000236 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000237 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000238}
239
John McCall064d77b2009-12-03 22:31:13 +0000240/// The Objective-C property callback. This should be defined where
241/// it's used, but instead it's been lifted to here to support VS2005.
242struct Parser::ObjCPropertyCallback : FieldCallback {
243 Parser &P;
244 DeclPtrTy IDecl;
245 llvm::SmallVectorImpl<DeclPtrTy> &Props;
246 ObjCDeclSpec &OCDS;
247 SourceLocation AtLoc;
248 tok::ObjCKeywordKind MethodImplKind;
249
250 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
251 llvm::SmallVectorImpl<DeclPtrTy> &Props,
252 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
253 tok::ObjCKeywordKind MethodImplKind) :
254 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
255 MethodImplKind(MethodImplKind) {
256 }
257
258 DeclPtrTy invoke(FieldDeclarator &FD) {
259 if (FD.D.getIdentifier() == 0) {
260 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
261 << FD.D.getSourceRange();
262 return DeclPtrTy();
263 }
264 if (FD.BitfieldSize) {
265 P.Diag(AtLoc, diag::err_objc_property_bitfield)
266 << FD.D.getSourceRange();
267 return DeclPtrTy();
268 }
269
270 // Install the property declarator into interfaceDecl.
271 IdentifierInfo *SelName =
272 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
273
274 Selector GetterSel =
275 P.PP.getSelectorTable().getNullarySelector(SelName);
276 IdentifierInfo *SetterName = OCDS.getSetterName();
277 Selector SetterSel;
278 if (SetterName)
279 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
280 else
281 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
282 P.PP.getSelectorTable(),
283 FD.D.getIdentifier());
284 bool isOverridingProperty = false;
285 DeclPtrTy Property =
286 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
287 GetterSel, SetterSel, IDecl,
288 &isOverridingProperty,
289 MethodImplKind);
290 if (!isOverridingProperty)
291 Props.push_back(Property);
292
293 return Property;
294 }
295};
296
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000297/// objc-interface-decl-list:
298/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000299/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000300/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000301/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000302/// objc-interface-decl-list declaration
303/// objc-interface-decl-list ';'
304///
Steve Naroff99264b42007-08-22 16:35:03 +0000305/// objc-method-requirement: [OBJC2]
306/// @required
307/// @optional
308///
Chris Lattner83f095c2009-03-28 19:18:32 +0000309void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000310 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000311 llvm::SmallVector<DeclPtrTy, 32> allMethods;
312 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000313 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000314 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenekc7c64312010-01-07 01:20:12 +0000316 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000317
Steve Naroff99264b42007-08-22 16:35:03 +0000318 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000319 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000320 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000321 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000322 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000323 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000324 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
325 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000326 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
327 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000328 continue;
329 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000330 if (Tok.is(tok::l_paren)) {
331 Diag(Tok, diag::err_expected_minus_or_plus);
332 DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(),
333 tok::minus,
334 interfaceDecl,
335 MethodImplKind);
336 continue;
337 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000338 // Ignore excess semicolons.
339 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000340 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000341 continue;
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattnerda9fb152008-10-20 06:10:06 +0000344 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000345 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000346 break;
Mike Stump11289f42009-09-09 15:08:12 +0000347
Douglas Gregorf1934162010-01-13 21:24:21 +0000348 // Code completion within an Objective-C interface.
349 if (Tok.is(tok::code_completion)) {
350 Actions.CodeCompleteOrdinaryName(CurScope,
351 ObjCImpDecl? Action::CCC_ObjCImplementation
352 : Action::CCC_ObjCInterface);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000353 ConsumeCodeCompletionToken();
Douglas Gregorf1934162010-01-13 21:24:21 +0000354 }
355
Chris Lattner038a3e32008-10-20 05:46:22 +0000356 // If we don't have an @ directive, parse it as a function definition.
357 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000358 // The code below does not consume '}'s because it is afraid of eating the
359 // end of a namespace. Because of the way this code is structured, an
360 // erroneous r_brace would cause an infinite loop if not handled here.
361 if (Tok.is(tok::r_brace))
362 break;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Steve Narofff1bc45b2007-08-22 18:35:33 +0000364 // FIXME: as the name implies, this rule allows function definitions.
365 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000366 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000367 continue;
368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
Chris Lattner038a3e32008-10-20 05:46:22 +0000370 // Otherwise, we have an @ directive, eat the @.
371 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000372 if (Tok.is(tok::code_completion)) {
373 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000374 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000375 break;
376 }
377
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000378 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000380 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000381 AtEnd.setBegin(AtLoc);
382 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000383 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000384 } else if (DirectiveKind == tok::objc_not_keyword) {
385 Diag(Tok, diag::err_objc_unknown_at);
386 SkipUntil(tok::semi);
387 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattnerda9fb152008-10-20 06:10:06 +0000390 // Eat the identifier.
391 ConsumeToken();
392
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000393 switch (DirectiveKind) {
394 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000395 // FIXME: If someone forgets an @end on a protocol, this loop will
396 // continue to eat up tons of stuff and spew lots of nonsense errors. It
397 // would probably be better to bail out if we saw an @class or @interface
398 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000399 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000400 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000401 SkipUntil(tok::r_brace, tok::at);
402 break;
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000404 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000405 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000406 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000407 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000408 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000409 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000410 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000411 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000412 break;
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000414 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000415 if (!getLang().ObjC2)
416 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
417
Chris Lattner038a3e32008-10-20 05:46:22 +0000418 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000419 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000420 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000421 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
422 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000423
John McCall064d77b2009-12-03 22:31:13 +0000424 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
425 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000426
Chris Lattner038a3e32008-10-20 05:46:22 +0000427 // Parse all the comma separated declarators.
428 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000429 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000431 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
432 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000433 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000434 }
Steve Naroff99264b42007-08-22 16:35:03 +0000435 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000436
437 // We break out of the big loop in two cases: when we see @end or when we see
438 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000439 if (Tok.is(tok::code_completion)) {
440 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000441 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000442 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000443 ConsumeToken(); // the "end" identifier
444 else
445 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000447 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000448 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000449 Actions.ActOnAtEnd(CurScope, AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000450 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000451 allProperties.data(), allProperties.size(),
452 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000453}
454
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000455/// Parse property attribute declarations.
456///
457/// property-attr-decl: '(' property-attrlist ')'
458/// property-attrlist:
459/// property-attribute
460/// property-attrlist ',' property-attribute
461/// property-attribute:
462/// getter '=' identifier
463/// setter '=' identifier ':'
464/// readonly
465/// readwrite
466/// assign
467/// retain
468/// copy
469/// nonatomic
470///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000471void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
472 DeclPtrTy *Methods,
473 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000474 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000475 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000477 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000478 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000479 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000480 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000481 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000482 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner76619232008-10-20 07:22:18 +0000484 // If this is not an identifier at all, bail out early.
485 if (II == 0) {
486 MatchRHSPunctuation(tok::r_paren, LHSLoc);
487 return;
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner1db33542008-10-20 07:37:22 +0000490 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattner68e48682008-11-20 04:42:34 +0000492 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000493 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000494 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000495 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000496 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000497 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000498 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000499 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000500 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000502 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000504 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000505 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000506 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
507 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000508 return;
Mike Stump11289f42009-09-09 15:08:12 +0000509
Douglas Gregorc8537c52009-11-19 07:41:15 +0000510 if (Tok.is(tok::code_completion)) {
511 if (II->getNameStart()[0] == 's')
512 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
513 Methods, NumMethods);
514 else
515 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
516 Methods, NumMethods);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000517 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000518 }
519
Chris Lattnerbeca7702008-10-20 07:24:39 +0000520 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000521 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000522 SkipUntil(tok::r_paren);
523 return;
524 }
Mike Stump11289f42009-09-09 15:08:12 +0000525
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000526 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000527 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
528 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000529 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000530
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000531 if (ExpectAndConsume(tok::colon,
532 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000533 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000534 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000535 } else {
536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
537 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000538 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000539 }
Chris Lattner825bca12008-10-20 07:39:53 +0000540 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000541 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000542 SkipUntil(tok::r_paren);
543 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattner1db33542008-10-20 07:37:22 +0000546 if (Tok.isNot(tok::comma))
547 break;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattner1db33542008-10-20 07:37:22 +0000549 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner1db33542008-10-20 07:37:22 +0000552 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000553}
554
Steve Naroff09bf8152007-09-06 21:24:23 +0000555/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000556/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000557/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000558///
559/// objc-instance-method: '-'
560/// objc-class-method: '+'
561///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000562/// objc-method-attributes: [OBJC2]
563/// __attribute__((deprecated))
564///
Mike Stump11289f42009-09-09 15:08:12 +0000565Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000566 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000567 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000568
Mike Stump11289f42009-09-09 15:08:12 +0000569 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000570 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000571
Chris Lattner83f095c2009-03-28 19:18:32 +0000572 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000573 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000574 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000575 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000576}
577
578/// objc-selector:
579/// identifier
580/// one of
581/// enum struct union if else while do for switch case default
582/// break continue return goto asm sizeof typeof __alignof
583/// unsigned long const short volatile signed restrict _Complex
584/// in out inout bycopy byref oneway int char float double void _Bool
585///
Chris Lattner4f472a32009-04-11 18:13:45 +0000586IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000587 switch (Tok.getKind()) {
588 default:
589 return 0;
590 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000591 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000592 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000593 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000594 case tok::kw_break:
595 case tok::kw_case:
596 case tok::kw_catch:
597 case tok::kw_char:
598 case tok::kw_class:
599 case tok::kw_const:
600 case tok::kw_const_cast:
601 case tok::kw_continue:
602 case tok::kw_default:
603 case tok::kw_delete:
604 case tok::kw_do:
605 case tok::kw_double:
606 case tok::kw_dynamic_cast:
607 case tok::kw_else:
608 case tok::kw_enum:
609 case tok::kw_explicit:
610 case tok::kw_export:
611 case tok::kw_extern:
612 case tok::kw_false:
613 case tok::kw_float:
614 case tok::kw_for:
615 case tok::kw_friend:
616 case tok::kw_goto:
617 case tok::kw_if:
618 case tok::kw_inline:
619 case tok::kw_int:
620 case tok::kw_long:
621 case tok::kw_mutable:
622 case tok::kw_namespace:
623 case tok::kw_new:
624 case tok::kw_operator:
625 case tok::kw_private:
626 case tok::kw_protected:
627 case tok::kw_public:
628 case tok::kw_register:
629 case tok::kw_reinterpret_cast:
630 case tok::kw_restrict:
631 case tok::kw_return:
632 case tok::kw_short:
633 case tok::kw_signed:
634 case tok::kw_sizeof:
635 case tok::kw_static:
636 case tok::kw_static_cast:
637 case tok::kw_struct:
638 case tok::kw_switch:
639 case tok::kw_template:
640 case tok::kw_this:
641 case tok::kw_throw:
642 case tok::kw_true:
643 case tok::kw_try:
644 case tok::kw_typedef:
645 case tok::kw_typeid:
646 case tok::kw_typename:
647 case tok::kw_typeof:
648 case tok::kw_union:
649 case tok::kw_unsigned:
650 case tok::kw_using:
651 case tok::kw_virtual:
652 case tok::kw_void:
653 case tok::kw_volatile:
654 case tok::kw_wchar_t:
655 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000656 case tok::kw__Bool:
657 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000658 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000659 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000660 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000661 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000662 }
Steve Naroff99264b42007-08-22 16:35:03 +0000663}
664
Fariborz Jahanian83615522008-01-02 22:54:34 +0000665/// objc-for-collection-in: 'in'
666///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000667bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000668 // FIXME: May have to do additional look-ahead to only allow for
669 // valid tokens following an 'in'; such as an identifier, unary operators,
670 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000671 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000672 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000673}
674
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000675/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000676/// qualifier list and builds their bitmask representation in the input
677/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000678///
679/// objc-type-qualifiers:
680/// objc-type-qualifier
681/// objc-type-qualifiers objc-type-qualifier
682///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000683void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000684 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000685 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000686 return;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattner61511e12007-12-12 06:56:32 +0000688 const IdentifierInfo *II = Tok.getIdentifierInfo();
689 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000690 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000691 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000693 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000694 switch (i) {
695 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000696 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
697 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
698 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
699 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
700 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
701 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000702 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000703 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000704 ConsumeToken();
705 II = 0;
706 break;
707 }
Mike Stump11289f42009-09-09 15:08:12 +0000708
Chris Lattner61511e12007-12-12 06:56:32 +0000709 // If this wasn't a recognized qualifier, bail out.
710 if (II) return;
711 }
712}
713
714/// objc-type-name:
715/// '(' objc-type-qualifiers[opt] type-name ')'
716/// '(' objc-type-qualifiers[opt] ')'
717///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000718Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000719 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000720
Chris Lattnerb7954432008-10-22 03:52:06 +0000721 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000722 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000723
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000724 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000725 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000726
Chris Lattnerb7954432008-10-22 03:52:06 +0000727 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000728 if (isTypeSpecifierQualifier()) {
729 TypeResult TypeSpec = ParseTypeName();
730 if (!TypeSpec.isInvalid())
731 Ty = TypeSpec.get();
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Steve Naroff90255b42008-10-21 14:15:04 +0000734 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000735 ConsumeParen();
736 else if (Tok.getLocation() == TypeStartLoc) {
737 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000738 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000739 SkipUntil(tok::r_paren);
740 } else {
741 // Otherwise, we found *something*, but didn't get a ')' in the right
742 // place. Emit an error then return what we have as the type.
743 MatchRHSPunctuation(tok::r_paren, LParenLoc);
744 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000745 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000746}
747
748/// objc-method-decl:
749/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000750/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000751/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000752/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000753///
754/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000755/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000756/// objc-keyword-selector objc-keyword-decl
757///
758/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000759/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
760/// objc-selector ':' objc-keyword-attributes[opt] identifier
761/// ':' objc-type-name objc-keyword-attributes[opt] identifier
762/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000763///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000764/// objc-parmlist:
765/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000766///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000767/// objc-parms:
768/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000769///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000770/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000771/// , ...
772///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000773/// objc-keyword-attributes: [OBJC2]
774/// __attribute__((unused))
775///
Chris Lattner83f095c2009-03-28 19:18:32 +0000776Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
777 tok::TokenKind mType,
778 DeclPtrTy IDecl,
779 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000780 ParsingDeclRAIIObject PD(*this);
781
Douglas Gregor636a61e2010-04-07 00:21:17 +0000782 if (Tok.is(tok::code_completion)) {
783 Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus,
784 /*ReturnType=*/0, IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000785 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000786 }
787
Chris Lattner2ebb1782008-08-23 01:48:03 +0000788 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000789 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000790 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000791 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000792 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000794 // If attributes exist before the method, parse them.
795 llvm::OwningPtr<AttributeList> MethodAttrs;
796 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
797 MethodAttrs.reset(ParseGNUAttributes());
798
Douglas Gregor636a61e2010-04-07 00:21:17 +0000799 if (Tok.is(tok::code_completion)) {
800 Actions.CodeCompleteObjCMethodDecl(CurScope, mType == tok::minus,
801 ReturnType, IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000802 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000803 }
804
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000805 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000806 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000807 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000808
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000809 // An unnamed colon is valid.
810 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000811 Diag(Tok, diag::err_expected_selector_for_method)
812 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000813 // Skip until we get a ; or {}.
814 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000815 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Fariborz Jahanian60462092010-04-08 00:30:06 +0000818 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000819 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000820 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000821 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000822 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
823 ParseGNUAttributes()));
Mike Stump11289f42009-09-09 15:08:12 +0000824
Chris Lattner5700fab2007-10-07 02:00:24 +0000825 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000826 DeclPtrTy Result
827 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000828 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000829 0,
830 CParamInfo.data(), CParamInfo.size(),
831 MethodAttrs.get(),
Ted Kremenekbc76c722009-05-04 17:04:30 +0000832 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000833 PD.complete(Result);
834 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000835 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000836
Steve Narofff73590d2007-09-27 14:38:14 +0000837 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000838 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000839
Chris Lattner5700fab2007-10-07 02:00:24 +0000840 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000841 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattner5700fab2007-10-07 02:00:24 +0000843 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000844 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000845 Diag(Tok, diag::err_expected_colon);
846 break;
847 }
848 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000850 ArgInfo.Type = 0;
851 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
852 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
853
Chris Lattner5700fab2007-10-07 02:00:24 +0000854 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000855 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000856 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000857 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000858
Chris Lattner0ef13522007-10-09 17:51:17 +0000859 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000860 Diag(Tok, diag::err_expected_ident); // missing argument name.
861 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000864 ArgInfo.Name = Tok.getIdentifierInfo();
865 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000866 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000867
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000868 ArgInfos.push_back(ArgInfo);
869 KeyIdents.push_back(SelIdent);
870
Chris Lattner5700fab2007-10-07 02:00:24 +0000871 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000872 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000873 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000874 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000875 break;
876 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000877 }
Mike Stump11289f42009-09-09 15:08:12 +0000878
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000879 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner5700fab2007-10-07 02:00:24 +0000881 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000882 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000883 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000884 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000885 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000886 ConsumeToken();
887 break;
888 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000889 DeclSpec DS;
890 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000891 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000892 Declarator ParmDecl(DS, Declarator::PrototypeContext);
893 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000894 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
895 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
896 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
897 ParmDecl.getIdentifierLoc(),
898 Param,
899 0));
900
Chris Lattner5700fab2007-10-07 02:00:24 +0000901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattner5700fab2007-10-07 02:00:24 +0000903 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000904 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000905 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000906 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
907 ParseGNUAttributes()));
Mike Stump11289f42009-09-09 15:08:12 +0000908
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000909 if (KeyIdents.size() == 0)
910 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000911 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
912 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000913 DeclPtrTy Result
914 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000915 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000916 &ArgInfos[0],
917 CParamInfo.data(), CParamInfo.size(),
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000918 MethodAttrs.get(),
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000919 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000920 PD.complete(Result);
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000921
922 // Delete referenced AttributeList objects.
923 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
924 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
925 delete I->ArgAttrs;
926
John McCall28a6aea2009-11-04 02:18:39 +0000927 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000928}
929
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000930/// objc-protocol-refs:
931/// '<' identifier-list '>'
932///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000933bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000934ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000935 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
936 bool WarnOnDeclarations,
937 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000938 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000939
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000940 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattner3bbae002008-07-26 04:03:38 +0000942 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000943
Chris Lattner3bbae002008-07-26 04:03:38 +0000944 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000945 if (Tok.is(tok::code_completion)) {
946 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
947 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000948 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +0000949 }
950
Chris Lattner3bbae002008-07-26 04:03:38 +0000951 if (Tok.isNot(tok::identifier)) {
952 Diag(Tok, diag::err_expected_ident);
953 SkipUntil(tok::greater);
954 return true;
955 }
956 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
957 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000958 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000959 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000960
Chris Lattner3bbae002008-07-26 04:03:38 +0000961 if (Tok.isNot(tok::comma))
962 break;
963 ConsumeToken();
964 }
Mike Stump11289f42009-09-09 15:08:12 +0000965
Chris Lattner3bbae002008-07-26 04:03:38 +0000966 // Consume the '>'.
967 if (Tok.isNot(tok::greater)) {
968 Diag(Tok, diag::err_expected_greater);
969 return true;
970 }
Mike Stump11289f42009-09-09 15:08:12 +0000971
Chris Lattner3bbae002008-07-26 04:03:38 +0000972 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000973
Chris Lattner3bbae002008-07-26 04:03:38 +0000974 // Convert the list of protocols identifiers into a list of protocol decls.
975 Actions.FindProtocolDeclaration(WarnOnDeclarations,
976 &ProtocolIdents[0], ProtocolIdents.size(),
977 Protocols);
978 return false;
979}
980
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000981/// objc-class-instance-variables:
982/// '{' objc-instance-variable-decl-list[opt] '}'
983///
984/// objc-instance-variable-decl-list:
985/// objc-visibility-spec
986/// objc-instance-variable-decl ';'
987/// ';'
988/// objc-instance-variable-decl-list objc-visibility-spec
989/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
990/// objc-instance-variable-decl-list ';'
991///
992/// objc-visibility-spec:
993/// @private
994/// @protected
995/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000996/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000997///
998/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000999/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001000///
Chris Lattner83f095c2009-03-28 19:18:32 +00001001void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001002 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001003 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001004 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +00001005 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001006
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001007 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001008
Steve Naroff00433d32007-08-21 21:17:12 +00001009 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001010
Steve Naroff00433d32007-08-21 21:17:12 +00001011 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001012 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001013 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001014
Steve Naroff00433d32007-08-21 21:17:12 +00001015 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001016 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +00001017 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001018 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001019 ConsumeToken();
1020 continue;
1021 }
Mike Stump11289f42009-09-09 15:08:12 +00001022
Steve Naroff00433d32007-08-21 21:17:12 +00001023 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001024 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001025 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001026
1027 if (Tok.is(tok::code_completion)) {
1028 Actions.CodeCompleteObjCAtVisibility(CurScope);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001029 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001030 }
1031
Steve Naroff7c348172007-08-23 18:16:40 +00001032 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001033 case tok::objc_private:
1034 case tok::objc_public:
1035 case tok::objc_protected:
1036 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001037 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001038 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001039 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001040 default:
1041 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001042 continue;
1043 }
1044 }
Mike Stump11289f42009-09-09 15:08:12 +00001045
Douglas Gregor48d46252010-01-13 21:54:15 +00001046 if (Tok.is(tok::code_completion)) {
1047 Actions.CodeCompleteOrdinaryName(CurScope,
1048 Action::CCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001049 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001050 }
1051
John McCallcfefb6d2009-11-03 02:38:08 +00001052 struct ObjCIvarCallback : FieldCallback {
1053 Parser &P;
1054 DeclPtrTy IDecl;
1055 tok::ObjCKeywordKind visibility;
1056 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1057
1058 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1059 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1060 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1061 }
1062
1063 DeclPtrTy invoke(FieldDeclarator &FD) {
1064 // Install the declarator into the interface decl.
1065 DeclPtrTy Field
1066 = P.Actions.ActOnIvar(P.CurScope,
1067 FD.D.getDeclSpec().getSourceRange().getBegin(),
1068 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001069 if (Field)
1070 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001071 return Field;
1072 }
1073 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1074
Chris Lattnera12405b2008-04-10 06:46:29 +00001075 // Parse all the comma separated declarators.
1076 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001077 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattner0ef13522007-10-09 17:51:17 +00001079 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001080 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001081 } else {
1082 Diag(Tok, diag::err_expected_semi_decl_list);
1083 // Skip to end of block or statement
1084 SkipUntil(tok::r_brace, true, true);
1085 }
1086 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001087 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001088 // Call ActOnFields() even if we don't have any decls. This is useful
1089 // for code rewriting tools that need to be aware of the empty list.
1090 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001091 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001092 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001093 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001094}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001095
1096/// objc-protocol-declaration:
1097/// objc-protocol-definition
1098/// objc-protocol-forward-reference
1099///
1100/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001101/// @protocol identifier
1102/// objc-protocol-refs[opt]
1103/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001104/// @end
1105///
1106/// objc-protocol-forward-reference:
1107/// @protocol identifier-list ';'
1108///
1109/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001110/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001111/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001112Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1113 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001114 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001115 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1116 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001117
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001118 if (Tok.is(tok::code_completion)) {
1119 Actions.CodeCompleteObjCProtocolDecl(CurScope);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001120 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001121 }
1122
Chris Lattner0ef13522007-10-09 17:51:17 +00001123 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001124 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001125 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001126 }
1127 // Save the protocol name, then consume it.
1128 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1129 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001130
Chris Lattner0ef13522007-10-09 17:51:17 +00001131 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001132 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001133 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001134 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001135 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Chris Lattner0ef13522007-10-09 17:51:17 +00001138 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001139 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1140 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1141
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001142 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001143 while (1) {
1144 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001145 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001146 Diag(Tok, diag::err_expected_ident);
1147 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001148 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001149 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001150 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1151 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001152 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner0ef13522007-10-09 17:51:17 +00001154 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001155 break;
1156 }
1157 // Consume the ';'.
1158 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001159 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001160
Steve Naroff93eb5f12007-10-10 17:32:04 +00001161 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001162 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001163 ProtocolRefs.size(),
1164 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001165 }
Mike Stump11289f42009-09-09 15:08:12 +00001166
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001167 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001168 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001169
Chris Lattner83f095c2009-03-28 19:18:32 +00001170 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001171 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001172 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001173 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1174 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001175 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001176
Chris Lattner83f095c2009-03-28 19:18:32 +00001177 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001178 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001179 ProtocolRefs.data(),
1180 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001181 ProtocolLocs.data(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001182 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001183 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001184 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001185}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001186
1187/// objc-implementation:
1188/// objc-class-implementation-prologue
1189/// objc-category-implementation-prologue
1190///
1191/// objc-class-implementation-prologue:
1192/// @implementation identifier objc-superclass[opt]
1193/// objc-class-instance-variables[opt]
1194///
1195/// objc-category-implementation-prologue:
1196/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001197Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001198 SourceLocation atLoc) {
1199 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1200 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1201 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001202
Douglas Gregor49c22a72009-11-18 16:26:39 +00001203 // Code completion after '@implementation'.
1204 if (Tok.is(tok::code_completion)) {
1205 Actions.CodeCompleteObjCImplementationDecl(CurScope);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001206 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001207 }
1208
Chris Lattner0ef13522007-10-09 17:51:17 +00001209 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001210 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001211 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001212 }
1213 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001214 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001215 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001216
1217 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001218 // we have a category implementation.
1219 SourceLocation lparenLoc = ConsumeParen();
1220 SourceLocation categoryLoc, rparenLoc;
1221 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001222
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001223 if (Tok.is(tok::code_completion)) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001224 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001225 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001226 }
1227
Chris Lattner0ef13522007-10-09 17:51:17 +00001228 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001229 categoryId = Tok.getIdentifierInfo();
1230 categoryLoc = ConsumeToken();
1231 } else {
1232 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001233 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001234 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001235 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001236 Diag(Tok, diag::err_expected_rparen);
1237 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001238 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001239 }
1240 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001241 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001242 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001243 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001244 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001245 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001246 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001247 }
1248 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001249 SourceLocation superClassLoc;
1250 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001251 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001252 // We have a super class
1253 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001254 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001255 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001256 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001257 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001258 superClassId = Tok.getIdentifierInfo();
1259 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001260 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001261 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001262 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001263 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001264
Steve Naroff33a1e802007-10-29 21:38:07 +00001265 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001266 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahaniana9effb52010-03-22 19:04:14 +00001267 tok::objc_private, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001268 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001269 PendingObjCImpDecl.push_back(ObjCImpDecl);
1270
Chris Lattner83f095c2009-03-28 19:18:32 +00001271 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001272}
Steve Naroff33a1e802007-10-29 21:38:07 +00001273
Ted Kremenekc7c64312010-01-07 01:20:12 +00001274Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001275 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1276 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001277 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001278 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001279 if (ObjCImpDecl) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001280 Actions.ActOnAtEnd(CurScope, atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001281 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001282 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001283 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001284 else {
1285 // missing @implementation
1286 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1287 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001288 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001289}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001290
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001291Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001292 if (PendingObjCImpDecl.empty())
1293 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1294 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001295 Actions.ActOnAtEnd(CurScope, SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001296 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1297}
1298
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001299/// compatibility-alias-decl:
1300/// @compatibility_alias alias-name class-name ';'
1301///
Chris Lattner83f095c2009-03-28 19:18:32 +00001302Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001303 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1304 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1305 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001306 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001307 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001308 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001309 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001310 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1311 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001312 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001313 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001314 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001315 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001316 IdentifierInfo *classId = Tok.getIdentifierInfo();
1317 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1318 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001319 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001320 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001321 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001322 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1323 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001324}
1325
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001326/// property-synthesis:
1327/// @synthesize property-ivar-list ';'
1328///
1329/// property-ivar-list:
1330/// property-ivar
1331/// property-ivar-list ',' property-ivar
1332///
1333/// property-ivar:
1334/// identifier
1335/// identifier '=' identifier
1336///
Chris Lattner83f095c2009-03-28 19:18:32 +00001337Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001338 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1339 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001340 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001341
Douglas Gregor88e72a02009-11-18 19:45:45 +00001342 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001343 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001344 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001345 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001346 }
1347
Douglas Gregor88e72a02009-11-18 19:45:45 +00001348 if (Tok.isNot(tok::identifier)) {
1349 Diag(Tok, diag::err_synthesized_property_name);
1350 SkipUntil(tok::semi);
1351 return DeclPtrTy();
1352 }
1353
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001354 IdentifierInfo *propertyIvar = 0;
1355 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1356 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001357 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001358 // property '=' ivar-name
1359 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001360
1361 if (Tok.is(tok::code_completion)) {
1362 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1363 ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001364 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001365 }
1366
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001368 Diag(Tok, diag::err_expected_ident);
1369 break;
1370 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001371 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001372 ConsumeToken(); // consume ivar-name
1373 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001374 Actions.ActOnPropertyImplDecl(CurScope, atLoc, propertyLoc, true, ObjCImpDecl,
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001375 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001376 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001377 break;
1378 ConsumeToken(); // consume ','
1379 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001380 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001381 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001382 SkipUntil(tok::semi);
1383 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001384 else
1385 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001386 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001387}
1388
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001389/// property-dynamic:
1390/// @dynamic property-list
1391///
1392/// property-list:
1393/// identifier
1394/// property-list ',' identifier
1395///
Chris Lattner83f095c2009-03-28 19:18:32 +00001396Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001397 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1398 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1399 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001400 while (true) {
1401 if (Tok.is(tok::code_completion)) {
1402 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001403 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001404 }
1405
1406 if (Tok.isNot(tok::identifier)) {
1407 Diag(Tok, diag::err_expected_ident);
1408 SkipUntil(tok::semi);
1409 return DeclPtrTy();
1410 }
1411
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001412 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1413 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001414 Actions.ActOnPropertyImplDecl(CurScope, atLoc, propertyLoc, false, ObjCImpDecl,
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001415 propertyId, 0);
1416
Chris Lattner0ef13522007-10-09 17:51:17 +00001417 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001418 break;
1419 ConsumeToken(); // consume ','
1420 }
Fariborz Jahanianb5795c02010-04-14 20:52:42 +00001421 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001422 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianb5795c02010-04-14 20:52:42 +00001423 SkipUntil(tok::semi);
1424 }
1425 else
1426 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001427 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001428}
Mike Stump11289f42009-09-09 15:08:12 +00001429
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001430/// objc-throw-statement:
1431/// throw expression[opt];
1432///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001433Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001434 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001435 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001436 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001437 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001438 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001439 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001440 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001441 }
1442 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001443 // consume ';'
1444 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
Steve Naroff5ee2c022009-02-11 20:05:44 +00001445 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001446}
1447
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001448/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001449/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001450///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001451Parser::OwningStmtResult
1452Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001453 ConsumeToken(); // consume synchronized
1454 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001455 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001456 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001457 }
1458 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001459 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001460 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001461 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001462 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001463 }
1464 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001465 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001466 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001467 }
1468 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001469 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001470 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001471 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001472 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001473 // Enter a scope to hold everything within the compound stmt. Compound
1474 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001475 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001476
Sebastian Redl042ad952008-12-11 19:30:53 +00001477 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001478
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001479 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001480 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001481 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001482 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001483}
1484
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001485/// objc-try-catch-statement:
1486/// @try compound-statement objc-catch-list[opt]
1487/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1488///
1489/// objc-catch-list:
1490/// @catch ( parameter-declaration ) compound-statement
1491/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1492/// catch-parameter-declaration:
1493/// parameter-declaration
1494/// '...' [OBJC2]
1495///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001496Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001497 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001498
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001499 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001500 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001501 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001502 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001503 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001504 StmtVector CatchStmts(Actions);
Sebastian Redlc13f2682008-12-09 20:22:58 +00001505 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001506 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001507 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001508 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001509 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001510 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001511
Chris Lattner0ef13522007-10-09 17:51:17 +00001512 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001513 // At this point, we need to lookahead to determine if this @ is the start
1514 // of an @catch or @finally. We don't want to consume the @ token if this
1515 // is an @try or @encode or something else.
1516 Token AfterAt = GetLookAheadToken(1);
1517 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1518 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1519 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001520
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001521 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001522 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001523 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001524 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001525 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001526 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001527 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001528 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001529 DeclSpec DS;
1530 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001531 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001532 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001533 // we must accept either 'declarator' or 'abstract-declarator' here.
1534 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1535 ParseDeclarator(ParmDecl);
1536
Douglas Gregore11ee112010-04-23 23:01:43 +00001537 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001538 // gets added to the current scope.
Douglas Gregore11ee112010-04-23 23:01:43 +00001539 FirstPart = Actions.ActOnObjCExceptionDecl(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001540 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001541 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001542
Steve Naroff65a00892009-04-07 22:56:58 +00001543 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001544
Steve Naroff65a00892009-04-07 22:56:58 +00001545 if (Tok.is(tok::r_paren))
1546 RParenLoc = ConsumeParen();
1547 else // Skip over garbage, until we get to ')'. Eat the ')'.
1548 SkipUntil(tok::r_paren, true, false);
1549
Sebastian Redlc13f2682008-12-09 20:22:58 +00001550 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001551 if (Tok.is(tok::l_brace))
1552 CatchBody = ParseCompoundStatementBody();
1553 else
1554 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001555 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001556 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001557
1558 OwningStmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
1559 RParenLoc,
1560 FirstPart,
1561 move(CatchBody));
1562 if (!Catch.isInvalid())
1563 CatchStmts.push_back(Catch.release());
1564
Steve Naroffe6016792008-02-05 21:27:35 +00001565 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001566 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1567 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001568 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001569 }
1570 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001571 } else {
1572 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001573 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001574 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001575
Sebastian Redlc13f2682008-12-09 20:22:58 +00001576 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001577 if (Tok.is(tok::l_brace))
1578 FinallyBody = ParseCompoundStatementBody();
1579 else
1580 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001581 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001582 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001583 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001584 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001585 catch_or_finally_seen = true;
1586 break;
1587 }
1588 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001589 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001590 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001591 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001592 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001593
1594 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody),
1595 move_arg(CatchStmts),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001596 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001597}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001598
Steve Naroff09bf8152007-09-06 21:24:23 +00001599/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001600///
Chris Lattner83f095c2009-03-28 19:18:32 +00001601Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1602 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001603
Chris Lattnereae6cb62009-03-05 08:00:35 +00001604 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1605 PP.getSourceManager(),
1606 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001607
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001608 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001609 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001610 if (ObjCImpDecl) {
1611 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001612 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001613 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001614 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001615 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001616
Steve Naroffbb875722007-11-11 19:54:21 +00001617 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001618 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001619 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001620
Steve Naroffbb875722007-11-11 19:54:21 +00001621 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1622 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001623
Steve Naroffbb875722007-11-11 19:54:21 +00001624 // If we didn't find the '{', bail out.
1625 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001626 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001627 }
Steve Naroffbb875722007-11-11 19:54:21 +00001628 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001629
Steve Naroffbb875722007-11-11 19:54:21 +00001630 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001631 ParseScope BodyScope(this,
1632 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001633
Steve Naroffbb875722007-11-11 19:54:21 +00001634 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001635 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001636 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001637
1638 OwningStmtResult FnBody(ParseCompoundStatementBody());
1639
Steve Naroffbb875722007-11-11 19:54:21 +00001640 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001641 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001642 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1643 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001644
Steve Naroffb94d7f62009-03-02 22:00:56 +00001645 // TODO: Pass argument information.
1646 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001647
Steve Naroffbb875722007-11-11 19:54:21 +00001648 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001649 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001650
Steve Naroff7b8fa472007-11-13 23:01:27 +00001651 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001652}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001653
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001654Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001655 if (Tok.is(tok::code_completion)) {
1656 Actions.CodeCompleteObjCAtStatement(CurScope);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001657 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001658 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001659 }
1660
1661 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001662 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001663
1664 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001665 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001666
1667 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001668 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001669
Sebastian Redl90893182008-12-11 22:33:27 +00001670 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001671 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001672 // If the expression is invalid, skip ahead to the next semicolon. Not
1673 // doing this opens us up to the possibility of infinite loops if
1674 // ParseExpression does not consume any tokens.
1675 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001676 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001677 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001678
Steve Naroffe6016792008-02-05 21:27:35 +00001679 // Otherwise, eat the semicolon.
1680 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001681 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001682}
1683
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001684Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001685 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001686 case tok::code_completion:
1687 Actions.CodeCompleteObjCAtExpression(CurScope);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001688 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001689 return ExprError();
1690
Chris Lattnere002fbe2007-12-12 01:04:12 +00001691 case tok::string_literal: // primary-expression: string-literal
1692 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001693 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001694 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001695 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001696 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001697
Chris Lattner197a3012008-08-05 06:19:09 +00001698 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1699 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001700 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001701 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001702 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001703 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001704 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001705 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001706 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001707 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001708 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001709}
1710
Douglas Gregor8d4de672010-04-21 22:36:40 +00001711/// \brirg Parse the receiver of an Objective-C++ message send.
1712///
1713/// This routine parses the receiver of a message send in
1714/// Objective-C++ either as a type or as an expression. Note that this
1715/// routine must not be called to parse a send to 'super', since it
1716/// has no way to return such a result.
1717///
1718/// \param IsExpr Whether the receiver was parsed as an expression.
1719///
1720/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1721/// IsExpr is true), the parsed expression. If the receiver was parsed
1722/// as a type (\c IsExpr is false), the parsed type.
1723///
1724/// \returns True if an error occurred during parsing or semantic
1725/// analysis, in which case the arguments do not have valid
1726/// values. Otherwise, returns false for a successful parse.
1727///
1728/// objc-receiver: [C++]
1729/// 'super' [not parsed here]
1730/// expression
1731/// simple-type-specifier
1732/// typename-specifier
1733
1734bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
1735 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1736 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1737 TryAnnotateTypeOrScopeToken();
1738
1739 if (!isCXXSimpleTypeSpecifier()) {
1740 // objc-receiver:
1741 // expression
1742 OwningExprResult Receiver = ParseExpression();
1743 if (Receiver.isInvalid())
1744 return true;
1745
1746 IsExpr = true;
1747 TypeOrExpr = Receiver.take();
1748 return false;
1749 }
1750
1751 // objc-receiver:
1752 // typename-specifier
1753 // simple-type-specifier
1754 // expression (that starts with one of the above)
1755 DeclSpec DS;
1756 ParseCXXSimpleTypeSpecifier(DS);
1757
1758 if (Tok.is(tok::l_paren)) {
1759 // If we see an opening parentheses at this point, we are
1760 // actually parsing an expression that starts with a
1761 // function-style cast, e.g.,
1762 //
1763 // postfix-expression:
1764 // simple-type-specifier ( expression-list [opt] )
1765 // typename-specifier ( expression-list [opt] )
1766 //
1767 // Parse the remainder of this case, then the (optional)
1768 // postfix-expression suffix, followed by the (optional)
1769 // right-hand side of the binary expression. We have an
1770 // instance method.
1771 OwningExprResult Receiver = ParseCXXTypeConstructExpression(DS);
1772 if (!Receiver.isInvalid())
1773 Receiver = ParsePostfixExpressionSuffix(move(Receiver));
1774 if (!Receiver.isInvalid())
1775 Receiver = ParseRHSOfBinaryExpression(move(Receiver), prec::Comma);
1776 if (Receiver.isInvalid())
1777 return true;
1778
1779 IsExpr = true;
1780 TypeOrExpr = Receiver.take();
1781 return false;
1782 }
1783
1784 // We have a class message. Turn the simple-type-specifier or
1785 // typename-specifier we parsed into a type and parse the
1786 // remainder of the class message.
1787 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1788 TypeResult Type = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
1789 if (Type.isInvalid())
1790 return true;
1791
1792 IsExpr = false;
1793 TypeOrExpr = Type.get();
1794 return false;
1795}
1796
Mike Stump11289f42009-09-09 15:08:12 +00001797/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001798/// '[' objc-receiver objc-message-args ']'
1799///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001800/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00001801/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001802/// expression
1803/// class-name
1804/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00001805///
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001806Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001807 assert(Tok.is(tok::l_square) && "'[' expected");
1808 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1809
Douglas Gregor8d4de672010-04-21 22:36:40 +00001810 if (getLang().CPlusPlus) {
1811 // We completely separate the C and C++ cases because C++ requires
1812 // more complicated (read: slower) parsing.
1813
1814 // Handle send to super.
1815 // FIXME: This doesn't benefit from the same typo-correction we
1816 // get in Objective-C.
1817 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
1818 NextToken().isNot(tok::period) && CurScope->isInObjcMethodScope())
1819 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
1820 ExprArg(Actions));
1821
1822 // Parse the receiver, which is either a type or an expression.
1823 bool IsExpr;
1824 void *TypeOrExpr;
1825 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1826 SkipUntil(tok::r_square);
1827 return ExprError();
1828 }
1829
1830 if (IsExpr)
1831 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
1832 OwningExprResult(Actions, TypeOrExpr));
1833
1834 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1835 TypeOrExpr, ExprArg(Actions));
1836 } else if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00001837 IdentifierInfo *Name = Tok.getIdentifierInfo();
1838 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregore5798dc2010-04-21 20:38:13 +00001839 TypeTy *ReceiverType;
Douglas Gregora148a1d2010-04-14 02:22:16 +00001840 switch (Actions.getObjCMessageKind(CurScope, Name, NameLoc,
1841 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00001842 NextToken().is(tok::period),
1843 ReceiverType)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00001844 case Action::ObjCSuperMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001845 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001846 ExprArg(Actions));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001847
Douglas Gregore5798dc2010-04-21 20:38:13 +00001848 case Action::ObjCClassMessage:
1849 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001850 SkipUntil(tok::r_square);
1851 return ExprError();
1852 }
1853
Douglas Gregore5798dc2010-04-21 20:38:13 +00001854 ConsumeToken(); // the type name
1855
1856 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1857 ReceiverType,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001858 ExprArg(Actions));
Douglas Gregora148a1d2010-04-14 02:22:16 +00001859
1860 case Action::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001861 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00001862 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001863 }
Chris Lattner8f697062008-01-25 18:59:06 +00001864 }
Chris Lattnera36ec422010-04-11 08:28:14 +00001865
1866 // Otherwise, an arbitrary expression can be the receiver of a send.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001867 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001868 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001869 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001870 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001871 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001872
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001873 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
1874 move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001875}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001876
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001877/// \brief Parse the remainder of an Objective-C message following the
1878/// '[' objc-receiver.
1879///
1880/// This routine handles sends to super, class messages (sent to a
1881/// class name), and instance messages (sent to an object), and the
1882/// target is represented by \p SuperLoc, \p ReceiverType, or \p
1883/// ReceiverExpr, respectively. Only one of these parameters may have
1884/// a valid value.
1885///
1886/// \param LBracLoc The location of the opening '['.
1887///
1888/// \param SuperLoc If this is a send to 'super', the location of the
1889/// 'super' keyword that indicates a send to the superclass.
1890///
1891/// \param ReceiverType If this is a class message, the type of the
1892/// class we are sending a message to.
1893///
1894/// \param ReceiverExpr If this is an instance message, the expression
1895/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00001896///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001897/// objc-message-args:
1898/// objc-selector
1899/// objc-keywordarg-list
1900///
1901/// objc-keywordarg-list:
1902/// objc-keywordarg
1903/// objc-keywordarg-list objc-keywordarg
1904///
Mike Stump11289f42009-09-09 15:08:12 +00001905/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001906/// selector-name[opt] ':' objc-keywordexpr
1907///
1908/// objc-keywordexpr:
1909/// nonempty-expr-list
1910///
1911/// nonempty-expr-list:
1912/// assignment-expression
1913/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001914///
1915Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001916Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001917 SourceLocation SuperLoc,
1918 TypeTy *ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001919 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001920 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001921 if (SuperLoc.isValid())
1922 Actions.CodeCompleteObjCSuperMessage(CurScope, SuperLoc, 0, 0);
1923 else if (ReceiverType)
1924 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverType, 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001925 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001926 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1927 0, 0);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001928 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00001929 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001930
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001931 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001932 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001933 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001934
Anders Carlsson978f08d2009-02-14 18:21:46 +00001935 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001936
Steve Narofff73590d2007-09-27 14:38:14 +00001937 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001938 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001939
Chris Lattner0ef13522007-10-09 17:51:17 +00001940 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001941 while (1) {
1942 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001943 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001944
Chris Lattner0ef13522007-10-09 17:51:17 +00001945 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001946 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001947 // We must manually skip to a ']', otherwise the expression skipper will
1948 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1949 // the enclosing expression.
1950 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001951 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001952 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001953
Steve Narofff73590d2007-09-27 14:38:14 +00001954 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001955 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001956 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001957 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001958 // We must manually skip to a ']', otherwise the expression skipper will
1959 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1960 // the enclosing expression.
1961 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001962 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001963 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001964
Steve Naroff486760a2007-09-17 20:25:27 +00001965 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001966 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001967
Douglas Gregor1b605f72009-11-19 01:08:35 +00001968 // Code completion after each argument.
1969 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001970 if (SuperLoc.isValid())
1971 Actions.CodeCompleteObjCSuperMessage(CurScope, SuperLoc,
1972 KeyIdents.data(),
1973 KeyIdents.size());
1974 else if (ReceiverType)
1975 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00001976 KeyIdents.data(),
1977 KeyIdents.size());
1978 else
1979 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1980 KeyIdents.data(),
1981 KeyIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001982 ConsumeCodeCompletionToken();
Douglas Gregor1b605f72009-11-19 01:08:35 +00001983 }
1984
Steve Naroff486760a2007-09-17 20:25:27 +00001985 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001986 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001987 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001988 break;
1989 // We have a selector or a colon, continue parsing.
1990 }
1991 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001992 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001993 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001994 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001995 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001996 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001997 // We must manually skip to a ']', otherwise the expression skipper will
1998 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1999 // the enclosing expression.
2000 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002001 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002002 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002003
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002004 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002005 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002006 }
2007 } else if (!selIdent) {
2008 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002009
Chris Lattner197a3012008-08-05 06:19:09 +00002010 // We must manually skip to a ']', otherwise the expression skipper will
2011 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2012 // the enclosing expression.
2013 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002014 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002015 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002016
Chris Lattner0ef13522007-10-09 17:51:17 +00002017 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002018 if (Tok.is(tok::identifier))
2019 Diag(Tok, diag::err_expected_colon);
2020 else
2021 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002022 // We must manually skip to a ']', otherwise the expression skipper will
2023 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2024 // the enclosing expression.
2025 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002026 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002027 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002028
Chris Lattner8f697062008-01-25 18:59:06 +00002029 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002030
Steve Naroffe61bfa82007-10-05 18:42:47 +00002031 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002032 if (nKeys == 0)
2033 KeyIdents.push_back(selIdent);
2034 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002035
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002036 if (SuperLoc.isValid())
2037 return Actions.ActOnSuperMessage(CurScope, SuperLoc, Sel,
2038 LBracLoc, SelectorLoc, RBracLoc,
2039 Action::MultiExprArg(Actions,
2040 KeyExprs.take(),
2041 KeyExprs.size()));
2042 else if (ReceiverType)
2043 return Actions.ActOnClassMessage(CurScope, ReceiverType, Sel,
2044 LBracLoc, SelectorLoc, RBracLoc,
2045 Action::MultiExprArg(Actions,
2046 KeyExprs.take(),
2047 KeyExprs.size()));
2048 return Actions.ActOnInstanceMessage(CurScope, move(ReceiverExpr), Sel,
2049 LBracLoc, SelectorLoc, RBracLoc,
2050 Action::MultiExprArg(Actions,
2051 KeyExprs.take(),
2052 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002053}
2054
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002055Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00002056 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002057 if (Res.isInvalid()) return move(Res);
2058
Chris Lattnere002fbe2007-12-12 01:04:12 +00002059 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2060 // expressions. At this point, we know that the only valid thing that starts
2061 // with '@' is an @"".
2062 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002063 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002064 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002065 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002066
Chris Lattnere002fbe2007-12-12 01:04:12 +00002067 while (Tok.is(tok::at)) {
2068 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002069
Sebastian Redlc13f2682008-12-09 20:22:58 +00002070 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002071 if (!isTokenStringLiteral())
2072 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002073
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002074 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002075 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002076 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002077
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002078 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002079 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002080
2081 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2082 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002083}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002084
2085/// objc-encode-expression:
2086/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002087Parser::OwningExprResult
2088Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002089 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002090
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002091 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002092
Chris Lattner197a3012008-08-05 06:19:09 +00002093 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002094 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2095
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002096 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002097
Douglas Gregor220cac52009-02-18 17:45:20 +00002098 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002099
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002100 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002101
Douglas Gregor220cac52009-02-18 17:45:20 +00002102 if (Ty.isInvalid())
2103 return ExprError();
2104
Mike Stump11289f42009-09-09 15:08:12 +00002105 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002106 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002107}
Anders Carlssone01493d2007-08-23 15:25:28 +00002108
2109/// objc-protocol-expression
2110/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002111Parser::OwningExprResult
2112Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002113 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002114
Chris Lattner197a3012008-08-05 06:19:09 +00002115 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002116 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2117
Anders Carlssone01493d2007-08-23 15:25:28 +00002118 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002119
Chris Lattner197a3012008-08-05 06:19:09 +00002120 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002121 return ExprError(Diag(Tok, diag::err_expected_ident));
2122
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002123 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002124 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002125
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002126 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002127
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002128 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2129 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002130}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002131
2132/// objc-selector-expression
2133/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002134Parser::OwningExprResult
2135Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002136 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002137
Chris Lattner197a3012008-08-05 06:19:09 +00002138 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002139 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2140
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002141 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002142 SourceLocation LParenLoc = ConsumeParen();
2143 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002144 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002145 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
2146 return ExprError(Diag(Tok, diag::err_expected_ident));
2147
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002148 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002149 unsigned nColons = 0;
2150 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002151 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00002152 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002153 return ExprError(Diag(Tok, diag::err_expected_colon));
2154
Chris Lattner5e530bc2007-12-27 19:57:00 +00002155 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002156 ConsumeToken(); // Eat the ':'.
2157 if (Tok.is(tok::r_paren))
2158 break;
2159 // Check for another keyword selector.
2160 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002161 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002162 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002163 if (!SelIdent && Tok.isNot(tok::colon))
2164 break;
2165 }
Steve Naroff152dd812007-12-05 22:21:29 +00002166 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002167 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002168 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002169 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2170 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002171 }