blob: 7ab0e71dc2356639db1b3294a134a9ac93c150b7 [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();
Mike Stump11289f42009-09-09 15:08:12 +0000145
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;
Mike Stump11289f42009-09-09 15:08:12 +0000150
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000151 if (Tok.is(tok::code_completion)) {
152 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
153 ConsumeToken();
154 }
155
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000156 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000157 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158 categoryId = Tok.getIdentifierInfo();
159 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000160 } else if (!getLang().ObjC2) {
161 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000164 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 Diag(Tok, diag::err_expected_rparen);
166 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000167 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 }
169 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000170
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000172 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000173 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000174 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000175 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000176 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
177 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000178 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000179
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 if (attrList) // categories don't support attributes.
181 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000182
Jay Foad7d0479f2009-05-21 09:52:38 +0000183 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000184 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000185 nameId, nameLoc,
186 categoryId, categoryLoc,
187 ProtocolRefs.data(),
188 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000189 ProtocolLocs.data(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000190 EndProtoLoc);
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000191 if (Tok.is(tok::l_brace))
192 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
193 atLoc);
194
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000195 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000196 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000197 }
198 // Parse a class interface.
199 IdentifierInfo *superClassId = 0;
200 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000201
Chris Lattner0ef13522007-10-09 17:51:17 +0000202 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000204
205 // Code completion of superclass names.
206 if (Tok.is(tok::code_completion)) {
207 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
208 ConsumeToken();
209 }
210
Chris Lattner0ef13522007-10-09 17:51:17 +0000211 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000212 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000213 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000214 }
215 superClassId = Tok.getIdentifierInfo();
216 superClassLoc = ConsumeToken();
217 }
218 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000219 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000220 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000222 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000223 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
224 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000225 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000226
227 DeclPtrTy ClsType =
228 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000229 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000230 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000231 ProtocolLocs.data(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000232 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000233
Chris Lattner0ef13522007-10-09 17:51:17 +0000234 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000235 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000237 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000238 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000239}
240
John McCall064d77b2009-12-03 22:31:13 +0000241/// The Objective-C property callback. This should be defined where
242/// it's used, but instead it's been lifted to here to support VS2005.
243struct Parser::ObjCPropertyCallback : FieldCallback {
244 Parser &P;
245 DeclPtrTy IDecl;
246 llvm::SmallVectorImpl<DeclPtrTy> &Props;
247 ObjCDeclSpec &OCDS;
248 SourceLocation AtLoc;
249 tok::ObjCKeywordKind MethodImplKind;
250
251 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
252 llvm::SmallVectorImpl<DeclPtrTy> &Props,
253 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
254 tok::ObjCKeywordKind MethodImplKind) :
255 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
256 MethodImplKind(MethodImplKind) {
257 }
258
259 DeclPtrTy invoke(FieldDeclarator &FD) {
260 if (FD.D.getIdentifier() == 0) {
261 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
262 << FD.D.getSourceRange();
263 return DeclPtrTy();
264 }
265 if (FD.BitfieldSize) {
266 P.Diag(AtLoc, diag::err_objc_property_bitfield)
267 << FD.D.getSourceRange();
268 return DeclPtrTy();
269 }
270
271 // Install the property declarator into interfaceDecl.
272 IdentifierInfo *SelName =
273 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
274
275 Selector GetterSel =
276 P.PP.getSelectorTable().getNullarySelector(SelName);
277 IdentifierInfo *SetterName = OCDS.getSetterName();
278 Selector SetterSel;
279 if (SetterName)
280 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
281 else
282 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
283 P.PP.getSelectorTable(),
284 FD.D.getIdentifier());
285 bool isOverridingProperty = false;
286 DeclPtrTy Property =
287 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
288 GetterSel, SetterSel, IDecl,
289 &isOverridingProperty,
290 MethodImplKind);
291 if (!isOverridingProperty)
292 Props.push_back(Property);
293
294 return Property;
295 }
296};
297
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000298/// objc-interface-decl-list:
299/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000300/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000301/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000302/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000303/// objc-interface-decl-list declaration
304/// objc-interface-decl-list ';'
305///
Steve Naroff99264b42007-08-22 16:35:03 +0000306/// objc-method-requirement: [OBJC2]
307/// @required
308/// @optional
309///
Chris Lattner83f095c2009-03-28 19:18:32 +0000310void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000311 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000312 llvm::SmallVector<DeclPtrTy, 32> allMethods;
313 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000314 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000315 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenekc7c64312010-01-07 01:20:12 +0000317 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000318
Steve Naroff99264b42007-08-22 16:35:03 +0000319 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000320 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000321 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000322 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000323 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000324 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000325 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
326 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000327 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
328 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000329 continue;
330 }
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattner038a3e32008-10-20 05:46:22 +0000332 // Ignore excess semicolons.
333 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000334 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000335 continue;
336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
Chris Lattnerda9fb152008-10-20 06:10:06 +0000338 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000339 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000340 break;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Douglas Gregorf1934162010-01-13 21:24:21 +0000342 // Code completion within an Objective-C interface.
343 if (Tok.is(tok::code_completion)) {
344 Actions.CodeCompleteOrdinaryName(CurScope,
345 ObjCImpDecl? Action::CCC_ObjCImplementation
346 : Action::CCC_ObjCInterface);
347 ConsumeToken();
348 }
349
Chris Lattner038a3e32008-10-20 05:46:22 +0000350 // If we don't have an @ directive, parse it as a function definition.
351 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000352 // The code below does not consume '}'s because it is afraid of eating the
353 // end of a namespace. Because of the way this code is structured, an
354 // erroneous r_brace would cause an infinite loop if not handled here.
355 if (Tok.is(tok::r_brace))
356 break;
Mike Stump11289f42009-09-09 15:08:12 +0000357
Steve Narofff1bc45b2007-08-22 18:35:33 +0000358 // FIXME: as the name implies, this rule allows function definitions.
359 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000360 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000361 continue;
362 }
Mike Stump11289f42009-09-09 15:08:12 +0000363
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 // Otherwise, we have an @ directive, eat the @.
365 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000366 if (Tok.is(tok::code_completion)) {
367 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
368 ConsumeToken();
369 break;
370 }
371
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000372 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000373
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000374 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000375 AtEnd.setBegin(AtLoc);
376 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000377 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattnerda9fb152008-10-20 06:10:06 +0000380 // Eat the identifier.
381 ConsumeToken();
382
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000383 switch (DirectiveKind) {
384 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000385 // FIXME: If someone forgets an @end on a protocol, this loop will
386 // continue to eat up tons of stuff and spew lots of nonsense errors. It
387 // would probably be better to bail out if we saw an @class or @interface
388 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000389 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000390 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000391 SkipUntil(tok::r_brace, tok::at);
392 break;
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000394 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000395 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000396 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000397 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000398 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000399 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000400 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000401 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000402 break;
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000404 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000405 if (!getLang().ObjC2)
406 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
407
Chris Lattner038a3e32008-10-20 05:46:22 +0000408 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000409 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000410 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000411 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
412 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000413
John McCall064d77b2009-12-03 22:31:13 +0000414 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
415 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000416
Chris Lattner038a3e32008-10-20 05:46:22 +0000417 // Parse all the comma separated declarators.
418 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000419 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000421 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
422 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000423 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000424 }
Steve Naroff99264b42007-08-22 16:35:03 +0000425 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000426
427 // We break out of the big loop in two cases: when we see @end or when we see
428 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000429 if (Tok.is(tok::code_completion)) {
430 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
431 ConsumeToken();
432 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000433 ConsumeToken(); // the "end" identifier
434 else
435 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000437 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000438 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenekc7c64312010-01-07 01:20:12 +0000439 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000440 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000441 allProperties.data(), allProperties.size(),
442 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000443}
444
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000445/// Parse property attribute declarations.
446///
447/// property-attr-decl: '(' property-attrlist ')'
448/// property-attrlist:
449/// property-attribute
450/// property-attrlist ',' property-attribute
451/// property-attribute:
452/// getter '=' identifier
453/// setter '=' identifier ':'
454/// readonly
455/// readwrite
456/// assign
457/// retain
458/// copy
459/// nonatomic
460///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000461void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
462 DeclPtrTy *Methods,
463 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000464 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000465 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000467 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000468 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000469 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroff936354c2009-10-08 21:55:05 +0000470 ConsumeToken();
471 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000472 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattner76619232008-10-20 07:22:18 +0000474 // If this is not an identifier at all, bail out early.
475 if (II == 0) {
476 MatchRHSPunctuation(tok::r_paren, LHSLoc);
477 return;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattner1db33542008-10-20 07:37:22 +0000480 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattner68e48682008-11-20 04:42:34 +0000482 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000483 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000484 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000485 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000486 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000487 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000488 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000489 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000490 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000491 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000492 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000493 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000494 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000495 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000496 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
497 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000498 return;
Mike Stump11289f42009-09-09 15:08:12 +0000499
Douglas Gregorc8537c52009-11-19 07:41:15 +0000500 if (Tok.is(tok::code_completion)) {
501 if (II->getNameStart()[0] == 's')
502 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
503 Methods, NumMethods);
504 else
505 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
506 Methods, NumMethods);
507 ConsumeToken();
508 }
509
Chris Lattnerbeca7702008-10-20 07:24:39 +0000510 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000511 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000512 SkipUntil(tok::r_paren);
513 return;
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000516 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000517 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
518 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000519 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000520
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000521 if (ExpectAndConsume(tok::colon,
522 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000523 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000524 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000525 } else {
526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
527 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000528 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000529 }
Chris Lattner825bca12008-10-20 07:39:53 +0000530 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000531 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000532 SkipUntil(tok::r_paren);
533 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner1db33542008-10-20 07:37:22 +0000536 if (Tok.isNot(tok::comma))
537 break;
Mike Stump11289f42009-09-09 15:08:12 +0000538
Chris Lattner1db33542008-10-20 07:37:22 +0000539 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattner1db33542008-10-20 07:37:22 +0000542 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000543}
544
Steve Naroff09bf8152007-09-06 21:24:23 +0000545/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000546/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000547/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000548///
549/// objc-instance-method: '-'
550/// objc-class-method: '+'
551///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000552/// objc-method-attributes: [OBJC2]
553/// __attribute__((deprecated))
554///
Mike Stump11289f42009-09-09 15:08:12 +0000555Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000556 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000557 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000558
Mike Stump11289f42009-09-09 15:08:12 +0000559 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000560 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000561
Chris Lattner83f095c2009-03-28 19:18:32 +0000562 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000563 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000564 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000565 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000566}
567
568/// objc-selector:
569/// identifier
570/// one of
571/// enum struct union if else while do for switch case default
572/// break continue return goto asm sizeof typeof __alignof
573/// unsigned long const short volatile signed restrict _Complex
574/// in out inout bycopy byref oneway int char float double void _Bool
575///
Chris Lattner4f472a32009-04-11 18:13:45 +0000576IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000577 switch (Tok.getKind()) {
578 default:
579 return 0;
580 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000581 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000582 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000583 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000584 case tok::kw_break:
585 case tok::kw_case:
586 case tok::kw_catch:
587 case tok::kw_char:
588 case tok::kw_class:
589 case tok::kw_const:
590 case tok::kw_const_cast:
591 case tok::kw_continue:
592 case tok::kw_default:
593 case tok::kw_delete:
594 case tok::kw_do:
595 case tok::kw_double:
596 case tok::kw_dynamic_cast:
597 case tok::kw_else:
598 case tok::kw_enum:
599 case tok::kw_explicit:
600 case tok::kw_export:
601 case tok::kw_extern:
602 case tok::kw_false:
603 case tok::kw_float:
604 case tok::kw_for:
605 case tok::kw_friend:
606 case tok::kw_goto:
607 case tok::kw_if:
608 case tok::kw_inline:
609 case tok::kw_int:
610 case tok::kw_long:
611 case tok::kw_mutable:
612 case tok::kw_namespace:
613 case tok::kw_new:
614 case tok::kw_operator:
615 case tok::kw_private:
616 case tok::kw_protected:
617 case tok::kw_public:
618 case tok::kw_register:
619 case tok::kw_reinterpret_cast:
620 case tok::kw_restrict:
621 case tok::kw_return:
622 case tok::kw_short:
623 case tok::kw_signed:
624 case tok::kw_sizeof:
625 case tok::kw_static:
626 case tok::kw_static_cast:
627 case tok::kw_struct:
628 case tok::kw_switch:
629 case tok::kw_template:
630 case tok::kw_this:
631 case tok::kw_throw:
632 case tok::kw_true:
633 case tok::kw_try:
634 case tok::kw_typedef:
635 case tok::kw_typeid:
636 case tok::kw_typename:
637 case tok::kw_typeof:
638 case tok::kw_union:
639 case tok::kw_unsigned:
640 case tok::kw_using:
641 case tok::kw_virtual:
642 case tok::kw_void:
643 case tok::kw_volatile:
644 case tok::kw_wchar_t:
645 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000646 case tok::kw__Bool:
647 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000648 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000649 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000650 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000651 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000652 }
Steve Naroff99264b42007-08-22 16:35:03 +0000653}
654
Fariborz Jahanian83615522008-01-02 22:54:34 +0000655/// objc-for-collection-in: 'in'
656///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000657bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000658 // FIXME: May have to do additional look-ahead to only allow for
659 // valid tokens following an 'in'; such as an identifier, unary operators,
660 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000661 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000662 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000663}
664
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000665/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000666/// qualifier list and builds their bitmask representation in the input
667/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000668///
669/// objc-type-qualifiers:
670/// objc-type-qualifier
671/// objc-type-qualifiers objc-type-qualifier
672///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000673void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000674 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000675 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000676 return;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner61511e12007-12-12 06:56:32 +0000678 const IdentifierInfo *II = Tok.getIdentifierInfo();
679 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000680 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000681 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000682
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000683 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000684 switch (i) {
685 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000686 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
687 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
688 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
689 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
690 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
691 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000692 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000693 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000694 ConsumeToken();
695 II = 0;
696 break;
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattner61511e12007-12-12 06:56:32 +0000699 // If this wasn't a recognized qualifier, bail out.
700 if (II) return;
701 }
702}
703
704/// objc-type-name:
705/// '(' objc-type-qualifiers[opt] type-name ')'
706/// '(' objc-type-qualifiers[opt] ')'
707///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000708Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000709 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000710
Chris Lattnerb7954432008-10-22 03:52:06 +0000711 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000712 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000713
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000714 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000715 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000716
Chris Lattnerb7954432008-10-22 03:52:06 +0000717 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000718 if (isTypeSpecifierQualifier()) {
719 TypeResult TypeSpec = ParseTypeName();
720 if (!TypeSpec.isInvalid())
721 Ty = TypeSpec.get();
722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723
Steve Naroff90255b42008-10-21 14:15:04 +0000724 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000725 ConsumeParen();
726 else if (Tok.getLocation() == TypeStartLoc) {
727 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000728 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000729 SkipUntil(tok::r_paren);
730 } else {
731 // Otherwise, we found *something*, but didn't get a ')' in the right
732 // place. Emit an error then return what we have as the type.
733 MatchRHSPunctuation(tok::r_paren, LParenLoc);
734 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000735 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000736}
737
738/// objc-method-decl:
739/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000740/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000741/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000742/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000743///
744/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000745/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000746/// objc-keyword-selector objc-keyword-decl
747///
748/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000749/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
750/// objc-selector ':' objc-keyword-attributes[opt] identifier
751/// ':' objc-type-name objc-keyword-attributes[opt] identifier
752/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000753///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000754/// objc-parmlist:
755/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000756///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000757/// objc-parms:
758/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000759///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000760/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000761/// , ...
762///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000763/// objc-keyword-attributes: [OBJC2]
764/// __attribute__((unused))
765///
Chris Lattner83f095c2009-03-28 19:18:32 +0000766Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
767 tok::TokenKind mType,
768 DeclPtrTy IDecl,
769 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000770 ParsingDeclRAIIObject PD(*this);
771
Chris Lattner2ebb1782008-08-23 01:48:03 +0000772 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000773 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000774 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000775 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000776 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000777
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000778 // If attributes exist before the method, parse them.
779 llvm::OwningPtr<AttributeList> MethodAttrs;
780 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
781 MethodAttrs.reset(ParseGNUAttributes());
782
783 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000784 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000785 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000786
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000787 // An unnamed colon is valid.
788 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000789 Diag(Tok, diag::err_expected_selector_for_method)
790 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000791 // Skip until we get a ; or {}.
792 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000793 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000794 }
Mike Stump11289f42009-09-09 15:08:12 +0000795
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000796 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000797 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000798 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000799 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000800 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
801 ParseGNUAttributes()));
Mike Stump11289f42009-09-09 15:08:12 +0000802
Chris Lattner5700fab2007-10-07 02:00:24 +0000803 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000804 DeclPtrTy Result
805 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000806 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000807 0, CargNames, MethodAttrs.get(),
Ted Kremenekbc76c722009-05-04 17:04:30 +0000808 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000809 PD.complete(Result);
810 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000811 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000812
Steve Narofff73590d2007-09-27 14:38:14 +0000813 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000814 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner5700fab2007-10-07 02:00:24 +0000816 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000817 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Chris Lattner5700fab2007-10-07 02:00:24 +0000819 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000820 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000821 Diag(Tok, diag::err_expected_colon);
822 break;
823 }
824 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000825
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000826 ArgInfo.Type = 0;
827 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
828 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
829
Chris Lattner5700fab2007-10-07 02:00:24 +0000830 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000831 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000832 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000833 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000834
Chris Lattner0ef13522007-10-09 17:51:17 +0000835 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000836 Diag(Tok, diag::err_expected_ident); // missing argument name.
837 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000838 }
Mike Stump11289f42009-09-09 15:08:12 +0000839
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000840 ArgInfo.Name = Tok.getIdentifierInfo();
841 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000842 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000844 ArgInfos.push_back(ArgInfo);
845 KeyIdents.push_back(SelIdent);
846
Chris Lattner5700fab2007-10-07 02:00:24 +0000847 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000848 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000849 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000850 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000851 break;
852 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000855 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner5700fab2007-10-07 02:00:24 +0000857 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000858 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000859 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000860 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000861 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000862 ConsumeToken();
863 break;
864 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000865 DeclSpec DS;
866 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000867 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000868 Declarator ParmDecl(DS, Declarator::PrototypeContext);
869 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000870 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner5700fab2007-10-07 02:00:24 +0000873 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000874 // If attributes exist after the method, parse them.
Mike Stump11289f42009-09-09 15:08:12 +0000875 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000876 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
877 ParseGNUAttributes()));
Mike Stump11289f42009-09-09 15:08:12 +0000878
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000879 if (KeyIdents.size() == 0)
880 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000881 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
882 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000883 DeclPtrTy Result
884 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000885 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000886 &ArgInfos[0], CargNames,
887 MethodAttrs.get(),
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000888 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000889 PD.complete(Result);
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000890
891 // Delete referenced AttributeList objects.
892 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
893 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
894 delete I->ArgAttrs;
895
John McCall28a6aea2009-11-04 02:18:39 +0000896 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000897}
898
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000899/// objc-protocol-refs:
900/// '<' identifier-list '>'
901///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000902bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000903ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000904 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
905 bool WarnOnDeclarations,
906 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000907 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000908
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000909 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner3bbae002008-07-26 04:03:38 +0000911 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner3bbae002008-07-26 04:03:38 +0000913 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000914 if (Tok.is(tok::code_completion)) {
915 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
916 ProtocolIdents.size());
917 ConsumeToken();
918 }
919
Chris Lattner3bbae002008-07-26 04:03:38 +0000920 if (Tok.isNot(tok::identifier)) {
921 Diag(Tok, diag::err_expected_ident);
922 SkipUntil(tok::greater);
923 return true;
924 }
925 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
926 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000927 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000928 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000929
Chris Lattner3bbae002008-07-26 04:03:38 +0000930 if (Tok.isNot(tok::comma))
931 break;
932 ConsumeToken();
933 }
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattner3bbae002008-07-26 04:03:38 +0000935 // Consume the '>'.
936 if (Tok.isNot(tok::greater)) {
937 Diag(Tok, diag::err_expected_greater);
938 return true;
939 }
Mike Stump11289f42009-09-09 15:08:12 +0000940
Chris Lattner3bbae002008-07-26 04:03:38 +0000941 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000942
Chris Lattner3bbae002008-07-26 04:03:38 +0000943 // Convert the list of protocols identifiers into a list of protocol decls.
944 Actions.FindProtocolDeclaration(WarnOnDeclarations,
945 &ProtocolIdents[0], ProtocolIdents.size(),
946 Protocols);
947 return false;
948}
949
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000950/// objc-class-instance-variables:
951/// '{' objc-instance-variable-decl-list[opt] '}'
952///
953/// objc-instance-variable-decl-list:
954/// objc-visibility-spec
955/// objc-instance-variable-decl ';'
956/// ';'
957/// objc-instance-variable-decl-list objc-visibility-spec
958/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
959/// objc-instance-variable-decl-list ';'
960///
961/// objc-visibility-spec:
962/// @private
963/// @protected
964/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000965/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000966///
967/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000968/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000969///
Chris Lattner83f095c2009-03-28 19:18:32 +0000970void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000971 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +0000972 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000973 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000974 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000975
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000976 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000977
Steve Naroff00433d32007-08-21 21:17:12 +0000978 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000979
Steve Naroff00433d32007-08-21 21:17:12 +0000980 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000981 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000982 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000983
Steve Naroff00433d32007-08-21 21:17:12 +0000984 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000985 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000986 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000987 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +0000988 ConsumeToken();
989 continue;
990 }
Mike Stump11289f42009-09-09 15:08:12 +0000991
Steve Naroff00433d32007-08-21 21:17:12 +0000992 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000993 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000994 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +0000995
996 if (Tok.is(tok::code_completion)) {
997 Actions.CodeCompleteObjCAtVisibility(CurScope);
998 ConsumeToken();
999 }
1000
Steve Naroff7c348172007-08-23 18:16:40 +00001001 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001002 case tok::objc_private:
1003 case tok::objc_public:
1004 case tok::objc_protected:
1005 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001006 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001007 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001008 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001009 default:
1010 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001011 continue;
1012 }
1013 }
Mike Stump11289f42009-09-09 15:08:12 +00001014
Douglas Gregor48d46252010-01-13 21:54:15 +00001015 if (Tok.is(tok::code_completion)) {
1016 Actions.CodeCompleteOrdinaryName(CurScope,
1017 Action::CCC_ObjCInstanceVariableList);
1018 ConsumeToken();
1019 }
1020
John McCallcfefb6d2009-11-03 02:38:08 +00001021 struct ObjCIvarCallback : FieldCallback {
1022 Parser &P;
1023 DeclPtrTy IDecl;
1024 tok::ObjCKeywordKind visibility;
1025 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1026
1027 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1028 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1029 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1030 }
1031
1032 DeclPtrTy invoke(FieldDeclarator &FD) {
1033 // Install the declarator into the interface decl.
1034 DeclPtrTy Field
1035 = P.Actions.ActOnIvar(P.CurScope,
1036 FD.D.getDeclSpec().getSourceRange().getBegin(),
1037 IDecl, FD.D, FD.BitfieldSize, visibility);
1038 AllIvarDecls.push_back(Field);
1039 return Field;
1040 }
1041 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1042
Chris Lattnera12405b2008-04-10 06:46:29 +00001043 // Parse all the comma separated declarators.
1044 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001045 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001046
Chris Lattner0ef13522007-10-09 17:51:17 +00001047 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001048 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001049 } else {
1050 Diag(Tok, diag::err_expected_semi_decl_list);
1051 // Skip to end of block or statement
1052 SkipUntil(tok::r_brace, true, true);
1053 }
1054 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001055 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001056 // Call ActOnFields() even if we don't have any decls. This is useful
1057 // for code rewriting tools that need to be aware of the empty list.
1058 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001059 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001060 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001061 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001062}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001063
1064/// objc-protocol-declaration:
1065/// objc-protocol-definition
1066/// objc-protocol-forward-reference
1067///
1068/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001069/// @protocol identifier
1070/// objc-protocol-refs[opt]
1071/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001072/// @end
1073///
1074/// objc-protocol-forward-reference:
1075/// @protocol identifier-list ';'
1076///
1077/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001078/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001079/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001080Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1081 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001082 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001083 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1084 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001085
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001086 if (Tok.is(tok::code_completion)) {
1087 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1088 ConsumeToken();
1089 }
1090
Chris Lattner0ef13522007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001092 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001093 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001094 }
1095 // Save the protocol name, then consume it.
1096 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1097 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattner0ef13522007-10-09 17:51:17 +00001099 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001100 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001101 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001102 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001103 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001104 }
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner0ef13522007-10-09 17:51:17 +00001106 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001107 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1108 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1109
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001110 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001111 while (1) {
1112 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001113 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001114 Diag(Tok, diag::err_expected_ident);
1115 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001116 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001117 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001118 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1119 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001120 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner0ef13522007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001123 break;
1124 }
1125 // Consume the ';'.
1126 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001127 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001128
Steve Naroff93eb5f12007-10-10 17:32:04 +00001129 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001130 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001131 ProtocolRefs.size(),
1132 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001133 }
Mike Stump11289f42009-09-09 15:08:12 +00001134
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001135 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001136 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001137
Chris Lattner83f095c2009-03-28 19:18:32 +00001138 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001139 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001140 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001141 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1142 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001143 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001144
Chris Lattner83f095c2009-03-28 19:18:32 +00001145 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001146 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001147 ProtocolRefs.data(),
1148 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001149 ProtocolLocs.data(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001150 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001151 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001152 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001153}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001154
1155/// objc-implementation:
1156/// objc-class-implementation-prologue
1157/// objc-category-implementation-prologue
1158///
1159/// objc-class-implementation-prologue:
1160/// @implementation identifier objc-superclass[opt]
1161/// objc-class-instance-variables[opt]
1162///
1163/// objc-category-implementation-prologue:
1164/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001165Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001166 SourceLocation atLoc) {
1167 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1168 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1169 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001170
Douglas Gregor49c22a72009-11-18 16:26:39 +00001171 // Code completion after '@implementation'.
1172 if (Tok.is(tok::code_completion)) {
1173 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1174 ConsumeToken();
1175 }
1176
Chris Lattner0ef13522007-10-09 17:51:17 +00001177 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001178 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001179 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001180 }
1181 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001182 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001183 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001184
1185 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001186 // we have a category implementation.
1187 SourceLocation lparenLoc = ConsumeParen();
1188 SourceLocation categoryLoc, rparenLoc;
1189 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001190
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001191 if (Tok.is(tok::code_completion)) {
1192 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1193 ConsumeToken();
1194 }
1195
Chris Lattner0ef13522007-10-09 17:51:17 +00001196 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001197 categoryId = Tok.getIdentifierInfo();
1198 categoryLoc = ConsumeToken();
1199 } else {
1200 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001201 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001202 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001203 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001204 Diag(Tok, diag::err_expected_rparen);
1205 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001206 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001207 }
1208 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001209 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001210 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001211 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001212 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001213 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001214 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001215 }
1216 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001217 SourceLocation superClassLoc;
1218 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001219 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001220 // We have a super class
1221 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001222 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001223 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001224 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001225 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001226 superClassId = Tok.getIdentifierInfo();
1227 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001228 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001229 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001230 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001231 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001232
Steve Naroff33a1e802007-10-29 21:38:07 +00001233 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001234 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
1235 tok::objc_protected, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001236 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001237 PendingObjCImpDecl.push_back(ObjCImpDecl);
1238
Chris Lattner83f095c2009-03-28 19:18:32 +00001239 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001240}
Steve Naroff33a1e802007-10-29 21:38:07 +00001241
Ted Kremenekc7c64312010-01-07 01:20:12 +00001242Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001243 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1244 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001245 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001246 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001247 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001248 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001249 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001250 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001251 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001252 else {
1253 // missing @implementation
1254 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1255 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001256 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001257}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001258
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001259Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001260 if (PendingObjCImpDecl.empty())
1261 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1262 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001263 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001264 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1265}
1266
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001267/// compatibility-alias-decl:
1268/// @compatibility_alias alias-name class-name ';'
1269///
Chris Lattner83f095c2009-03-28 19:18:32 +00001270Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001271 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1272 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1273 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001274 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001275 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001276 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001277 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001278 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1279 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001280 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001281 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001282 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001283 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001284 IdentifierInfo *classId = Tok.getIdentifierInfo();
1285 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1286 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001287 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001288 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001289 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001290 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1291 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001292}
1293
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001294/// property-synthesis:
1295/// @synthesize property-ivar-list ';'
1296///
1297/// property-ivar-list:
1298/// property-ivar
1299/// property-ivar-list ',' property-ivar
1300///
1301/// property-ivar:
1302/// identifier
1303/// identifier '=' identifier
1304///
Chris Lattner83f095c2009-03-28 19:18:32 +00001305Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001306 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1307 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001308 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001309
Douglas Gregor88e72a02009-11-18 19:45:45 +00001310 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001311 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001312 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001313 ConsumeToken();
1314 }
1315
Douglas Gregor88e72a02009-11-18 19:45:45 +00001316 if (Tok.isNot(tok::identifier)) {
1317 Diag(Tok, diag::err_synthesized_property_name);
1318 SkipUntil(tok::semi);
1319 return DeclPtrTy();
1320 }
1321
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001322 IdentifierInfo *propertyIvar = 0;
1323 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1324 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001325 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001326 // property '=' ivar-name
1327 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001328
1329 if (Tok.is(tok::code_completion)) {
1330 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1331 ObjCImpDecl);
1332 ConsumeToken();
1333 }
1334
Chris Lattner0ef13522007-10-09 17:51:17 +00001335 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001336 Diag(Tok, diag::err_expected_ident);
1337 break;
1338 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001339 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001340 ConsumeToken(); // consume ivar-name
1341 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001342 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1343 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001344 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001345 break;
1346 ConsumeToken(); // consume ','
1347 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001348 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001349 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001350 SkipUntil(tok::semi);
1351 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001352 else
1353 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001354 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001355}
1356
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001357/// property-dynamic:
1358/// @dynamic property-list
1359///
1360/// property-list:
1361/// identifier
1362/// property-list ',' identifier
1363///
Chris Lattner83f095c2009-03-28 19:18:32 +00001364Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001365 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1366 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1367 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001368 while (true) {
1369 if (Tok.is(tok::code_completion)) {
1370 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1371 ConsumeToken();
1372 }
1373
1374 if (Tok.isNot(tok::identifier)) {
1375 Diag(Tok, diag::err_expected_ident);
1376 SkipUntil(tok::semi);
1377 return DeclPtrTy();
1378 }
1379
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001380 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1381 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1382 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1383 propertyId, 0);
1384
Chris Lattner0ef13522007-10-09 17:51:17 +00001385 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001386 break;
1387 ConsumeToken(); // consume ','
1388 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001389 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001390 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001391 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001392}
Mike Stump11289f42009-09-09 15:08:12 +00001393
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001394/// objc-throw-statement:
1395/// throw expression[opt];
1396///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001397Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001398 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001399 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001401 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001402 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001403 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001404 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001405 }
1406 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001407 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001408 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001409}
1410
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001411/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001412/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001413///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001414Parser::OwningStmtResult
1415Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001416 ConsumeToken(); // consume synchronized
1417 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001418 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001419 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001420 }
1421 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001422 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001423 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001424 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001425 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001426 }
1427 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001428 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001429 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001430 }
1431 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001432 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001433 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001434 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001435 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001436 // Enter a scope to hold everything within the compound stmt. Compound
1437 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001438 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001439
Sebastian Redl042ad952008-12-11 19:30:53 +00001440 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001441
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001442 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001443 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001444 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001445 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001446}
1447
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001448/// objc-try-catch-statement:
1449/// @try compound-statement objc-catch-list[opt]
1450/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1451///
1452/// objc-catch-list:
1453/// @catch ( parameter-declaration ) compound-statement
1454/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1455/// catch-parameter-declaration:
1456/// parameter-declaration
1457/// '...' [OBJC2]
1458///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001459Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001460 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001461
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001462 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001463 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001464 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001465 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001466 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001467 OwningStmtResult CatchStmts(Actions);
1468 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001469 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001470 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001471 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001472 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001473 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001474
Chris Lattner0ef13522007-10-09 17:51:17 +00001475 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001476 // At this point, we need to lookahead to determine if this @ is the start
1477 // of an @catch or @finally. We don't want to consume the @ token if this
1478 // is an @try or @encode or something else.
1479 Token AfterAt = GetLookAheadToken(1);
1480 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1481 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1482 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001483
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001484 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001485 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001486 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001487 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001488 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001489 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001490 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001491 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001492 DeclSpec DS;
1493 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001494 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001495 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001496 // we must accept either 'declarator' or 'abstract-declarator' here.
1497 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1498 ParseDeclarator(ParmDecl);
1499
1500 // Inform the actions module about the parameter declarator, so it
1501 // gets added to the current scope.
Fariborz Jahaniane1651912010-02-03 00:32:51 +00001502 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff371b8fb2009-03-03 19:52:17 +00001503 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian08d614d2010-02-03 00:01:43 +00001504 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroffe6016792008-02-05 21:27:35 +00001505 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001506 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001507
Steve Naroff65a00892009-04-07 22:56:58 +00001508 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001509
Steve Naroff65a00892009-04-07 22:56:58 +00001510 if (Tok.is(tok::r_paren))
1511 RParenLoc = ConsumeParen();
1512 else // Skip over garbage, until we get to ')'. Eat the ')'.
1513 SkipUntil(tok::r_paren, true, false);
1514
Sebastian Redlc13f2682008-12-09 20:22:58 +00001515 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001516 if (Tok.is(tok::l_brace))
1517 CatchBody = ParseCompoundStatementBody();
1518 else
1519 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001520 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001521 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001522 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001523 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001524 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001525 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001526 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1527 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001528 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001529 }
1530 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001531 } else {
1532 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001533 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001534 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001535
Sebastian Redlc13f2682008-12-09 20:22:58 +00001536 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001537 if (Tok.is(tok::l_brace))
1538 FinallyBody = ParseCompoundStatementBody();
1539 else
1540 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001541 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001542 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001543 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001544 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001545 catch_or_finally_seen = true;
1546 break;
1547 }
1548 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001549 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001550 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001551 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001552 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001553 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1554 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001555}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001556
Steve Naroff09bf8152007-09-06 21:24:23 +00001557/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001558///
Chris Lattner83f095c2009-03-28 19:18:32 +00001559Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1560 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001561
Chris Lattnereae6cb62009-03-05 08:00:35 +00001562 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1563 PP.getSourceManager(),
1564 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001565
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001566 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001567 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001568 if (ObjCImpDecl) {
1569 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001570 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001571 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001572 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001573 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001574
Steve Naroffbb875722007-11-11 19:54:21 +00001575 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001576 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001577 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001578
Steve Naroffbb875722007-11-11 19:54:21 +00001579 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1580 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001581
Steve Naroffbb875722007-11-11 19:54:21 +00001582 // If we didn't find the '{', bail out.
1583 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001584 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001585 }
Steve Naroffbb875722007-11-11 19:54:21 +00001586 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001587
Steve Naroffbb875722007-11-11 19:54:21 +00001588 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001589 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001590
Steve Naroffbb875722007-11-11 19:54:21 +00001591 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001592 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001593 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001594
1595 OwningStmtResult FnBody(ParseCompoundStatementBody());
1596
Steve Naroffbb875722007-11-11 19:54:21 +00001597 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001598 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001599 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1600 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001601
Steve Naroffb94d7f62009-03-02 22:00:56 +00001602 // TODO: Pass argument information.
1603 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001604
Steve Naroffbb875722007-11-11 19:54:21 +00001605 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001606 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001607
Steve Naroff7b8fa472007-11-13 23:01:27 +00001608 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001609}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001610
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001611Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001612 if (Tok.is(tok::code_completion)) {
1613 Actions.CodeCompleteObjCAtStatement(CurScope);
1614 ConsumeToken();
1615 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001616 }
1617
1618 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001619 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001620
1621 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001622 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001623
1624 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001625 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001626
Sebastian Redl90893182008-12-11 22:33:27 +00001627 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001628 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001629 // If the expression is invalid, skip ahead to the next semicolon. Not
1630 // doing this opens us up to the possibility of infinite loops if
1631 // ParseExpression does not consume any tokens.
1632 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001633 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001634 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001635
Steve Naroffe6016792008-02-05 21:27:35 +00001636 // Otherwise, eat the semicolon.
1637 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001638 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001639}
1640
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001641Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001642 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001643 case tok::code_completion:
1644 Actions.CodeCompleteObjCAtExpression(CurScope);
1645 ConsumeToken();
1646 return ExprError();
1647
Chris Lattnere002fbe2007-12-12 01:04:12 +00001648 case tok::string_literal: // primary-expression: string-literal
1649 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001650 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001651 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001652 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001653 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001654
Chris Lattner197a3012008-08-05 06:19:09 +00001655 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1656 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001657 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001658 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001659 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001660 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001661 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001662 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001663 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001664 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001665 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001666}
1667
Mike Stump11289f42009-09-09 15:08:12 +00001668/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001669/// '[' objc-receiver objc-message-args ']'
1670///
1671/// objc-receiver:
1672/// expression
1673/// class-name
1674/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001675Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001676 assert(Tok.is(tok::l_square) && "'[' expected");
1677 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1678
1679 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001680 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001681 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001682 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1683 SourceLocation NameLoc = ConsumeToken();
1684 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1685 ExprArg(Actions));
1686 }
Chris Lattner8f697062008-01-25 18:59:06 +00001687 }
1688
Sebastian Redl59b5e512008-12-11 21:36:32 +00001689 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001690 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001691 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001692 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001693 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001694
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001695 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001696 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001697}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001698
Chris Lattner8f697062008-01-25 18:59:06 +00001699/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1700/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001701///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001702/// objc-message-args:
1703/// objc-selector
1704/// objc-keywordarg-list
1705///
1706/// objc-keywordarg-list:
1707/// objc-keywordarg
1708/// objc-keywordarg-list objc-keywordarg
1709///
Mike Stump11289f42009-09-09 15:08:12 +00001710/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001711/// selector-name[opt] ':' objc-keywordexpr
1712///
1713/// objc-keywordexpr:
1714/// nonempty-expr-list
1715///
1716/// nonempty-expr-list:
1717/// assignment-expression
1718/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001719///
1720Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001721Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001722 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001723 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001724 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001725 if (Tok.is(tok::code_completion)) {
1726 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001727 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1728 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001729 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001730 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1731 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001732 ConsumeToken();
1733 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001734
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001735 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001736 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001737 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001738
Anders Carlsson978f08d2009-02-14 18:21:46 +00001739 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001740
Steve Narofff73590d2007-09-27 14:38:14 +00001741 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001742 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001743
Chris Lattner0ef13522007-10-09 17:51:17 +00001744 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001745 while (1) {
1746 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001747 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001748
Chris Lattner0ef13522007-10-09 17:51:17 +00001749 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001750 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001751 // We must manually skip to a ']', otherwise the expression skipper will
1752 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1753 // the enclosing expression.
1754 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001755 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001756 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001757
Steve Narofff73590d2007-09-27 14:38:14 +00001758 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001759 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001760 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001761 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001762 // We must manually skip to a ']', otherwise the expression skipper will
1763 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1764 // the enclosing expression.
1765 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001766 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001767 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001768
Steve Naroff486760a2007-09-17 20:25:27 +00001769 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001770 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001771
Douglas Gregor1b605f72009-11-19 01:08:35 +00001772 // Code completion after each argument.
1773 if (Tok.is(tok::code_completion)) {
1774 if (ReceiverName)
1775 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1776 KeyIdents.data(),
1777 KeyIdents.size());
1778 else
1779 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1780 KeyIdents.data(),
1781 KeyIdents.size());
1782 ConsumeToken();
1783 }
1784
Steve Naroff486760a2007-09-17 20:25:27 +00001785 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001786 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001787 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001788 break;
1789 // We have a selector or a colon, continue parsing.
1790 }
1791 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001792 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001793 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001794 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001795 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001796 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001797 // We must manually skip to a ']', otherwise the expression skipper will
1798 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1799 // the enclosing expression.
1800 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001801 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001802 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001803
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001804 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001805 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001806 }
1807 } else if (!selIdent) {
1808 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001809
Chris Lattner197a3012008-08-05 06:19:09 +00001810 // We must manually skip to a ']', otherwise the expression skipper will
1811 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1812 // the enclosing expression.
1813 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001814 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001815 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001816
Chris Lattner0ef13522007-10-09 17:51:17 +00001817 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001818 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001819 // We must manually skip to a ']', otherwise the expression skipper will
1820 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1821 // the enclosing expression.
1822 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001823 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001824 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001825
Chris Lattner8f697062008-01-25 18:59:06 +00001826 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001827
Steve Naroffe61bfa82007-10-05 18:42:47 +00001828 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001829 if (nKeys == 0)
1830 KeyIdents.push_back(selIdent);
1831 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001832
Chris Lattner5700fab2007-10-07 02:00:24 +00001833 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001834 if (ReceiverName)
1835 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001836 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001837 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001838 KeyExprs.take(), KeyExprs.size()));
1839 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001840 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001841 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001842}
1843
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001844Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001845 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001846 if (Res.isInvalid()) return move(Res);
1847
Chris Lattnere002fbe2007-12-12 01:04:12 +00001848 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1849 // expressions. At this point, we know that the only valid thing that starts
1850 // with '@' is an @"".
1851 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001852 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001853 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001854 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001855
Chris Lattnere002fbe2007-12-12 01:04:12 +00001856 while (Tok.is(tok::at)) {
1857 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001858
Sebastian Redlc13f2682008-12-09 20:22:58 +00001859 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001860 if (!isTokenStringLiteral())
1861 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001862
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001863 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001864 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001866
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001867 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001868 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001869
1870 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1871 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001872}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001873
1874/// objc-encode-expression:
1875/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001876Parser::OwningExprResult
1877Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001878 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001879
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001880 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001881
Chris Lattner197a3012008-08-05 06:19:09 +00001882 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001883 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1884
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001885 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001886
Douglas Gregor220cac52009-02-18 17:45:20 +00001887 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001888
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001889 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001890
Douglas Gregor220cac52009-02-18 17:45:20 +00001891 if (Ty.isInvalid())
1892 return ExprError();
1893
Mike Stump11289f42009-09-09 15:08:12 +00001894 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001895 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001896}
Anders Carlssone01493d2007-08-23 15:25:28 +00001897
1898/// objc-protocol-expression
1899/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001900Parser::OwningExprResult
1901Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001902 SourceLocation ProtoLoc = 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) << "@protocol");
1906
Anders Carlssone01493d2007-08-23 15:25:28 +00001907 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001908
Chris Lattner197a3012008-08-05 06:19:09 +00001909 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001910 return ExprError(Diag(Tok, diag::err_expected_ident));
1911
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001912 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001913 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001914
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001915 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001916
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001917 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1918 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001919}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001920
1921/// objc-selector-expression
1922/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001923Parser::OwningExprResult
1924Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001925 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001926
Chris Lattner197a3012008-08-05 06:19:09 +00001927 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001928 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1929
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001930 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001931 SourceLocation LParenLoc = ConsumeParen();
1932 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001933 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001934 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1935 return ExprError(Diag(Tok, diag::err_expected_ident));
1936
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001937 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001938 unsigned nColons = 0;
1939 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001940 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001941 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001942 return ExprError(Diag(Tok, diag::err_expected_colon));
1943
Chris Lattner5e530bc2007-12-27 19:57:00 +00001944 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001945 ConsumeToken(); // Eat the ':'.
1946 if (Tok.is(tok::r_paren))
1947 break;
1948 // Check for another keyword selector.
1949 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001950 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001951 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001952 if (!SelIdent && Tok.isNot(tok::colon))
1953 break;
1954 }
Steve Naroff152dd812007-12-05 22:21:29 +00001955 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001956 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001957 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001958 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1959 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001960 }