blob: 7a8aed7a21fa828b24d960891cd351881ed8524d [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01: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 Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
Chris Lattner891dca62008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-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 Lattnerb28317a2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000032
Douglas Gregorc464ae82009-12-07 09:27:33 +000033 if (Tok.is(tok::code_completion)) {
34 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
35 ConsumeToken();
36 }
37
Steve Naroff861cf3e2007-08-23 18:16:40 +000038 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-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 Lattnerb28317a2009-03-28 19:18:32 +000058 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000059 }
60}
61
62///
Mike Stump1eb44332009-09-09 15:08:12 +000063/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000064/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000065///
Chris Lattnerb28317a2009-03-28 19:18:32 +000066Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 ConsumeToken(); // the identifier "class"
68 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000069 llvm::SmallVector<SourceLocation, 8> ClassLocs;
70
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000073 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000074 Diag(Tok, diag::err_expected_ident);
75 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000076 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000077 }
Reid Spencer5f016e22007-07-11 17:01:13 +000078 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000079 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattnerdf195262007-10-09 17:51:17 +000082 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000083 break;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Reid Spencer5f016e22007-07-11 17:01:13 +000085 ConsumeToken();
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Consume the ';'.
89 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000090 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekc09cba62009-11-17 23:12:20 +000092 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93 ClassLocs.data(),
94 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Steve Naroffdac269b2007-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 Stump1eb44332009-09-09 15:08:12 +0000103/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000104/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-interface-decl-list
107/// @end
108///
109/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000110/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-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 Lattnerb28317a2009-03-28 19:18:32 +0000125Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000126 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000127 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor3b49aca2009-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 Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000139 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000140 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000141
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000143 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattnerdf195262007-10-09 17:51:17 +0000146 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000151 if (Tok.is(tok::code_completion)) {
152 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
153 ConsumeToken();
154 }
155
Steve Naroff527fe232007-08-23 19:56:30 +0000156 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 categoryId = Tok.getIdentifierInfo();
159 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000160 } else if (!getLang().ObjC2) {
161 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000163 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000164 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 Diag(Tok, diag::err_expected_rparen);
166 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000167 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000172 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000173 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000174 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000175 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000176 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
177 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000178 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 if (attrList) // categories don't support attributes.
181 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Jay Foadbeaaccd2009-05-21 09:52:38 +0000183 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000184 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000185 nameId, nameLoc,
186 categoryId, categoryLoc,
187 ProtocolRefs.data(),
188 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000189 ProtocolLocs.data(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000190 EndProtoLoc);
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000191 if (Tok.is(tok::l_brace))
192 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
193 atLoc);
194
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000195 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000196 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000197 }
198 // Parse a class interface.
199 IdentifierInfo *superClassId = 0;
200 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000201
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 ConsumeToken();
Douglas Gregor3b49aca2009-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 Lattnerdf195262007-10-09 17:51:17 +0000211 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000212 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000213 return DeclPtrTy();
Steve Naroffdac269b2007-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 Lattnerb28317a2009-03-28 19:18:32 +0000219 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000220 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000222 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000223 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
224 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000225 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000226
227 DeclPtrTy ClsType =
228 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000229 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000230 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000231 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000232 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattnerdf195262007-10-09 17:51:17 +0000234 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000235 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000236
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000237 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000238 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000239}
240
John McCalld0014542009-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 Naroffdac269b2007-08-20 21:31:48 +0000298/// objc-interface-decl-list:
299/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000300/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000301/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000302/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000303/// objc-interface-decl-list declaration
304/// objc-interface-decl-list ';'
305///
Steve Naroff294494e2007-08-22 16:35:03 +0000306/// objc-method-requirement: [OBJC2]
307/// @required
308/// @optional
309///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000310void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000311 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000312 llvm::SmallVector<DeclPtrTy, 32> allMethods;
313 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000314 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000315 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek782f2f52010-01-07 01:20:12 +0000317 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000318
Steve Naroff294494e2007-08-22 16:35:03 +0000319 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000320 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000321 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000322 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000323 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000324 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000325 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
326 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000327 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
328 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000329 continue;
330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattnere82a10f2008-10-20 05:46:22 +0000332 // Ignore excess semicolons.
333 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000334 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000335 continue;
336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattnerbc662af2008-10-20 06:10:06 +0000338 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000339 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000340 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Douglas Gregorb6ac2452010-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 Lattnere82a10f2008-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 Lattner1fd80112009-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 Stump1eb44332009-09-09 15:08:12 +0000357
Steve Naroff4985ace2007-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.
Sean Huntbbd37c62009-11-21 08:43:09 +0000360 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000361 continue;
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattnere82a10f2008-10-20 05:46:22 +0000364 // Otherwise, we have an @ directive, eat the @.
365 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-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 Lattnera2449b22008-10-20 05:57:40 +0000372 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Chris Lattnera2449b22008-10-20 05:57:40 +0000374 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000375 AtEnd.setBegin(AtLoc);
376 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000377 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000378 } else if (DirectiveKind == tok::objc_not_keyword) {
379 Diag(Tok, diag::err_objc_unknown_at);
380 SkipUntil(tok::semi);
381 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattnerbc662af2008-10-20 06:10:06 +0000384 // Eat the identifier.
385 ConsumeToken();
386
Chris Lattnera2449b22008-10-20 05:57:40 +0000387 switch (DirectiveKind) {
388 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000389 // FIXME: If someone forgets an @end on a protocol, this loop will
390 // continue to eat up tons of stuff and spew lots of nonsense errors. It
391 // would probably be better to bail out if we saw an @class or @interface
392 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000393 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000394 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000395 SkipUntil(tok::r_brace, tok::at);
396 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattnera2449b22008-10-20 05:57:40 +0000398 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000399 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000400 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000401 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000402 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000403 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000404 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000405 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000406 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattnera2449b22008-10-20 05:57:40 +0000408 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000409 if (!getLang().ObjC2)
410 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
411
Chris Lattnere82a10f2008-10-20 05:46:22 +0000412 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000413 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000414 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000415 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
416 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000417
John McCalld0014542009-12-03 22:31:13 +0000418 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
419 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000420
Chris Lattnere82a10f2008-10-20 05:46:22 +0000421 // Parse all the comma separated declarators.
422 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000423 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000425 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
426 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000427 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000428 }
Steve Naroff294494e2007-08-22 16:35:03 +0000429 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000430
431 // We break out of the big loop in two cases: when we see @end or when we see
432 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000433 if (Tok.is(tok::code_completion)) {
434 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
435 ConsumeToken();
436 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000437 ConsumeToken(); // the "end" identifier
438 else
439 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattnera2449b22008-10-20 05:57:40 +0000441 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000442 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek782f2f52010-01-07 01:20:12 +0000443 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000444 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000445 allProperties.data(), allProperties.size(),
446 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000447}
448
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000449/// Parse property attribute declarations.
450///
451/// property-attr-decl: '(' property-attrlist ')'
452/// property-attrlist:
453/// property-attribute
454/// property-attrlist ',' property-attribute
455/// property-attribute:
456/// getter '=' identifier
457/// setter '=' identifier ':'
458/// readonly
459/// readwrite
460/// assign
461/// retain
462/// copy
463/// nonatomic
464///
Douglas Gregor4ad96852009-11-19 07:41:15 +0000465void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
466 DeclPtrTy *Methods,
467 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000468 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000469 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000471 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000472 if (Tok.is(tok::code_completion)) {
Douglas Gregora93b1082009-11-18 23:08:07 +0000473 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroffece8e712009-10-08 21:55:05 +0000474 ConsumeToken();
475 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000476 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000478 // If this is not an identifier at all, bail out early.
479 if (II == 0) {
480 MatchRHSPunctuation(tok::r_paren, LHSLoc);
481 return;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner156b0612008-10-20 07:37:22 +0000484 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner92e62b02008-11-20 04:42:34 +0000486 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000487 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000488 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000489 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000490 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000491 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000492 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000493 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000494 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000495 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000496 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000497 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000498 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000499 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000500 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
501 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000502 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor4ad96852009-11-19 07:41:15 +0000504 if (Tok.is(tok::code_completion)) {
505 if (II->getNameStart()[0] == 's')
506 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
507 Methods, NumMethods);
508 else
509 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
510 Methods, NumMethods);
511 ConsumeToken();
512 }
513
Chris Lattner8ca329c2008-10-20 07:24:39 +0000514 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000515 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000516 SkipUntil(tok::r_paren);
517 return;
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Daniel Dunbare013d682009-10-18 20:26:12 +0000520 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000521 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
522 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000523 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000525 if (ExpectAndConsume(tok::colon,
526 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000527 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000528 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000529 } else {
530 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
531 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000532 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000533 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000534 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000535 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000536 SkipUntil(tok::r_paren);
537 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Chris Lattner156b0612008-10-20 07:37:22 +0000540 if (Tok.isNot(tok::comma))
541 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattner156b0612008-10-20 07:37:22 +0000543 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner156b0612008-10-20 07:37:22 +0000546 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000547}
548
Steve Naroff3536b442007-09-06 21:24:23 +0000549/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000550/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000551/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000552///
553/// objc-instance-method: '-'
554/// objc-class-method: '+'
555///
Steve Naroff4985ace2007-08-22 18:35:33 +0000556/// objc-method-attributes: [OBJC2]
557/// __attribute__((deprecated))
558///
Mike Stump1eb44332009-09-09 15:08:12 +0000559Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000560 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000561 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000562
Mike Stump1eb44332009-09-09 15:08:12 +0000563 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000564 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Chris Lattnerb28317a2009-03-28 19:18:32 +0000566 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000567 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000568 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000569 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000570}
571
572/// objc-selector:
573/// identifier
574/// one of
575/// enum struct union if else while do for switch case default
576/// break continue return goto asm sizeof typeof __alignof
577/// unsigned long const short volatile signed restrict _Complex
578/// in out inout bycopy byref oneway int char float double void _Bool
579///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000580IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000581 switch (Tok.getKind()) {
582 default:
583 return 0;
584 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000585 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000586 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000587 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000588 case tok::kw_break:
589 case tok::kw_case:
590 case tok::kw_catch:
591 case tok::kw_char:
592 case tok::kw_class:
593 case tok::kw_const:
594 case tok::kw_const_cast:
595 case tok::kw_continue:
596 case tok::kw_default:
597 case tok::kw_delete:
598 case tok::kw_do:
599 case tok::kw_double:
600 case tok::kw_dynamic_cast:
601 case tok::kw_else:
602 case tok::kw_enum:
603 case tok::kw_explicit:
604 case tok::kw_export:
605 case tok::kw_extern:
606 case tok::kw_false:
607 case tok::kw_float:
608 case tok::kw_for:
609 case tok::kw_friend:
610 case tok::kw_goto:
611 case tok::kw_if:
612 case tok::kw_inline:
613 case tok::kw_int:
614 case tok::kw_long:
615 case tok::kw_mutable:
616 case tok::kw_namespace:
617 case tok::kw_new:
618 case tok::kw_operator:
619 case tok::kw_private:
620 case tok::kw_protected:
621 case tok::kw_public:
622 case tok::kw_register:
623 case tok::kw_reinterpret_cast:
624 case tok::kw_restrict:
625 case tok::kw_return:
626 case tok::kw_short:
627 case tok::kw_signed:
628 case tok::kw_sizeof:
629 case tok::kw_static:
630 case tok::kw_static_cast:
631 case tok::kw_struct:
632 case tok::kw_switch:
633 case tok::kw_template:
634 case tok::kw_this:
635 case tok::kw_throw:
636 case tok::kw_true:
637 case tok::kw_try:
638 case tok::kw_typedef:
639 case tok::kw_typeid:
640 case tok::kw_typename:
641 case tok::kw_typeof:
642 case tok::kw_union:
643 case tok::kw_unsigned:
644 case tok::kw_using:
645 case tok::kw_virtual:
646 case tok::kw_void:
647 case tok::kw_volatile:
648 case tok::kw_wchar_t:
649 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000650 case tok::kw__Bool:
651 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000652 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000653 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000654 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000655 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000656 }
Steve Naroff294494e2007-08-22 16:35:03 +0000657}
658
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000659/// objc-for-collection-in: 'in'
660///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000661bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000662 // FIXME: May have to do additional look-ahead to only allow for
663 // valid tokens following an 'in'; such as an identifier, unary operators,
664 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000665 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000666 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000667}
668
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000670/// qualifier list and builds their bitmask representation in the input
671/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000672///
673/// objc-type-qualifiers:
674/// objc-type-qualifier
675/// objc-type-qualifiers objc-type-qualifier
676///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000677void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000678 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000679 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000680 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattnere8b724d2007-12-12 06:56:32 +0000682 const IdentifierInfo *II = Tok.getIdentifierInfo();
683 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000685 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000688 switch (i) {
689 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
691 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
692 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
693 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
694 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
695 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000696 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000698 ConsumeToken();
699 II = 0;
700 break;
701 }
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Chris Lattnere8b724d2007-12-12 06:56:32 +0000703 // If this wasn't a recognized qualifier, bail out.
704 if (II) return;
705 }
706}
707
708/// objc-type-name:
709/// '(' objc-type-qualifiers[opt] type-name ')'
710/// '(' objc-type-qualifiers[opt] ')'
711///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000712Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000713 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner4a76b292008-10-22 03:52:06 +0000715 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000716 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000718 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000719 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000720
Chris Lattner4a76b292008-10-22 03:52:06 +0000721 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000722 if (isTypeSpecifierQualifier()) {
723 TypeResult TypeSpec = ParseTypeName();
724 if (!TypeSpec.isInvalid())
725 Ty = TypeSpec.get();
726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Steve Naroffd7333c22008-10-21 14:15:04 +0000728 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000729 ConsumeParen();
730 else if (Tok.getLocation() == TypeStartLoc) {
731 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000732 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000733 SkipUntil(tok::r_paren);
734 } else {
735 // Otherwise, we found *something*, but didn't get a ')' in the right
736 // place. Emit an error then return what we have as the type.
737 MatchRHSPunctuation(tok::r_paren, LParenLoc);
738 }
Steve Narofff28b2642007-09-05 23:30:30 +0000739 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000740}
741
742/// objc-method-decl:
743/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000744/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000745/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000746/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000747///
748/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000749/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000750/// objc-keyword-selector objc-keyword-decl
751///
752/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000753/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
754/// objc-selector ':' objc-keyword-attributes[opt] identifier
755/// ':' objc-type-name objc-keyword-attributes[opt] identifier
756/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000757///
Steve Naroff4985ace2007-08-22 18:35:33 +0000758/// objc-parmlist:
759/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000760///
Steve Naroff4985ace2007-08-22 18:35:33 +0000761/// objc-parms:
762/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000763///
Steve Naroff4985ace2007-08-22 18:35:33 +0000764/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000765/// , ...
766///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000767/// objc-keyword-attributes: [OBJC2]
768/// __attribute__((unused))
769///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000770Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
771 tok::TokenKind mType,
772 DeclPtrTy IDecl,
773 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000774 ParsingDeclRAIIObject PD(*this);
775
Chris Lattnere8904e92008-08-23 01:48:03 +0000776 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000777 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000779 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000780 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Ted Kremenek9e049352010-02-18 23:05:16 +0000782 // If attributes exist before the method, parse them.
783 llvm::OwningPtr<AttributeList> MethodAttrs;
784 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
785 MethodAttrs.reset(ParseGNUAttributes());
786
787 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000788 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000789 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000790
Steve Naroff84c43102009-02-11 20:43:13 +0000791 // An unnamed colon is valid.
792 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000793 Diag(Tok, diag::err_expected_selector_for_method)
794 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000795 // Skip until we get a ; or {}.
796 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000797 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000800 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000801 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000802 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000803 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000804 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
805 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Chris Lattnerff384912007-10-07 02:00:24 +0000807 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000808 DeclPtrTy Result
809 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000810 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1e377652010-02-11 02:19:13 +0000811 0, CargNames, MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000812 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000813 PD.complete(Result);
814 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000815 }
Steve Narofff28b2642007-09-05 23:30:30 +0000816
Steve Naroff68d331a2007-09-27 14:38:14 +0000817 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000818 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattnerff384912007-10-07 02:00:24 +0000820 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000821 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattnerff384912007-10-07 02:00:24 +0000823 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000824 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000825 Diag(Tok, diag::err_expected_colon);
826 break;
827 }
828 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Chris Lattnere294d3f2009-04-11 18:57:04 +0000830 ArgInfo.Type = 0;
831 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
832 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
833
Chris Lattnerff384912007-10-07 02:00:24 +0000834 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000835 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000836 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000837 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000838
Chris Lattnerdf195262007-10-09 17:51:17 +0000839 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000840 Diag(Tok, diag::err_expected_ident); // missing argument name.
841 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattnere294d3f2009-04-11 18:57:04 +0000844 ArgInfo.Name = Tok.getIdentifierInfo();
845 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000846 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnere294d3f2009-04-11 18:57:04 +0000848 ArgInfos.push_back(ArgInfo);
849 KeyIdents.push_back(SelIdent);
850
Chris Lattnerff384912007-10-07 02:00:24 +0000851 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000852 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000853 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000854 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000855 break;
856 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Steve Naroff335eafa2007-11-15 12:35:21 +0000859 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattnerff384912007-10-07 02:00:24 +0000861 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000862 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000863 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000864 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000865 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000866 ConsumeToken();
867 break;
868 }
Chris Lattnerff384912007-10-07 02:00:24 +0000869 DeclSpec DS;
870 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000871 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000872 Declarator ParmDecl(DS, Declarator::PrototypeContext);
873 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000874 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Chris Lattnerff384912007-10-07 02:00:24 +0000877 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000878 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000879 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000880 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
881 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000883 if (KeyIdents.size() == 0)
884 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000885 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
886 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000887 DeclPtrTy Result
888 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000889 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1e377652010-02-11 02:19:13 +0000890 &ArgInfos[0], CargNames,
891 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000892 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000893 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000894
895 // Delete referenced AttributeList objects.
896 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
897 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
898 delete I->ArgAttrs;
899
John McCall54abf7d2009-11-04 02:18:39 +0000900 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000901}
902
Steve Naroffdac269b2007-08-20 21:31:48 +0000903/// objc-protocol-refs:
904/// '<' identifier-list '>'
905///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000906bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000907ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000908 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
909 bool WarnOnDeclarations,
910 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000911 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000913 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Chris Lattnere13b9592008-07-26 04:03:38 +0000915 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattnere13b9592008-07-26 04:03:38 +0000917 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000918 if (Tok.is(tok::code_completion)) {
919 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
920 ProtocolIdents.size());
921 ConsumeToken();
922 }
923
Chris Lattnere13b9592008-07-26 04:03:38 +0000924 if (Tok.isNot(tok::identifier)) {
925 Diag(Tok, diag::err_expected_ident);
926 SkipUntil(tok::greater);
927 return true;
928 }
929 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
930 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000931 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000932 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattnere13b9592008-07-26 04:03:38 +0000934 if (Tok.isNot(tok::comma))
935 break;
936 ConsumeToken();
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Chris Lattnere13b9592008-07-26 04:03:38 +0000939 // Consume the '>'.
940 if (Tok.isNot(tok::greater)) {
941 Diag(Tok, diag::err_expected_greater);
942 return true;
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattnere13b9592008-07-26 04:03:38 +0000945 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattnere13b9592008-07-26 04:03:38 +0000947 // Convert the list of protocols identifiers into a list of protocol decls.
948 Actions.FindProtocolDeclaration(WarnOnDeclarations,
949 &ProtocolIdents[0], ProtocolIdents.size(),
950 Protocols);
951 return false;
952}
953
Steve Naroffdac269b2007-08-20 21:31:48 +0000954/// objc-class-instance-variables:
955/// '{' objc-instance-variable-decl-list[opt] '}'
956///
957/// objc-instance-variable-decl-list:
958/// objc-visibility-spec
959/// objc-instance-variable-decl ';'
960/// ';'
961/// objc-instance-variable-decl-list objc-visibility-spec
962/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
963/// objc-instance-variable-decl-list ';'
964///
965/// objc-visibility-spec:
966/// @private
967/// @protected
968/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000969/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000970///
971/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000972/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000973///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000974void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000975 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +0000976 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000977 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000978 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000979
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000980 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000981
Steve Naroffddbff782007-08-21 21:17:12 +0000982 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Steve Naroffddbff782007-08-21 21:17:12 +0000984 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000985 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000986 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Steve Naroffddbff782007-08-21 21:17:12 +0000988 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000989 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000990 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000991 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +0000992 ConsumeToken();
993 continue;
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Steve Naroffddbff782007-08-21 21:17:12 +0000996 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000997 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000998 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +0000999
1000 if (Tok.is(tok::code_completion)) {
1001 Actions.CodeCompleteObjCAtVisibility(CurScope);
1002 ConsumeToken();
1003 }
1004
Steve Naroff861cf3e2007-08-23 18:16:40 +00001005 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001006 case tok::objc_private:
1007 case tok::objc_public:
1008 case tok::objc_protected:
1009 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001010 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001011 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001012 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001013 default:
1014 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001015 continue;
1016 }
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001019 if (Tok.is(tok::code_completion)) {
1020 Actions.CodeCompleteOrdinaryName(CurScope,
1021 Action::CCC_ObjCInstanceVariableList);
1022 ConsumeToken();
1023 }
1024
John McCallbdd563e2009-11-03 02:38:08 +00001025 struct ObjCIvarCallback : FieldCallback {
1026 Parser &P;
1027 DeclPtrTy IDecl;
1028 tok::ObjCKeywordKind visibility;
1029 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1030
1031 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1032 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1033 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1034 }
1035
1036 DeclPtrTy invoke(FieldDeclarator &FD) {
1037 // Install the declarator into the interface decl.
1038 DeclPtrTy Field
1039 = P.Actions.ActOnIvar(P.CurScope,
1040 FD.D.getDeclSpec().getSourceRange().getBegin(),
1041 IDecl, FD.D, FD.BitfieldSize, visibility);
1042 AllIvarDecls.push_back(Field);
1043 return Field;
1044 }
1045 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1046
Chris Lattnere1359422008-04-10 06:46:29 +00001047 // Parse all the comma separated declarators.
1048 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001049 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattnerdf195262007-10-09 17:51:17 +00001051 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001052 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001053 } else {
1054 Diag(Tok, diag::err_expected_semi_decl_list);
1055 // Skip to end of block or statement
1056 SkipUntil(tok::r_brace, true, true);
1057 }
1058 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001059 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +00001060 // Call ActOnFields() even if we don't have any decls. This is useful
1061 // for code rewriting tools that need to be aware of the empty list.
1062 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001063 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001064 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001065 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001066}
Steve Naroffdac269b2007-08-20 21:31:48 +00001067
1068/// objc-protocol-declaration:
1069/// objc-protocol-definition
1070/// objc-protocol-forward-reference
1071///
1072/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001073/// @protocol identifier
1074/// objc-protocol-refs[opt]
1075/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001076/// @end
1077///
1078/// objc-protocol-forward-reference:
1079/// @protocol identifier-list ';'
1080///
1081/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001082/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001083/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001084Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1085 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001086 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001087 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1088 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregor083128f2009-11-18 04:49:41 +00001090 if (Tok.is(tok::code_completion)) {
1091 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1092 ConsumeToken();
1093 }
1094
Chris Lattnerdf195262007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001096 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001097 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001098 }
1099 // Save the protocol name, then consume it.
1100 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1101 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattnerdf195262007-10-09 17:51:17 +00001103 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001104 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001105 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001106 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001107 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattnerdf195262007-10-09 17:51:17 +00001110 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001111 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1112 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1113
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001114 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001115 while (1) {
1116 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001117 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001118 Diag(Tok, diag::err_expected_ident);
1119 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001120 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001121 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001122 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1123 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001124 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Chris Lattnerdf195262007-10-09 17:51:17 +00001126 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001127 break;
1128 }
1129 // Consume the ';'.
1130 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001131 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Steve Naroffe440eb82007-10-10 17:32:04 +00001133 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001134 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001135 ProtocolRefs.size(),
1136 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001139 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001140 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001141
Chris Lattnerb28317a2009-03-28 19:18:32 +00001142 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001143 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001144 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001145 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1146 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001147 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattnerb28317a2009-03-28 19:18:32 +00001149 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001150 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001151 ProtocolRefs.data(),
1152 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001153 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001154 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001155 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001156 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001157}
Steve Naroffdac269b2007-08-20 21:31:48 +00001158
1159/// objc-implementation:
1160/// objc-class-implementation-prologue
1161/// objc-category-implementation-prologue
1162///
1163/// objc-class-implementation-prologue:
1164/// @implementation identifier objc-superclass[opt]
1165/// objc-class-instance-variables[opt]
1166///
1167/// objc-category-implementation-prologue:
1168/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001169Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001170 SourceLocation atLoc) {
1171 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1172 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1173 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001175 // Code completion after '@implementation'.
1176 if (Tok.is(tok::code_completion)) {
1177 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1178 ConsumeToken();
1179 }
1180
Chris Lattnerdf195262007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001182 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001183 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001184 }
1185 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001186 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001187 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001188
1189 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001190 // we have a category implementation.
1191 SourceLocation lparenLoc = ConsumeParen();
1192 SourceLocation categoryLoc, rparenLoc;
1193 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001195 if (Tok.is(tok::code_completion)) {
1196 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1197 ConsumeToken();
1198 }
1199
Chris Lattnerdf195262007-10-09 17:51:17 +00001200 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001201 categoryId = Tok.getIdentifierInfo();
1202 categoryLoc = ConsumeToken();
1203 } else {
1204 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001205 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001206 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001207 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001208 Diag(Tok, diag::err_expected_rparen);
1209 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001210 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001211 }
1212 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001213 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001214 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001215 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001216 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001217 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001218 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001219 }
1220 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001221 SourceLocation superClassLoc;
1222 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001223 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001224 // We have a super class
1225 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001226 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001227 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001228 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001229 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001230 superClassId = Tok.getIdentifierInfo();
1231 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001232 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001233 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001234 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001235 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Steve Naroff60fccee2007-10-29 21:38:07 +00001237 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001238 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001239 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001240 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001241 PendingObjCImpDecl.push_back(ObjCImpDecl);
1242
Chris Lattnerb28317a2009-03-28 19:18:32 +00001243 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001244}
Steve Naroff60fccee2007-10-29 21:38:07 +00001245
Ted Kremenek782f2f52010-01-07 01:20:12 +00001246Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001247 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1248 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001249 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001250 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001251 if (ObjCImpDecl) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001252 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001253 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001254 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001255 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001256 else {
1257 // missing @implementation
1258 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1259 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001260 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001261}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001262
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001263Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001264 if (PendingObjCImpDecl.empty())
1265 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1266 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenek782f2f52010-01-07 01:20:12 +00001267 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001268 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1269}
1270
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001271/// compatibility-alias-decl:
1272/// @compatibility_alias alias-name class-name ';'
1273///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001274Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001275 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1276 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1277 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001278 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001279 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001280 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001281 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001282 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1283 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001284 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001285 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001286 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001287 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001288 IdentifierInfo *classId = Tok.getIdentifierInfo();
1289 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1290 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001291 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001292 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001293 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001294 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1295 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296}
1297
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001298/// property-synthesis:
1299/// @synthesize property-ivar-list ';'
1300///
1301/// property-ivar-list:
1302/// property-ivar
1303/// property-ivar-list ',' property-ivar
1304///
1305/// property-ivar:
1306/// identifier
1307/// identifier '=' identifier
1308///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001309Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001310 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1311 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001312 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Douglas Gregorb328c422009-11-18 19:45:45 +00001314 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001315 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001316 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001317 ConsumeToken();
1318 }
1319
Douglas Gregorb328c422009-11-18 19:45:45 +00001320 if (Tok.isNot(tok::identifier)) {
1321 Diag(Tok, diag::err_synthesized_property_name);
1322 SkipUntil(tok::semi);
1323 return DeclPtrTy();
1324 }
1325
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001326 IdentifierInfo *propertyIvar = 0;
1327 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1328 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001329 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001330 // property '=' ivar-name
1331 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001332
1333 if (Tok.is(tok::code_completion)) {
1334 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1335 ObjCImpDecl);
1336 ConsumeToken();
1337 }
1338
Chris Lattnerdf195262007-10-09 17:51:17 +00001339 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 Diag(Tok, diag::err_expected_ident);
1341 break;
1342 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001343 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001344 ConsumeToken(); // consume ivar-name
1345 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001346 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1347 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001348 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001349 break;
1350 ConsumeToken(); // consume ','
1351 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001352 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001353 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001354 SkipUntil(tok::semi);
1355 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001356 else
1357 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001358 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001359}
1360
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001361/// property-dynamic:
1362/// @dynamic property-list
1363///
1364/// property-list:
1365/// identifier
1366/// property-list ',' identifier
1367///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001368Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001369 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1370 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1371 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001372 while (true) {
1373 if (Tok.is(tok::code_completion)) {
1374 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1375 ConsumeToken();
1376 }
1377
1378 if (Tok.isNot(tok::identifier)) {
1379 Diag(Tok, diag::err_expected_ident);
1380 SkipUntil(tok::semi);
1381 return DeclPtrTy();
1382 }
1383
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001384 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1385 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1386 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1387 propertyId, 0);
1388
Chris Lattnerdf195262007-10-09 17:51:17 +00001389 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001390 break;
1391 ConsumeToken(); // consume ','
1392 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001393 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001394 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001395 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001396}
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001398/// objc-throw-statement:
1399/// throw expression[opt];
1400///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001401Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001402 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001403 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001404 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001405 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001406 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001407 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001408 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001409 }
1410 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001411 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001412 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001413}
1414
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001415/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001416/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001417///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001418Parser::OwningStmtResult
1419Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001420 ConsumeToken(); // consume synchronized
1421 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001422 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001423 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001424 }
1425 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001426 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001427 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001428 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001429 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001430 }
1431 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001432 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001433 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001434 }
1435 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001436 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001437 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001438 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001439 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001440 // Enter a scope to hold everything within the compound stmt. Compound
1441 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001442 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001443
Sebastian Redl61364dd2008-12-11 19:30:53 +00001444 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001445
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001446 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001447 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001448 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001449 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001450}
1451
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001452/// objc-try-catch-statement:
1453/// @try compound-statement objc-catch-list[opt]
1454/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1455///
1456/// objc-catch-list:
1457/// @catch ( parameter-declaration ) compound-statement
1458/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1459/// catch-parameter-declaration:
1460/// parameter-declaration
1461/// '...' [OBJC2]
1462///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001463Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001464 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001465
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001466 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001467 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001468 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001469 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001470 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001471 OwningStmtResult CatchStmts(Actions);
1472 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001473 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001474 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001475 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001476 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001477 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001478
Chris Lattnerdf195262007-10-09 17:51:17 +00001479 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001480 // At this point, we need to lookahead to determine if this @ is the start
1481 // of an @catch or @finally. We don't want to consume the @ token if this
1482 // is an @try or @encode or something else.
1483 Token AfterAt = GetLookAheadToken(1);
1484 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1485 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1486 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001487
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001488 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001489 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001490 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001491 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001492 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001493 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001494 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001495 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001496 DeclSpec DS;
1497 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001498 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001499 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001500 // we must accept either 'declarator' or 'abstract-declarator' here.
1501 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1502 ParseDeclarator(ParmDecl);
1503
1504 // Inform the actions module about the parameter declarator, so it
1505 // gets added to the current scope.
Fariborz Jahaniand219a3a2010-02-03 00:32:51 +00001506 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff7ba138a2009-03-03 19:52:17 +00001507 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian2f764f12010-02-03 00:01:43 +00001508 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroff64515f32008-02-05 21:27:35 +00001509 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001510 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Steve Naroff93a25952009-04-07 22:56:58 +00001512 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Steve Naroff93a25952009-04-07 22:56:58 +00001514 if (Tok.is(tok::r_paren))
1515 RParenLoc = ConsumeParen();
1516 else // Skip over garbage, until we get to ')'. Eat the ')'.
1517 SkipUntil(tok::r_paren, true, false);
1518
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001519 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001520 if (Tok.is(tok::l_brace))
1521 CatchBody = ParseCompoundStatementBody();
1522 else
1523 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001524 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001525 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001526 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001527 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001528 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001529 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001530 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1531 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001532 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001533 }
1534 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001535 } else {
1536 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001537 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001538 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001539
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001540 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001541 if (Tok.is(tok::l_brace))
1542 FinallyBody = ParseCompoundStatementBody();
1543 else
1544 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001545 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001546 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001547 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001548 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001549 catch_or_finally_seen = true;
1550 break;
1551 }
1552 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001553 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001554 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001555 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001556 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001557 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1558 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001559}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001560
Steve Naroff3536b442007-09-06 21:24:23 +00001561/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001562///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001563Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1564 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Chris Lattner49f28ca2009-03-05 08:00:35 +00001566 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1567 PP.getSourceManager(),
1568 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001570 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001571 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001572 if (ObjCImpDecl) {
1573 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001574 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001575 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001576 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001577 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001578
Steve Naroff409be832007-11-11 19:54:21 +00001579 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001580 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001581 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Steve Naroff409be832007-11-11 19:54:21 +00001583 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1584 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Steve Naroff409be832007-11-11 19:54:21 +00001586 // If we didn't find the '{', bail out.
1587 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001588 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001589 }
Steve Naroff409be832007-11-11 19:54:21 +00001590 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Steve Naroff409be832007-11-11 19:54:21 +00001592 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001593 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Steve Naroff409be832007-11-11 19:54:21 +00001595 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001596 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001597 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001598
1599 OwningStmtResult FnBody(ParseCompoundStatementBody());
1600
Steve Naroff409be832007-11-11 19:54:21 +00001601 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001602 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001603 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1604 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001605
Steve Naroff32ce8372009-03-02 22:00:56 +00001606 // TODO: Pass argument information.
1607 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Steve Naroff409be832007-11-11 19:54:21 +00001609 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001610 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001611
Steve Naroff71c0a952007-11-13 23:01:27 +00001612 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001613}
Anders Carlsson55085182007-08-21 17:43:55 +00001614
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001615Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001616 if (Tok.is(tok::code_completion)) {
1617 Actions.CodeCompleteObjCAtStatement(CurScope);
1618 ConsumeToken();
1619 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001620 }
1621
1622 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001623 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001624
1625 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001626 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001627
1628 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001629 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001630
Sebastian Redld8c4e152008-12-11 22:33:27 +00001631 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001632 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001633 // If the expression is invalid, skip ahead to the next semicolon. Not
1634 // doing this opens us up to the possibility of infinite loops if
1635 // ParseExpression does not consume any tokens.
1636 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001637 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001638 }
Chris Lattner5d803162009-12-07 16:33:19 +00001639
Steve Naroff64515f32008-02-05 21:27:35 +00001640 // Otherwise, eat the semicolon.
1641 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001642 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001643}
1644
Sebastian Redl1d922962008-12-13 15:32:12 +00001645Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001646 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001647 case tok::code_completion:
1648 Actions.CodeCompleteObjCAtExpression(CurScope);
1649 ConsumeToken();
1650 return ExprError();
1651
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001652 case tok::string_literal: // primary-expression: string-literal
1653 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001654 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001655 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001656 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001657 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001658
Chris Lattner4fef81d2008-08-05 06:19:09 +00001659 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1660 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001661 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001662 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001663 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001664 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001665 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001666 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001667 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001668 }
Anders Carlsson55085182007-08-21 17:43:55 +00001669 }
Anders Carlsson55085182007-08-21 17:43:55 +00001670}
1671
Mike Stump1eb44332009-09-09 15:08:12 +00001672/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001673/// '[' objc-receiver objc-message-args ']'
1674///
1675/// objc-receiver:
1676/// expression
1677/// class-name
1678/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001679Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001680 assert(Tok.is(tok::l_square) && "'[' expected");
1681 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1682
1683 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001684 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001685 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001686 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1687 SourceLocation NameLoc = ConsumeToken();
1688 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1689 ExprArg(Actions));
1690 }
Chris Lattner699b6612008-01-25 18:59:06 +00001691 }
1692
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001693 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001694 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001695 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001696 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001697 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001698
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001699 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001700 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001701}
Sebastian Redl1d922962008-12-13 15:32:12 +00001702
Chris Lattner699b6612008-01-25 18:59:06 +00001703/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1704/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001705///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001706/// objc-message-args:
1707/// objc-selector
1708/// objc-keywordarg-list
1709///
1710/// objc-keywordarg-list:
1711/// objc-keywordarg
1712/// objc-keywordarg-list objc-keywordarg
1713///
Mike Stump1eb44332009-09-09 15:08:12 +00001714/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001715/// selector-name[opt] ':' objc-keywordexpr
1716///
1717/// objc-keywordexpr:
1718/// nonempty-expr-list
1719///
1720/// nonempty-expr-list:
1721/// assignment-expression
1722/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001723///
1724Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001725Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001726 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001727 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001728 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001729 if (Tok.is(tok::code_completion)) {
1730 if (ReceiverName)
Douglas Gregord3c68542009-11-19 01:08:35 +00001731 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1732 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001733 else
Douglas Gregord3c68542009-11-19 01:08:35 +00001734 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1735 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001736 ConsumeToken();
1737 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001738
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001739 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001740 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001741 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001742
Anders Carlssonff975cf2009-02-14 18:21:46 +00001743 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Steve Naroff68d331a2007-09-27 14:38:14 +00001745 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001746 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001747
Chris Lattnerdf195262007-10-09 17:51:17 +00001748 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001749 while (1) {
1750 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001751 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001752
Chris Lattnerdf195262007-10-09 17:51:17 +00001753 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001754 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001755 // We must manually skip to a ']', otherwise the expression skipper will
1756 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1757 // the enclosing expression.
1758 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001759 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001760 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001761
Steve Naroff68d331a2007-09-27 14:38:14 +00001762 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001763 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001764 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001765 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001766 // We must manually skip to a ']', otherwise the expression skipper will
1767 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1768 // the enclosing expression.
1769 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001770 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001771 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001772
Steve Naroff37387c92007-09-17 20:25:27 +00001773 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001774 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001775
Douglas Gregord3c68542009-11-19 01:08:35 +00001776 // Code completion after each argument.
1777 if (Tok.is(tok::code_completion)) {
1778 if (ReceiverName)
1779 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1780 KeyIdents.data(),
1781 KeyIdents.size());
1782 else
1783 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1784 KeyIdents.data(),
1785 KeyIdents.size());
1786 ConsumeToken();
1787 }
1788
Steve Naroff37387c92007-09-17 20:25:27 +00001789 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001790 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001791 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001792 break;
1793 // We have a selector or a colon, continue parsing.
1794 }
1795 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001796 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001797 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001798 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001799 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001800 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001801 // We must manually skip to a ']', otherwise the expression skipper will
1802 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1803 // the enclosing expression.
1804 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001805 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001806 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001807
Steve Naroff49f109c2007-11-15 13:05:42 +00001808 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001809 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001810 }
1811 } else if (!selIdent) {
1812 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001813
Chris Lattner4fef81d2008-08-05 06:19:09 +00001814 // We must manually skip to a ']', otherwise the expression skipper will
1815 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1816 // the enclosing expression.
1817 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001818 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001819 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001820
Chris Lattnerdf195262007-10-09 17:51:17 +00001821 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001822 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001823 // We must manually skip to a ']', otherwise the expression skipper will
1824 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1825 // the enclosing expression.
1826 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001827 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001828 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001829
Chris Lattner699b6612008-01-25 18:59:06 +00001830 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001831
Steve Naroff29238a02007-10-05 18:42:47 +00001832 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001833 if (nKeys == 0)
1834 KeyIdents.push_back(selIdent);
1835 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001836
Chris Lattnerff384912007-10-07 02:00:24 +00001837 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001838 if (ReceiverName)
1839 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001840 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001841 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001842 KeyExprs.take(), KeyExprs.size()));
1843 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001844 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001845 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001846}
1847
Sebastian Redl1d922962008-12-13 15:32:12 +00001848Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001849 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001850 if (Res.isInvalid()) return move(Res);
1851
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001852 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1853 // expressions. At this point, we know that the only valid thing that starts
1854 // with '@' is an @"".
1855 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001856 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001857 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001858 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001859
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001860 while (Tok.is(tok::at)) {
1861 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001862
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001863 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001864 if (!isTokenStringLiteral())
1865 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001866
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001867 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001868 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001869 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001870
Sebastian Redleffa8d12008-12-10 00:02:53 +00001871 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001872 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001873
1874 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1875 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001876}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001877
1878/// objc-encode-expression:
1879/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001880Parser::OwningExprResult
1881Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001882 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001883
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001884 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001885
Chris Lattner4fef81d2008-08-05 06:19:09 +00001886 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001887 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1888
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001889 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001890
Douglas Gregor809070a2009-02-18 17:45:20 +00001891 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001892
Anders Carlsson4988ae32007-08-23 15:31:37 +00001893 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001894
Douglas Gregor809070a2009-02-18 17:45:20 +00001895 if (Ty.isInvalid())
1896 return ExprError();
1897
Mike Stump1eb44332009-09-09 15:08:12 +00001898 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001899 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001900}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001901
1902/// objc-protocol-expression
1903/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001904Parser::OwningExprResult
1905Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001906 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001907
Chris Lattner4fef81d2008-08-05 06:19:09 +00001908 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001909 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1910
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001911 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001912
Chris Lattner4fef81d2008-08-05 06:19:09 +00001913 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001914 return ExprError(Diag(Tok, diag::err_expected_ident));
1915
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001916 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001917 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001918
Anders Carlsson4988ae32007-08-23 15:31:37 +00001919 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001920
Sebastian Redl1d922962008-12-13 15:32:12 +00001921 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1922 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001923}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001924
1925/// objc-selector-expression
1926/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001927Parser::OwningExprResult
1928Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001929 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001930
Chris Lattner4fef81d2008-08-05 06:19:09 +00001931 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001932 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1933
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001934 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001935 SourceLocation LParenLoc = ConsumeParen();
1936 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001937 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001938 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1939 return ExprError(Diag(Tok, diag::err_expected_ident));
1940
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001941 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001942 unsigned nColons = 0;
1943 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001944 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001945 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001946 return ExprError(Diag(Tok, diag::err_expected_colon));
1947
Chris Lattnercb53b362007-12-27 19:57:00 +00001948 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001949 ConsumeToken(); // Eat the ':'.
1950 if (Tok.is(tok::r_paren))
1951 break;
1952 // Check for another keyword selector.
1953 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001954 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001955 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001956 if (!SelIdent && Tok.isNot(tok::colon))
1957 break;
1958 }
Steve Naroff887407e2007-12-05 22:21:29 +00001959 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001960 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001961 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001962 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1963 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001964 }