blob: 123a96261ad5d8891f359104dadd424f87a954a2 [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);
35 ConsumeToken();
36 }
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);
134 ConsumeToken();
135 }
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 Jahaniand077f712010-04-02 23:15:40 +0000145 bool Err = false;
Chris Lattner0ef13522007-10-09 17:51:17 +0000146 if (Tok.is(tok::l_paren)) { // 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)) {
151 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
152 ConsumeToken();
153 }
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 }
160 else if (isKnownToBeTypeSpecifier(Tok)) {
161 // Fall thru after diagnosing for better error recovery.
162 Diag(Tok, diag::err_expected_minus_or_plus);
163 ConsumeToken();
164 Err = true;
165 }
166 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000167 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000168 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000169 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000170 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 Diag(Tok, diag::err_expected_rparen);
172 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000173 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000174 }
175 rparenLoc = ConsumeParen();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000176 if (!Err) {
177 // Next, we need to check for any protocol references.
178 SourceLocation LAngleLoc, EndProtoLoc;
179 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
180 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
181 if (Tok.is(tok::less) &&
182 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000183 LAngleLoc, EndProtoLoc))
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000184 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000185
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000186 if (attrList) // categories don't support attributes.
187 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000188
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000189 DeclPtrTy CategoryType =
190 Actions.ActOnStartCategoryInterface(atLoc,
191 nameId, nameLoc,
192 categoryId, categoryLoc,
193 ProtocolRefs.data(),
194 ProtocolRefs.size(),
195 ProtocolLocs.data(),
196 EndProtoLoc);
197 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000198 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
199 atLoc);
200
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000201 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
202 return CategoryType;
203 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000204 }
205 // Parse a class interface.
206 IdentifierInfo *superClassId = 0;
207 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000208
Chris Lattner0ef13522007-10-09 17:51:17 +0000209 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000211
212 // Code completion of superclass names.
213 if (Tok.is(tok::code_completion)) {
214 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
215 ConsumeToken();
216 }
217
Chris Lattner0ef13522007-10-09 17:51:17 +0000218 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000220 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 }
222 superClassId = Tok.getIdentifierInfo();
223 superClassLoc = ConsumeToken();
224 }
225 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000226 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000227 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
228 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000229 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000230 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
231 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000232 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000233
234 DeclPtrTy ClsType =
235 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000236 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000237 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000238 ProtocolLocs.data(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000239 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner0ef13522007-10-09 17:51:17 +0000241 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000242 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000243
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000244 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000245 return Err ? DeclPtrTy() : ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000246}
247
John McCall064d77b2009-12-03 22:31:13 +0000248/// The Objective-C property callback. This should be defined where
249/// it's used, but instead it's been lifted to here to support VS2005.
250struct Parser::ObjCPropertyCallback : FieldCallback {
251 Parser &P;
252 DeclPtrTy IDecl;
253 llvm::SmallVectorImpl<DeclPtrTy> &Props;
254 ObjCDeclSpec &OCDS;
255 SourceLocation AtLoc;
256 tok::ObjCKeywordKind MethodImplKind;
257
258 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
259 llvm::SmallVectorImpl<DeclPtrTy> &Props,
260 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
261 tok::ObjCKeywordKind MethodImplKind) :
262 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
263 MethodImplKind(MethodImplKind) {
264 }
265
266 DeclPtrTy invoke(FieldDeclarator &FD) {
267 if (FD.D.getIdentifier() == 0) {
268 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
269 << FD.D.getSourceRange();
270 return DeclPtrTy();
271 }
272 if (FD.BitfieldSize) {
273 P.Diag(AtLoc, diag::err_objc_property_bitfield)
274 << FD.D.getSourceRange();
275 return DeclPtrTy();
276 }
277
278 // Install the property declarator into interfaceDecl.
279 IdentifierInfo *SelName =
280 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
281
282 Selector GetterSel =
283 P.PP.getSelectorTable().getNullarySelector(SelName);
284 IdentifierInfo *SetterName = OCDS.getSetterName();
285 Selector SetterSel;
286 if (SetterName)
287 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
288 else
289 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
290 P.PP.getSelectorTable(),
291 FD.D.getIdentifier());
292 bool isOverridingProperty = false;
293 DeclPtrTy Property =
294 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
295 GetterSel, SetterSel, IDecl,
296 &isOverridingProperty,
297 MethodImplKind);
298 if (!isOverridingProperty)
299 Props.push_back(Property);
300
301 return Property;
302 }
303};
304
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000305/// objc-interface-decl-list:
306/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000307/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000308/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000309/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000310/// objc-interface-decl-list declaration
311/// objc-interface-decl-list ';'
312///
Steve Naroff99264b42007-08-22 16:35:03 +0000313/// objc-method-requirement: [OBJC2]
314/// @required
315/// @optional
316///
Chris Lattner83f095c2009-03-28 19:18:32 +0000317void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000318 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000319 llvm::SmallVector<DeclPtrTy, 32> allMethods;
320 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000321 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000322 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenekc7c64312010-01-07 01:20:12 +0000324 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000325
Steve Naroff99264b42007-08-22 16:35:03 +0000326 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000327 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000328 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000329 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000331 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000332 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
333 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000334 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
335 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000336 continue;
337 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000338 if (Tok.is(tok::l_paren)) {
339 Diag(Tok, diag::err_expected_minus_or_plus);
340 DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(),
341 tok::minus,
342 interfaceDecl,
343 MethodImplKind);
344 continue;
345 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000346 // Ignore excess semicolons.
347 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000348 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000349 continue;
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattnerda9fb152008-10-20 06:10:06 +0000352 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000353 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000354 break;
Mike Stump11289f42009-09-09 15:08:12 +0000355
Douglas Gregorf1934162010-01-13 21:24:21 +0000356 // Code completion within an Objective-C interface.
357 if (Tok.is(tok::code_completion)) {
358 Actions.CodeCompleteOrdinaryName(CurScope,
359 ObjCImpDecl? Action::CCC_ObjCImplementation
360 : Action::CCC_ObjCInterface);
361 ConsumeToken();
362 }
363
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 // If we don't have an @ directive, parse it as a function definition.
365 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000366 // The code below does not consume '}'s because it is afraid of eating the
367 // end of a namespace. Because of the way this code is structured, an
368 // erroneous r_brace would cause an infinite loop if not handled here.
369 if (Tok.is(tok::r_brace))
370 break;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Steve Narofff1bc45b2007-08-22 18:35:33 +0000372 // FIXME: as the name implies, this rule allows function definitions.
373 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000374 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000375 continue;
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner038a3e32008-10-20 05:46:22 +0000378 // Otherwise, we have an @ directive, eat the @.
379 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000380 if (Tok.is(tok::code_completion)) {
381 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
382 ConsumeToken();
383 break;
384 }
385
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000386 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000387
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000388 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000389 AtEnd.setBegin(AtLoc);
390 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000391 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000392 } else if (DirectiveKind == tok::objc_not_keyword) {
393 Diag(Tok, diag::err_objc_unknown_at);
394 SkipUntil(tok::semi);
395 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattnerda9fb152008-10-20 06:10:06 +0000398 // Eat the identifier.
399 ConsumeToken();
400
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000401 switch (DirectiveKind) {
402 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000403 // FIXME: If someone forgets an @end on a protocol, this loop will
404 // continue to eat up tons of stuff and spew lots of nonsense errors. It
405 // would probably be better to bail out if we saw an @class or @interface
406 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000407 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000408 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000409 SkipUntil(tok::r_brace, tok::at);
410 break;
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000412 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000413 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000414 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000415 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000416 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000417 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000419 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000420 break;
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000422 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000423 if (!getLang().ObjC2)
424 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
425
Chris Lattner038a3e32008-10-20 05:46:22 +0000426 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000427 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000428 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000429 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
430 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000431
John McCall064d77b2009-12-03 22:31:13 +0000432 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
433 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000434
Chris Lattner038a3e32008-10-20 05:46:22 +0000435 // Parse all the comma separated declarators.
436 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000437 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000438
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000439 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
440 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000441 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000442 }
Steve Naroff99264b42007-08-22 16:35:03 +0000443 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000444
445 // We break out of the big loop in two cases: when we see @end or when we see
446 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000447 if (Tok.is(tok::code_completion)) {
448 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
449 ConsumeToken();
450 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000451 ConsumeToken(); // the "end" identifier
452 else
453 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000455 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000456 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenekc7c64312010-01-07 01:20:12 +0000457 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000458 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000459 allProperties.data(), allProperties.size(),
460 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000461}
462
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000463/// Parse property attribute declarations.
464///
465/// property-attr-decl: '(' property-attrlist ')'
466/// property-attrlist:
467/// property-attribute
468/// property-attrlist ',' property-attribute
469/// property-attribute:
470/// getter '=' identifier
471/// setter '=' identifier ':'
472/// readonly
473/// readwrite
474/// assign
475/// retain
476/// copy
477/// nonatomic
478///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000479void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
480 DeclPtrTy *Methods,
481 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000482 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000483 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000485 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000486 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000487 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroff936354c2009-10-08 21:55:05 +0000488 ConsumeToken();
489 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000490 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattner76619232008-10-20 07:22:18 +0000492 // If this is not an identifier at all, bail out early.
493 if (II == 0) {
494 MatchRHSPunctuation(tok::r_paren, LHSLoc);
495 return;
496 }
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattner1db33542008-10-20 07:37:22 +0000498 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000499
Chris Lattner68e48682008-11-20 04:42:34 +0000500 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000502 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000504 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000506 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000508 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000510 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000512 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000513 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000514 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
515 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000516 return;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Douglas Gregorc8537c52009-11-19 07:41:15 +0000518 if (Tok.is(tok::code_completion)) {
519 if (II->getNameStart()[0] == 's')
520 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
521 Methods, NumMethods);
522 else
523 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
524 Methods, NumMethods);
525 ConsumeToken();
526 }
527
Chris Lattnerbeca7702008-10-20 07:24:39 +0000528 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000529 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000530 SkipUntil(tok::r_paren);
531 return;
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000534 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000535 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
536 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000537 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000538
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000539 if (ExpectAndConsume(tok::colon,
540 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000541 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000542 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000543 } else {
544 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
545 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000546 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000547 }
Chris Lattner825bca12008-10-20 07:39:53 +0000548 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000549 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000550 SkipUntil(tok::r_paren);
551 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner1db33542008-10-20 07:37:22 +0000554 if (Tok.isNot(tok::comma))
555 break;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattner1db33542008-10-20 07:37:22 +0000557 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000558 }
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner1db33542008-10-20 07:37:22 +0000560 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000561}
562
Steve Naroff09bf8152007-09-06 21:24:23 +0000563/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000564/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000565/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000566///
567/// objc-instance-method: '-'
568/// objc-class-method: '+'
569///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000570/// objc-method-attributes: [OBJC2]
571/// __attribute__((deprecated))
572///
Mike Stump11289f42009-09-09 15:08:12 +0000573Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000574 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000575 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000576
Mike Stump11289f42009-09-09 15:08:12 +0000577 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000578 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000579
Chris Lattner83f095c2009-03-28 19:18:32 +0000580 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000581 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000582 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000583 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000584}
585
586/// objc-selector:
587/// identifier
588/// one of
589/// enum struct union if else while do for switch case default
590/// break continue return goto asm sizeof typeof __alignof
591/// unsigned long const short volatile signed restrict _Complex
592/// in out inout bycopy byref oneway int char float double void _Bool
593///
Chris Lattner4f472a32009-04-11 18:13:45 +0000594IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000595 switch (Tok.getKind()) {
596 default:
597 return 0;
598 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000599 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000600 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000601 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000602 case tok::kw_break:
603 case tok::kw_case:
604 case tok::kw_catch:
605 case tok::kw_char:
606 case tok::kw_class:
607 case tok::kw_const:
608 case tok::kw_const_cast:
609 case tok::kw_continue:
610 case tok::kw_default:
611 case tok::kw_delete:
612 case tok::kw_do:
613 case tok::kw_double:
614 case tok::kw_dynamic_cast:
615 case tok::kw_else:
616 case tok::kw_enum:
617 case tok::kw_explicit:
618 case tok::kw_export:
619 case tok::kw_extern:
620 case tok::kw_false:
621 case tok::kw_float:
622 case tok::kw_for:
623 case tok::kw_friend:
624 case tok::kw_goto:
625 case tok::kw_if:
626 case tok::kw_inline:
627 case tok::kw_int:
628 case tok::kw_long:
629 case tok::kw_mutable:
630 case tok::kw_namespace:
631 case tok::kw_new:
632 case tok::kw_operator:
633 case tok::kw_private:
634 case tok::kw_protected:
635 case tok::kw_public:
636 case tok::kw_register:
637 case tok::kw_reinterpret_cast:
638 case tok::kw_restrict:
639 case tok::kw_return:
640 case tok::kw_short:
641 case tok::kw_signed:
642 case tok::kw_sizeof:
643 case tok::kw_static:
644 case tok::kw_static_cast:
645 case tok::kw_struct:
646 case tok::kw_switch:
647 case tok::kw_template:
648 case tok::kw_this:
649 case tok::kw_throw:
650 case tok::kw_true:
651 case tok::kw_try:
652 case tok::kw_typedef:
653 case tok::kw_typeid:
654 case tok::kw_typename:
655 case tok::kw_typeof:
656 case tok::kw_union:
657 case tok::kw_unsigned:
658 case tok::kw_using:
659 case tok::kw_virtual:
660 case tok::kw_void:
661 case tok::kw_volatile:
662 case tok::kw_wchar_t:
663 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000664 case tok::kw__Bool:
665 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000666 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000667 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000668 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000669 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000670 }
Steve Naroff99264b42007-08-22 16:35:03 +0000671}
672
Fariborz Jahanian83615522008-01-02 22:54:34 +0000673/// objc-for-collection-in: 'in'
674///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000675bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000676 // FIXME: May have to do additional look-ahead to only allow for
677 // valid tokens following an 'in'; such as an identifier, unary operators,
678 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000679 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000680 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000681}
682
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000683/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000684/// qualifier list and builds their bitmask representation in the input
685/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000686///
687/// objc-type-qualifiers:
688/// objc-type-qualifier
689/// objc-type-qualifiers objc-type-qualifier
690///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000691void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000692 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000693 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000694 return;
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattner61511e12007-12-12 06:56:32 +0000696 const IdentifierInfo *II = Tok.getIdentifierInfo();
697 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000698 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000699 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000700
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000701 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000702 switch (i) {
703 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000704 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
705 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
706 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
707 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
708 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
709 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000710 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000711 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000712 ConsumeToken();
713 II = 0;
714 break;
715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattner61511e12007-12-12 06:56:32 +0000717 // If this wasn't a recognized qualifier, bail out.
718 if (II) return;
719 }
720}
721
722/// objc-type-name:
723/// '(' objc-type-qualifiers[opt] type-name ')'
724/// '(' objc-type-qualifiers[opt] ')'
725///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000726Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000727 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattnerb7954432008-10-22 03:52:06 +0000729 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000730 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000731
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000732 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000733 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000734
Chris Lattnerb7954432008-10-22 03:52:06 +0000735 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000736 if (isTypeSpecifierQualifier()) {
737 TypeResult TypeSpec = ParseTypeName();
738 if (!TypeSpec.isInvalid())
739 Ty = TypeSpec.get();
740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Steve Naroff90255b42008-10-21 14:15:04 +0000742 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000743 ConsumeParen();
744 else if (Tok.getLocation() == TypeStartLoc) {
745 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000746 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000747 SkipUntil(tok::r_paren);
748 } else {
749 // Otherwise, we found *something*, but didn't get a ')' in the right
750 // place. Emit an error then return what we have as the type.
751 MatchRHSPunctuation(tok::r_paren, LParenLoc);
752 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000753 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000754}
755
756/// objc-method-decl:
757/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000758/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000759/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000760/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000761///
762/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000763/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000764/// objc-keyword-selector objc-keyword-decl
765///
766/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000767/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
768/// objc-selector ':' objc-keyword-attributes[opt] identifier
769/// ':' objc-type-name objc-keyword-attributes[opt] identifier
770/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000771///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000772/// objc-parmlist:
773/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000774///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000775/// objc-parms:
776/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000777///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000778/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000779/// , ...
780///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000781/// objc-keyword-attributes: [OBJC2]
782/// __attribute__((unused))
783///
Chris Lattner83f095c2009-03-28 19:18:32 +0000784Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
785 tok::TokenKind mType,
786 DeclPtrTy IDecl,
787 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000788 ParsingDeclRAIIObject PD(*this);
789
Chris Lattner2ebb1782008-08-23 01:48:03 +0000790 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000791 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000792 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000793 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000794 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000796 // If attributes exist before the method, parse them.
797 llvm::OwningPtr<AttributeList> MethodAttrs;
798 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
799 MethodAttrs.reset(ParseGNUAttributes());
800
801 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000802 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000803 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000804
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000805 // An unnamed colon is valid.
806 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000807 Diag(Tok, diag::err_expected_selector_for_method)
808 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000809 // Skip until we get a ; or {}.
810 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000811 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000812 }
Mike Stump11289f42009-09-09 15:08:12 +0000813
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000814 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000815 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000816 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000817 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000818 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
819 ParseGNUAttributes()));
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattner5700fab2007-10-07 02:00:24 +0000821 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000822 DeclPtrTy Result
823 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000824 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000825 0, CargNames, MethodAttrs.get(),
Ted Kremenekbc76c722009-05-04 17:04:30 +0000826 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000827 PD.complete(Result);
828 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000829 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000830
Steve Narofff73590d2007-09-27 14:38:14 +0000831 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000832 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattner5700fab2007-10-07 02:00:24 +0000834 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000835 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000836
Chris Lattner5700fab2007-10-07 02:00:24 +0000837 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000838 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000839 Diag(Tok, diag::err_expected_colon);
840 break;
841 }
842 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000844 ArgInfo.Type = 0;
845 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
846 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
847
Chris Lattner5700fab2007-10-07 02:00:24 +0000848 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000849 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000850 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000851 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000852
Chris Lattner0ef13522007-10-09 17:51:17 +0000853 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000854 Diag(Tok, diag::err_expected_ident); // missing argument name.
855 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000858 ArgInfo.Name = Tok.getIdentifierInfo();
859 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000860 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000861
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000862 ArgInfos.push_back(ArgInfo);
863 KeyIdents.push_back(SelIdent);
864
Chris Lattner5700fab2007-10-07 02:00:24 +0000865 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000866 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000867 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000868 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000869 break;
870 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000873 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000874
Chris Lattner5700fab2007-10-07 02:00:24 +0000875 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000876 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000877 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000878 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000879 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000880 ConsumeToken();
881 break;
882 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000883 DeclSpec DS;
884 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000885 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000886 Declarator ParmDecl(DS, Declarator::PrototypeContext);
887 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000888 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000889 }
Mike Stump11289f42009-09-09 15:08:12 +0000890
Chris Lattner5700fab2007-10-07 02:00:24 +0000891 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000892 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000893 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000894 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
895 ParseGNUAttributes()));
Mike Stump11289f42009-09-09 15:08:12 +0000896
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000897 if (KeyIdents.size() == 0)
898 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000899 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
900 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000901 DeclPtrTy Result
902 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000903 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000904 &ArgInfos[0], CargNames,
905 MethodAttrs.get(),
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000906 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000907 PD.complete(Result);
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000908
909 // Delete referenced AttributeList objects.
910 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
911 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
912 delete I->ArgAttrs;
913
John McCall28a6aea2009-11-04 02:18:39 +0000914 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000915}
916
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000917/// objc-protocol-refs:
918/// '<' identifier-list '>'
919///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000920bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000921ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000922 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
923 bool WarnOnDeclarations,
924 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000925 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000926
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000927 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattner3bbae002008-07-26 04:03:38 +0000929 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000930
Chris Lattner3bbae002008-07-26 04:03:38 +0000931 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000932 if (Tok.is(tok::code_completion)) {
933 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
934 ProtocolIdents.size());
935 ConsumeToken();
936 }
937
Chris Lattner3bbae002008-07-26 04:03:38 +0000938 if (Tok.isNot(tok::identifier)) {
939 Diag(Tok, diag::err_expected_ident);
940 SkipUntil(tok::greater);
941 return true;
942 }
943 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
944 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000945 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000946 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000947
Chris Lattner3bbae002008-07-26 04:03:38 +0000948 if (Tok.isNot(tok::comma))
949 break;
950 ConsumeToken();
951 }
Mike Stump11289f42009-09-09 15:08:12 +0000952
Chris Lattner3bbae002008-07-26 04:03:38 +0000953 // Consume the '>'.
954 if (Tok.isNot(tok::greater)) {
955 Diag(Tok, diag::err_expected_greater);
956 return true;
957 }
Mike Stump11289f42009-09-09 15:08:12 +0000958
Chris Lattner3bbae002008-07-26 04:03:38 +0000959 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000960
Chris Lattner3bbae002008-07-26 04:03:38 +0000961 // Convert the list of protocols identifiers into a list of protocol decls.
962 Actions.FindProtocolDeclaration(WarnOnDeclarations,
963 &ProtocolIdents[0], ProtocolIdents.size(),
964 Protocols);
965 return false;
966}
967
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000968/// objc-class-instance-variables:
969/// '{' objc-instance-variable-decl-list[opt] '}'
970///
971/// objc-instance-variable-decl-list:
972/// objc-visibility-spec
973/// objc-instance-variable-decl ';'
974/// ';'
975/// objc-instance-variable-decl-list objc-visibility-spec
976/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
977/// objc-instance-variable-decl-list ';'
978///
979/// objc-visibility-spec:
980/// @private
981/// @protected
982/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000983/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000984///
985/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000986/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000987///
Chris Lattner83f095c2009-03-28 19:18:32 +0000988void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000989 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +0000990 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000991 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000992 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000993
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000994 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000995
Steve Naroff00433d32007-08-21 21:17:12 +0000996 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000997
Steve Naroff00433d32007-08-21 21:17:12 +0000998 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000999 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001000 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001001
Steve Naroff00433d32007-08-21 21:17:12 +00001002 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001003 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +00001004 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001005 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001006 ConsumeToken();
1007 continue;
1008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Steve Naroff00433d32007-08-21 21:17:12 +00001010 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001011 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001012 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001013
1014 if (Tok.is(tok::code_completion)) {
1015 Actions.CodeCompleteObjCAtVisibility(CurScope);
1016 ConsumeToken();
1017 }
1018
Steve Naroff7c348172007-08-23 18:16:40 +00001019 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001020 case tok::objc_private:
1021 case tok::objc_public:
1022 case tok::objc_protected:
1023 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001024 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001025 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001026 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001027 default:
1028 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001029 continue;
1030 }
1031 }
Mike Stump11289f42009-09-09 15:08:12 +00001032
Douglas Gregor48d46252010-01-13 21:54:15 +00001033 if (Tok.is(tok::code_completion)) {
1034 Actions.CodeCompleteOrdinaryName(CurScope,
1035 Action::CCC_ObjCInstanceVariableList);
1036 ConsumeToken();
1037 }
1038
John McCallcfefb6d2009-11-03 02:38:08 +00001039 struct ObjCIvarCallback : FieldCallback {
1040 Parser &P;
1041 DeclPtrTy IDecl;
1042 tok::ObjCKeywordKind visibility;
1043 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1044
1045 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1046 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1047 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1048 }
1049
1050 DeclPtrTy invoke(FieldDeclarator &FD) {
1051 // Install the declarator into the interface decl.
1052 DeclPtrTy Field
1053 = P.Actions.ActOnIvar(P.CurScope,
1054 FD.D.getDeclSpec().getSourceRange().getBegin(),
1055 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001056 if (Field)
1057 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001058 return Field;
1059 }
1060 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1061
Chris Lattnera12405b2008-04-10 06:46:29 +00001062 // Parse all the comma separated declarators.
1063 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001064 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001065
Chris Lattner0ef13522007-10-09 17:51:17 +00001066 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001067 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001068 } else {
1069 Diag(Tok, diag::err_expected_semi_decl_list);
1070 // Skip to end of block or statement
1071 SkipUntil(tok::r_brace, true, true);
1072 }
1073 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001074 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001075 // Call ActOnFields() even if we don't have any decls. This is useful
1076 // for code rewriting tools that need to be aware of the empty list.
1077 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001078 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001079 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001080 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001081}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001082
1083/// objc-protocol-declaration:
1084/// objc-protocol-definition
1085/// objc-protocol-forward-reference
1086///
1087/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001088/// @protocol identifier
1089/// objc-protocol-refs[opt]
1090/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001091/// @end
1092///
1093/// objc-protocol-forward-reference:
1094/// @protocol identifier-list ';'
1095///
1096/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001097/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001098/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001099Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1100 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001101 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001102 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1103 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001104
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001105 if (Tok.is(tok::code_completion)) {
1106 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1107 ConsumeToken();
1108 }
1109
Chris Lattner0ef13522007-10-09 17:51:17 +00001110 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001111 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001112 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001113 }
1114 // Save the protocol name, then consume it.
1115 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1116 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner0ef13522007-10-09 17:51:17 +00001118 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001119 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001120 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001121 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001122 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001123 }
Mike Stump11289f42009-09-09 15:08:12 +00001124
Chris Lattner0ef13522007-10-09 17:51:17 +00001125 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001126 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1127 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1128
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001129 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001130 while (1) {
1131 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001132 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001133 Diag(Tok, diag::err_expected_ident);
1134 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001135 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001136 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001137 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1138 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001139 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001140
Chris Lattner0ef13522007-10-09 17:51:17 +00001141 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001142 break;
1143 }
1144 // Consume the ';'.
1145 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001146 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001147
Steve Naroff93eb5f12007-10-10 17:32:04 +00001148 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001149 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001150 ProtocolRefs.size(),
1151 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001152 }
Mike Stump11289f42009-09-09 15:08:12 +00001153
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001154 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001155 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001156
Chris Lattner83f095c2009-03-28 19:18:32 +00001157 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001158 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001159 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001160 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1161 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001162 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001163
Chris Lattner83f095c2009-03-28 19:18:32 +00001164 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001165 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001166 ProtocolRefs.data(),
1167 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001168 ProtocolLocs.data(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001169 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001170 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001171 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001172}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001173
1174/// objc-implementation:
1175/// objc-class-implementation-prologue
1176/// objc-category-implementation-prologue
1177///
1178/// objc-class-implementation-prologue:
1179/// @implementation identifier objc-superclass[opt]
1180/// objc-class-instance-variables[opt]
1181///
1182/// objc-category-implementation-prologue:
1183/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001184Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001185 SourceLocation atLoc) {
1186 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1187 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1188 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001189
Douglas Gregor49c22a72009-11-18 16:26:39 +00001190 // Code completion after '@implementation'.
1191 if (Tok.is(tok::code_completion)) {
1192 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1193 ConsumeToken();
1194 }
1195
Chris Lattner0ef13522007-10-09 17:51:17 +00001196 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001197 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001198 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001199 }
1200 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001201 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001202 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001203
1204 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001205 // we have a category implementation.
1206 SourceLocation lparenLoc = ConsumeParen();
1207 SourceLocation categoryLoc, rparenLoc;
1208 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001209
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001210 if (Tok.is(tok::code_completion)) {
1211 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1212 ConsumeToken();
1213 }
1214
Chris Lattner0ef13522007-10-09 17:51:17 +00001215 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001216 categoryId = Tok.getIdentifierInfo();
1217 categoryLoc = ConsumeToken();
1218 } else {
1219 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001220 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001221 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001222 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001223 Diag(Tok, diag::err_expected_rparen);
1224 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001225 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001226 }
1227 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001228 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001229 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001230 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001231 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001232 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001233 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001234 }
1235 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001236 SourceLocation superClassLoc;
1237 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001238 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001239 // We have a super class
1240 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001241 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001242 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001243 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001244 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001245 superClassId = Tok.getIdentifierInfo();
1246 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001247 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001248 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001249 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001250 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001251
Steve Naroff33a1e802007-10-29 21:38:07 +00001252 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001253 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahaniana9effb52010-03-22 19:04:14 +00001254 tok::objc_private, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001255 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001256 PendingObjCImpDecl.push_back(ObjCImpDecl);
1257
Chris Lattner83f095c2009-03-28 19:18:32 +00001258 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001259}
Steve Naroff33a1e802007-10-29 21:38:07 +00001260
Ted Kremenekc7c64312010-01-07 01:20:12 +00001261Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001262 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1263 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001264 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001265 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001266 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001267 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001268 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001269 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001270 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001271 else {
1272 // missing @implementation
1273 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1274 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001275 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001276}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001277
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001278Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001279 if (PendingObjCImpDecl.empty())
1280 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1281 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001282 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001283 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1284}
1285
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001286/// compatibility-alias-decl:
1287/// @compatibility_alias alias-name class-name ';'
1288///
Chris Lattner83f095c2009-03-28 19:18:32 +00001289Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001290 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1291 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1292 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001293 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001294 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001295 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001296 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001297 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1298 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001299 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001300 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001301 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001302 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001303 IdentifierInfo *classId = Tok.getIdentifierInfo();
1304 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1305 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001306 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001307 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001308 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001309 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1310 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001311}
1312
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001313/// property-synthesis:
1314/// @synthesize property-ivar-list ';'
1315///
1316/// property-ivar-list:
1317/// property-ivar
1318/// property-ivar-list ',' property-ivar
1319///
1320/// property-ivar:
1321/// identifier
1322/// identifier '=' identifier
1323///
Chris Lattner83f095c2009-03-28 19:18:32 +00001324Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001325 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1326 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001327 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001328
Douglas Gregor88e72a02009-11-18 19:45:45 +00001329 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001330 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001331 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001332 ConsumeToken();
1333 }
1334
Douglas Gregor88e72a02009-11-18 19:45:45 +00001335 if (Tok.isNot(tok::identifier)) {
1336 Diag(Tok, diag::err_synthesized_property_name);
1337 SkipUntil(tok::semi);
1338 return DeclPtrTy();
1339 }
1340
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001341 IdentifierInfo *propertyIvar = 0;
1342 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1343 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001344 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001345 // property '=' ivar-name
1346 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001347
1348 if (Tok.is(tok::code_completion)) {
1349 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1350 ObjCImpDecl);
1351 ConsumeToken();
1352 }
1353
Chris Lattner0ef13522007-10-09 17:51:17 +00001354 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001355 Diag(Tok, diag::err_expected_ident);
1356 break;
1357 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001358 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001359 ConsumeToken(); // consume ivar-name
1360 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001361 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1362 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001363 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001364 break;
1365 ConsumeToken(); // consume ','
1366 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001367 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001368 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001369 SkipUntil(tok::semi);
1370 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001371 else
1372 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001373 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001374}
1375
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001376/// property-dynamic:
1377/// @dynamic property-list
1378///
1379/// property-list:
1380/// identifier
1381/// property-list ',' identifier
1382///
Chris Lattner83f095c2009-03-28 19:18:32 +00001383Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001384 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1385 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1386 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001387 while (true) {
1388 if (Tok.is(tok::code_completion)) {
1389 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1390 ConsumeToken();
1391 }
1392
1393 if (Tok.isNot(tok::identifier)) {
1394 Diag(Tok, diag::err_expected_ident);
1395 SkipUntil(tok::semi);
1396 return DeclPtrTy();
1397 }
1398
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001399 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1400 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1401 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1402 propertyId, 0);
1403
Chris Lattner0ef13522007-10-09 17:51:17 +00001404 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001405 break;
1406 ConsumeToken(); // consume ','
1407 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001408 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001409 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001410 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001411}
Mike Stump11289f42009-09-09 15:08:12 +00001412
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001413/// objc-throw-statement:
1414/// throw expression[opt];
1415///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001416Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001417 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001418 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001419 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001420 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001421 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001422 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001423 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001424 }
1425 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001426 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001427 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001428}
1429
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001430/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001431/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001432///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001433Parser::OwningStmtResult
1434Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001435 ConsumeToken(); // consume synchronized
1436 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001437 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001438 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001439 }
1440 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001441 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001442 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001443 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001444 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001445 }
1446 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001447 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001448 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001449 }
1450 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001451 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001452 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001453 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001454 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001455 // Enter a scope to hold everything within the compound stmt. Compound
1456 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001457 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001458
Sebastian Redl042ad952008-12-11 19:30:53 +00001459 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001460
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001461 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001462 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001463 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001464 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001465}
1466
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001467/// objc-try-catch-statement:
1468/// @try compound-statement objc-catch-list[opt]
1469/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1470///
1471/// objc-catch-list:
1472/// @catch ( parameter-declaration ) compound-statement
1473/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1474/// catch-parameter-declaration:
1475/// parameter-declaration
1476/// '...' [OBJC2]
1477///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001478Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001479 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001480
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001481 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001482 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001483 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001484 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001485 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001486 OwningStmtResult CatchStmts(Actions);
1487 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001488 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001489 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001490 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001491 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001492 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001493
Chris Lattner0ef13522007-10-09 17:51:17 +00001494 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001495 // At this point, we need to lookahead to determine if this @ is the start
1496 // of an @catch or @finally. We don't want to consume the @ token if this
1497 // is an @try or @encode or something else.
1498 Token AfterAt = GetLookAheadToken(1);
1499 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1500 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1501 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001502
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001503 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001504 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001505 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001506 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001507 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001508 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001509 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001510 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001511 DeclSpec DS;
1512 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001513 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001514 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001515 // we must accept either 'declarator' or 'abstract-declarator' here.
1516 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1517 ParseDeclarator(ParmDecl);
1518
1519 // Inform the actions module about the parameter declarator, so it
1520 // gets added to the current scope.
Fariborz Jahaniane1651912010-02-03 00:32:51 +00001521 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff371b8fb2009-03-03 19:52:17 +00001522 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian08d614d2010-02-03 00:01:43 +00001523 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroffe6016792008-02-05 21:27:35 +00001524 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001525 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001526
Steve Naroff65a00892009-04-07 22:56:58 +00001527 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001528
Steve Naroff65a00892009-04-07 22:56:58 +00001529 if (Tok.is(tok::r_paren))
1530 RParenLoc = ConsumeParen();
1531 else // Skip over garbage, until we get to ')'. Eat the ')'.
1532 SkipUntil(tok::r_paren, true, false);
1533
Sebastian Redlc13f2682008-12-09 20:22:58 +00001534 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001535 if (Tok.is(tok::l_brace))
1536 CatchBody = ParseCompoundStatementBody();
1537 else
1538 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001539 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001540 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001541 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001542 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001543 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001544 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001545 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1546 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001547 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001548 }
1549 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001550 } else {
1551 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001552 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001553 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001554
Sebastian Redlc13f2682008-12-09 20:22:58 +00001555 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001556 if (Tok.is(tok::l_brace))
1557 FinallyBody = ParseCompoundStatementBody();
1558 else
1559 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001560 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001561 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001562 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001563 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001564 catch_or_finally_seen = true;
1565 break;
1566 }
1567 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001568 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001569 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001570 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001571 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001572 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1573 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001574}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001575
Steve Naroff09bf8152007-09-06 21:24:23 +00001576/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001577///
Chris Lattner83f095c2009-03-28 19:18:32 +00001578Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1579 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001580
Chris Lattnereae6cb62009-03-05 08:00:35 +00001581 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1582 PP.getSourceManager(),
1583 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001584
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001585 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001586 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001587 if (ObjCImpDecl) {
1588 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001589 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001590 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001591 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001592 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001593
Steve Naroffbb875722007-11-11 19:54:21 +00001594 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001595 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001596 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001597
Steve Naroffbb875722007-11-11 19:54:21 +00001598 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1599 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001600
Steve Naroffbb875722007-11-11 19:54:21 +00001601 // If we didn't find the '{', bail out.
1602 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001603 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001604 }
Steve Naroffbb875722007-11-11 19:54:21 +00001605 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001606
Steve Naroffbb875722007-11-11 19:54:21 +00001607 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001608 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001609
Steve Naroffbb875722007-11-11 19:54:21 +00001610 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001611 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001612 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001613
1614 OwningStmtResult FnBody(ParseCompoundStatementBody());
1615
Steve Naroffbb875722007-11-11 19:54:21 +00001616 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001617 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001618 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1619 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001620
Steve Naroffb94d7f62009-03-02 22:00:56 +00001621 // TODO: Pass argument information.
1622 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001623
Steve Naroffbb875722007-11-11 19:54:21 +00001624 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001625 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001626
Steve Naroff7b8fa472007-11-13 23:01:27 +00001627 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001628}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001629
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001630Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001631 if (Tok.is(tok::code_completion)) {
1632 Actions.CodeCompleteObjCAtStatement(CurScope);
1633 ConsumeToken();
1634 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001635 }
1636
1637 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001638 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001639
1640 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001641 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001642
1643 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001644 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001645
Sebastian Redl90893182008-12-11 22:33:27 +00001646 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001647 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001648 // If the expression is invalid, skip ahead to the next semicolon. Not
1649 // doing this opens us up to the possibility of infinite loops if
1650 // ParseExpression does not consume any tokens.
1651 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001652 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001653 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001654
Steve Naroffe6016792008-02-05 21:27:35 +00001655 // Otherwise, eat the semicolon.
1656 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001657 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001658}
1659
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001660Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001661 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001662 case tok::code_completion:
1663 Actions.CodeCompleteObjCAtExpression(CurScope);
1664 ConsumeToken();
1665 return ExprError();
1666
Chris Lattnere002fbe2007-12-12 01:04:12 +00001667 case tok::string_literal: // primary-expression: string-literal
1668 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001669 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001670 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001671 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001672 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001673
Chris Lattner197a3012008-08-05 06:19:09 +00001674 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1675 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001676 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001677 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001678 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001679 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001680 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001681 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001682 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001683 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001684 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001685}
1686
Mike Stump11289f42009-09-09 15:08:12 +00001687/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001688/// '[' objc-receiver objc-message-args ']'
1689///
1690/// objc-receiver:
1691/// expression
1692/// class-name
1693/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001694Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001695 assert(Tok.is(tok::l_square) && "'[' expected");
1696 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1697
1698 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001699 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001700 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001701 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1702 SourceLocation NameLoc = ConsumeToken();
1703 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1704 ExprArg(Actions));
1705 }
Chris Lattner8f697062008-01-25 18:59:06 +00001706 }
1707
Sebastian Redl59b5e512008-12-11 21:36:32 +00001708 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001709 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001710 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001711 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001712 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001713
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001714 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001715 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001716}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001717
Chris Lattner8f697062008-01-25 18:59:06 +00001718/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1719/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001720///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001721/// objc-message-args:
1722/// objc-selector
1723/// objc-keywordarg-list
1724///
1725/// objc-keywordarg-list:
1726/// objc-keywordarg
1727/// objc-keywordarg-list objc-keywordarg
1728///
Mike Stump11289f42009-09-09 15:08:12 +00001729/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001730/// selector-name[opt] ':' objc-keywordexpr
1731///
1732/// objc-keywordexpr:
1733/// nonempty-expr-list
1734///
1735/// nonempty-expr-list:
1736/// assignment-expression
1737/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001738///
1739Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001740Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001741 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001742 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001743 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001744 if (Tok.is(tok::code_completion)) {
1745 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001746 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1747 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001748 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001749 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1750 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001751 ConsumeToken();
1752 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001753
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001754 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001755 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001756 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001757
Anders Carlsson978f08d2009-02-14 18:21:46 +00001758 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001759
Steve Narofff73590d2007-09-27 14:38:14 +00001760 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001761 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001762
Chris Lattner0ef13522007-10-09 17:51:17 +00001763 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001764 while (1) {
1765 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001766 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001767
Chris Lattner0ef13522007-10-09 17:51:17 +00001768 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001769 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001770 // We must manually skip to a ']', otherwise the expression skipper will
1771 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1772 // the enclosing expression.
1773 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001774 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001775 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001776
Steve Narofff73590d2007-09-27 14:38:14 +00001777 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001778 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001779 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001780 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001781 // We must manually skip to a ']', otherwise the expression skipper will
1782 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1783 // the enclosing expression.
1784 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001785 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001786 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001787
Steve Naroff486760a2007-09-17 20:25:27 +00001788 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001789 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001790
Douglas Gregor1b605f72009-11-19 01:08:35 +00001791 // Code completion after each argument.
1792 if (Tok.is(tok::code_completion)) {
1793 if (ReceiverName)
1794 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1795 KeyIdents.data(),
1796 KeyIdents.size());
1797 else
1798 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1799 KeyIdents.data(),
1800 KeyIdents.size());
1801 ConsumeToken();
1802 }
1803
Steve Naroff486760a2007-09-17 20:25:27 +00001804 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001805 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001806 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001807 break;
1808 // We have a selector or a colon, continue parsing.
1809 }
1810 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001811 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001812 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001813 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001814 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001815 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001816 // We must manually skip to a ']', otherwise the expression skipper will
1817 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1818 // the enclosing expression.
1819 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001820 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001821 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001822
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001823 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001824 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001825 }
1826 } else if (!selIdent) {
1827 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001828
Chris Lattner197a3012008-08-05 06:19:09 +00001829 // We must manually skip to a ']', otherwise the expression skipper will
1830 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1831 // the enclosing expression.
1832 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001833 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001834 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00001835
Chris Lattner0ef13522007-10-09 17:51:17 +00001836 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00001837 if (Tok.is(tok::identifier))
1838 Diag(Tok, diag::err_expected_colon);
1839 else
1840 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001841 // We must manually skip to a ']', otherwise the expression skipper will
1842 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1843 // the enclosing expression.
1844 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001845 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001846 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001847
Chris Lattner8f697062008-01-25 18:59:06 +00001848 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001849
Steve Naroffe61bfa82007-10-05 18:42:47 +00001850 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001851 if (nKeys == 0)
1852 KeyIdents.push_back(selIdent);
1853 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001854
Chris Lattner5700fab2007-10-07 02:00:24 +00001855 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001856 if (ReceiverName)
1857 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001858 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001859 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001860 KeyExprs.take(), KeyExprs.size()));
1861 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001862 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001863 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001864}
1865
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001866Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001867 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001868 if (Res.isInvalid()) return move(Res);
1869
Chris Lattnere002fbe2007-12-12 01:04:12 +00001870 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1871 // expressions. At this point, we know that the only valid thing that starts
1872 // with '@' is an @"".
1873 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001874 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001875 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001876 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001877
Chris Lattnere002fbe2007-12-12 01:04:12 +00001878 while (Tok.is(tok::at)) {
1879 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001880
Sebastian Redlc13f2682008-12-09 20:22:58 +00001881 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001882 if (!isTokenStringLiteral())
1883 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001884
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001885 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001886 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001887 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001888
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001889 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001890 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001891
1892 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1893 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001894}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001895
1896/// objc-encode-expression:
1897/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001898Parser::OwningExprResult
1899Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001900 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001901
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001902 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001903
Chris Lattner197a3012008-08-05 06:19:09 +00001904 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001905 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1906
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001907 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001908
Douglas Gregor220cac52009-02-18 17:45:20 +00001909 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001910
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001911 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001912
Douglas Gregor220cac52009-02-18 17:45:20 +00001913 if (Ty.isInvalid())
1914 return ExprError();
1915
Mike Stump11289f42009-09-09 15:08:12 +00001916 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001917 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001918}
Anders Carlssone01493d2007-08-23 15:25:28 +00001919
1920/// objc-protocol-expression
1921/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001922Parser::OwningExprResult
1923Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001924 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001925
Chris Lattner197a3012008-08-05 06:19:09 +00001926 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001927 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1928
Anders Carlssone01493d2007-08-23 15:25:28 +00001929 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001930
Chris Lattner197a3012008-08-05 06:19:09 +00001931 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001932 return ExprError(Diag(Tok, diag::err_expected_ident));
1933
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001934 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001935 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001936
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001937 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001938
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001939 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1940 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001941}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001942
1943/// objc-selector-expression
1944/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001945Parser::OwningExprResult
1946Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001947 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001948
Chris Lattner197a3012008-08-05 06:19:09 +00001949 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001950 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1951
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001952 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001953 SourceLocation LParenLoc = ConsumeParen();
1954 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001955 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001956 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1957 return ExprError(Diag(Tok, diag::err_expected_ident));
1958
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001959 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001960 unsigned nColons = 0;
1961 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001962 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001963 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001964 return ExprError(Diag(Tok, diag::err_expected_colon));
1965
Chris Lattner5e530bc2007-12-27 19:57:00 +00001966 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001967 ConsumeToken(); // Eat the ':'.
1968 if (Tok.is(tok::r_paren))
1969 break;
1970 // Check for another keyword selector.
1971 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001972 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001973 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001974 if (!SelIdent && Tok.isNot(tok::colon))
1975 break;
1976 }
Steve Naroff152dd812007-12-05 22:21:29 +00001977 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001978 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001979 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001980 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1981 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001982 }