blob: a7e08562d02af6d46a7d3f074c7555a0817cf647 [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)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000034 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000035 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000036 }
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)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000133 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000134 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000135 }
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();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000145 if (Tok.is(tok::l_paren) &&
146 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000150 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000151 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000152 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000153 }
154
Steve Naroff527fe232007-08-23 19:56:30 +0000155 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000156 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000157 categoryId = Tok.getIdentifierInfo();
158 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000159 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000160 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000161 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();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000170 // Next, we need to check for any protocol references.
171 SourceLocation LAngleLoc, EndProtoLoc;
172 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
173 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
174 if (Tok.is(tok::less) &&
175 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000176 LAngleLoc, EndProtoLoc))
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000177 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000179 if (attrList) // categories don't support attributes.
180 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000182 DeclPtrTy CategoryType =
183 Actions.ActOnStartCategoryInterface(atLoc,
184 nameId, nameLoc,
185 categoryId, categoryLoc,
186 ProtocolRefs.data(),
187 ProtocolRefs.size(),
188 ProtocolLocs.data(),
189 EndProtoLoc);
190 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000191 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
192 atLoc);
193
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000194 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
195 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000196 }
197 // Parse a class interface.
198 IdentifierInfo *superClassId = 0;
199 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000200
Chris Lattnerdf195262007-10-09 17:51:17 +0000201 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000202 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000203
204 // Code completion of superclass names.
205 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000206 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000207 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000208 }
209
Chris Lattnerdf195262007-10-09 17:51:17 +0000210 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000211 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000212 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000213 }
214 superClassId = Tok.getIdentifierInfo();
215 superClassLoc = ConsumeToken();
216 }
217 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000218 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000219 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
220 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000221 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000222 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
223 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000224 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
226 DeclPtrTy ClsType =
227 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000228 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000229 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000230 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000231 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000234 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000235
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000236 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000237 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000238}
239
John McCalld0014542009-12-03 22:31:13 +0000240/// The Objective-C property callback. This should be defined where
241/// it's used, but instead it's been lifted to here to support VS2005.
242struct Parser::ObjCPropertyCallback : FieldCallback {
243 Parser &P;
244 DeclPtrTy IDecl;
245 llvm::SmallVectorImpl<DeclPtrTy> &Props;
246 ObjCDeclSpec &OCDS;
247 SourceLocation AtLoc;
248 tok::ObjCKeywordKind MethodImplKind;
249
250 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
251 llvm::SmallVectorImpl<DeclPtrTy> &Props,
252 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
253 tok::ObjCKeywordKind MethodImplKind) :
254 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
255 MethodImplKind(MethodImplKind) {
256 }
257
258 DeclPtrTy invoke(FieldDeclarator &FD) {
259 if (FD.D.getIdentifier() == 0) {
260 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
261 << FD.D.getSourceRange();
262 return DeclPtrTy();
263 }
264 if (FD.BitfieldSize) {
265 P.Diag(AtLoc, diag::err_objc_property_bitfield)
266 << FD.D.getSourceRange();
267 return DeclPtrTy();
268 }
269
270 // Install the property declarator into interfaceDecl.
271 IdentifierInfo *SelName =
272 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
273
274 Selector GetterSel =
275 P.PP.getSelectorTable().getNullarySelector(SelName);
276 IdentifierInfo *SetterName = OCDS.getSetterName();
277 Selector SetterSel;
278 if (SetterName)
279 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
280 else
281 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
282 P.PP.getSelectorTable(),
283 FD.D.getIdentifier());
284 bool isOverridingProperty = false;
285 DeclPtrTy Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000286 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000287 GetterSel, SetterSel, IDecl,
288 &isOverridingProperty,
289 MethodImplKind);
290 if (!isOverridingProperty)
291 Props.push_back(Property);
292
293 return Property;
294 }
295};
296
Steve Naroffdac269b2007-08-20 21:31:48 +0000297/// objc-interface-decl-list:
298/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000299/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000300/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000301/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000302/// objc-interface-decl-list declaration
303/// objc-interface-decl-list ';'
304///
Steve Naroff294494e2007-08-22 16:35:03 +0000305/// objc-method-requirement: [OBJC2]
306/// @required
307/// @optional
308///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000310 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000311 llvm::SmallVector<DeclPtrTy, 32> allMethods;
312 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000313 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000314 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek782f2f52010-01-07 01:20:12 +0000316 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000317
Steve Naroff294494e2007-08-22 16:35:03 +0000318 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000319 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000320 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000321 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000322 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000323 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000324 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
325 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000326 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
327 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000328 continue;
329 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000330 if (Tok.is(tok::l_paren)) {
331 Diag(Tok, diag::err_expected_minus_or_plus);
332 DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(),
333 tok::minus,
334 interfaceDecl,
335 MethodImplKind);
336 continue;
337 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000338 // Ignore excess semicolons.
339 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000340 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000341 continue;
342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattnerbc662af2008-10-20 06:10:06 +0000344 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000345 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000346 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000348 // Code completion within an Objective-C interface.
349 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000350 Actions.CodeCompleteOrdinaryName(getCurScope(),
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000351 ObjCImpDecl? Action::CCC_ObjCImplementation
352 : Action::CCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000353 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000354 }
355
Chris Lattnere82a10f2008-10-20 05:46:22 +0000356 // If we don't have an @ directive, parse it as a function definition.
357 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000358 // The code below does not consume '}'s because it is afraid of eating the
359 // end of a namespace. Because of the way this code is structured, an
360 // erroneous r_brace would cause an infinite loop if not handled here.
361 if (Tok.is(tok::r_brace))
362 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Steve Naroff4985ace2007-08-22 18:35:33 +0000364 // FIXME: as the name implies, this rule allows function definitions.
365 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000366 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000367 continue;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattnere82a10f2008-10-20 05:46:22 +0000370 // Otherwise, we have an @ directive, eat the @.
371 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000372 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000373 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000374 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000375 break;
376 }
377
Chris Lattnera2449b22008-10-20 05:57:40 +0000378 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnera2449b22008-10-20 05:57:40 +0000380 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000381 AtEnd.setBegin(AtLoc);
382 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000383 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000384 } else if (DirectiveKind == tok::objc_not_keyword) {
385 Diag(Tok, diag::err_objc_unknown_at);
386 SkipUntil(tok::semi);
387 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnerbc662af2008-10-20 06:10:06 +0000390 // Eat the identifier.
391 ConsumeToken();
392
Chris Lattnera2449b22008-10-20 05:57:40 +0000393 switch (DirectiveKind) {
394 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000395 // FIXME: If someone forgets an @end on a protocol, this loop will
396 // continue to eat up tons of stuff and spew lots of nonsense errors. It
397 // would probably be better to bail out if we saw an @class or @interface
398 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000399 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000400 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000401 SkipUntil(tok::r_brace, tok::at);
402 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattnera2449b22008-10-20 05:57:40 +0000404 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000405 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000406 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000407 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000408 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000410 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000411 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000412 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattnera2449b22008-10-20 05:57:40 +0000414 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000415 if (!getLang().ObjC2)
416 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
417
Chris Lattnere82a10f2008-10-20 05:46:22 +0000418 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000419 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000420 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000421 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
422 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000423
John McCalld0014542009-12-03 22:31:13 +0000424 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
425 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000426
Chris Lattnere82a10f2008-10-20 05:46:22 +0000427 // Parse all the comma separated declarators.
428 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000429 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000431 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
432 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000433 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000434 }
Steve Naroff294494e2007-08-22 16:35:03 +0000435 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000436
437 // We break out of the big loop in two cases: when we see @end or when we see
438 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000439 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000440 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000441 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000442 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000443 ConsumeToken(); // the "end" identifier
444 else
445 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattnera2449b22008-10-20 05:57:40 +0000447 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000448 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000449 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000450 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000451 allProperties.data(), allProperties.size(),
452 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000453}
454
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000455/// Parse property attribute declarations.
456///
457/// property-attr-decl: '(' property-attrlist ')'
458/// property-attrlist:
459/// property-attribute
460/// property-attrlist ',' property-attribute
461/// property-attribute:
462/// getter '=' identifier
463/// setter '=' identifier ':'
464/// readonly
465/// readwrite
466/// assign
467/// retain
468/// copy
469/// nonatomic
470///
Douglas Gregor4ad96852009-11-19 07:41:15 +0000471void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
472 DeclPtrTy *Methods,
473 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000474 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000475 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000477 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000478 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000479 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000480 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000481 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000482 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000484 // If this is not an identifier at all, bail out early.
485 if (II == 0) {
486 MatchRHSPunctuation(tok::r_paren, LHSLoc);
487 return;
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner156b0612008-10-20 07:37:22 +0000490 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattner92e62b02008-11-20 04:42:34 +0000492 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000493 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000494 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000495 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000496 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000497 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000498 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000499 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000500 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000502 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000506 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
507 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000508 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregor4ad96852009-11-19 07:41:15 +0000510 if (Tok.is(tok::code_completion)) {
511 if (II->getNameStart()[0] == 's')
Douglas Gregor23c94db2010-07-02 17:43:08 +0000512 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000513 Methods, NumMethods);
514 else
Douglas Gregor23c94db2010-07-02 17:43:08 +0000515 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000516 Methods, NumMethods);
Douglas Gregordc845342010-05-25 05:58:43 +0000517 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000518 }
519
Chris Lattner8ca329c2008-10-20 07:24:39 +0000520 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000521 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000522 SkipUntil(tok::r_paren);
523 return;
524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Daniel Dunbare013d682009-10-18 20:26:12 +0000526 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000527 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
528 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000529 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000531 if (ExpectAndConsume(tok::colon,
532 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000533 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000534 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000535 } else {
536 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
537 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000538 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000539 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000540 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000541 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000542 SkipUntil(tok::r_paren);
543 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner156b0612008-10-20 07:37:22 +0000546 if (Tok.isNot(tok::comma))
547 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Chris Lattner156b0612008-10-20 07:37:22 +0000549 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner156b0612008-10-20 07:37:22 +0000552 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000553}
554
Steve Naroff3536b442007-09-06 21:24:23 +0000555/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000556/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000557/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000558///
559/// objc-instance-method: '-'
560/// objc-class-method: '+'
561///
Steve Naroff4985ace2007-08-22 18:35:33 +0000562/// objc-method-attributes: [OBJC2]
563/// __attribute__((deprecated))
564///
Mike Stump1eb44332009-09-09 15:08:12 +0000565Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000566 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000567 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000568
Mike Stump1eb44332009-09-09 15:08:12 +0000569 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000570 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattnerb28317a2009-03-28 19:18:32 +0000572 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000573 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000574 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000575 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000576}
577
578/// objc-selector:
579/// identifier
580/// one of
581/// enum struct union if else while do for switch case default
582/// break continue return goto asm sizeof typeof __alignof
583/// unsigned long const short volatile signed restrict _Complex
584/// in out inout bycopy byref oneway int char float double void _Bool
585///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000586IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000587 switch (Tok.getKind()) {
588 default:
589 return 0;
590 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000591 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000592 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000593 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000594 case tok::kw_break:
595 case tok::kw_case:
596 case tok::kw_catch:
597 case tok::kw_char:
598 case tok::kw_class:
599 case tok::kw_const:
600 case tok::kw_const_cast:
601 case tok::kw_continue:
602 case tok::kw_default:
603 case tok::kw_delete:
604 case tok::kw_do:
605 case tok::kw_double:
606 case tok::kw_dynamic_cast:
607 case tok::kw_else:
608 case tok::kw_enum:
609 case tok::kw_explicit:
610 case tok::kw_export:
611 case tok::kw_extern:
612 case tok::kw_false:
613 case tok::kw_float:
614 case tok::kw_for:
615 case tok::kw_friend:
616 case tok::kw_goto:
617 case tok::kw_if:
618 case tok::kw_inline:
619 case tok::kw_int:
620 case tok::kw_long:
621 case tok::kw_mutable:
622 case tok::kw_namespace:
623 case tok::kw_new:
624 case tok::kw_operator:
625 case tok::kw_private:
626 case tok::kw_protected:
627 case tok::kw_public:
628 case tok::kw_register:
629 case tok::kw_reinterpret_cast:
630 case tok::kw_restrict:
631 case tok::kw_return:
632 case tok::kw_short:
633 case tok::kw_signed:
634 case tok::kw_sizeof:
635 case tok::kw_static:
636 case tok::kw_static_cast:
637 case tok::kw_struct:
638 case tok::kw_switch:
639 case tok::kw_template:
640 case tok::kw_this:
641 case tok::kw_throw:
642 case tok::kw_true:
643 case tok::kw_try:
644 case tok::kw_typedef:
645 case tok::kw_typeid:
646 case tok::kw_typename:
647 case tok::kw_typeof:
648 case tok::kw_union:
649 case tok::kw_unsigned:
650 case tok::kw_using:
651 case tok::kw_virtual:
652 case tok::kw_void:
653 case tok::kw_volatile:
654 case tok::kw_wchar_t:
655 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000656 case tok::kw__Bool:
657 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000658 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000659 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000660 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000661 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000662 }
Steve Naroff294494e2007-08-22 16:35:03 +0000663}
664
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000665/// objc-for-collection-in: 'in'
666///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000667bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000668 // FIXME: May have to do additional look-ahead to only allow for
669 // valid tokens following an 'in'; such as an identifier, unary operators,
670 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000671 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000672 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000673}
674
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000675/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000676/// qualifier list and builds their bitmask representation in the input
677/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000678///
679/// objc-type-qualifiers:
680/// objc-type-qualifier
681/// objc-type-qualifiers objc-type-qualifier
682///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000684 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000685 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000686 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Chris Lattnere8b724d2007-12-12 06:56:32 +0000688 const IdentifierInfo *II = Tok.getIdentifierInfo();
689 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000691 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000694 switch (i) {
695 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
697 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
698 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
699 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
700 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
701 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000702 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000703 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000704 ConsumeToken();
705 II = 0;
706 break;
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Chris Lattnere8b724d2007-12-12 06:56:32 +0000709 // If this wasn't a recognized qualifier, bail out.
710 if (II) return;
711 }
712}
713
714/// objc-type-name:
715/// '(' objc-type-qualifiers[opt] type-name ')'
716/// '(' objc-type-qualifiers[opt] ')'
717///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000719 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Chris Lattner4a76b292008-10-22 03:52:06 +0000721 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000722 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000724 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000726
Chris Lattner4a76b292008-10-22 03:52:06 +0000727 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000728 if (isTypeSpecifierQualifier()) {
729 TypeResult TypeSpec = ParseTypeName();
730 if (!TypeSpec.isInvalid())
731 Ty = TypeSpec.get();
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Steve Naroffd7333c22008-10-21 14:15:04 +0000734 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000735 ConsumeParen();
736 else if (Tok.getLocation() == TypeStartLoc) {
737 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000738 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000739 SkipUntil(tok::r_paren);
740 } else {
741 // Otherwise, we found *something*, but didn't get a ')' in the right
742 // place. Emit an error then return what we have as the type.
743 MatchRHSPunctuation(tok::r_paren, LParenLoc);
744 }
Steve Narofff28b2642007-09-05 23:30:30 +0000745 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000746}
747
748/// objc-method-decl:
749/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000750/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000751/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000752/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000753///
754/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000755/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000756/// objc-keyword-selector objc-keyword-decl
757///
758/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000759/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
760/// objc-selector ':' objc-keyword-attributes[opt] identifier
761/// ':' objc-type-name objc-keyword-attributes[opt] identifier
762/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000763///
Steve Naroff4985ace2007-08-22 18:35:33 +0000764/// objc-parmlist:
765/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000766///
Steve Naroff4985ace2007-08-22 18:35:33 +0000767/// objc-parms:
768/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000769///
Steve Naroff4985ace2007-08-22 18:35:33 +0000770/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000771/// , ...
772///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000773/// objc-keyword-attributes: [OBJC2]
774/// __attribute__((unused))
775///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000776Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
777 tok::TokenKind mType,
778 DeclPtrTy IDecl,
779 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000780 ParsingDeclRAIIObject PD(*this);
781
Douglas Gregore8f5a172010-04-07 00:21:17 +0000782 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000783 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000784 /*ReturnType=*/0, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000785 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000786 }
787
Chris Lattnere8904e92008-08-23 01:48:03 +0000788 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000789 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000791 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000792 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek9e049352010-02-18 23:05:16 +0000794 // If attributes exist before the method, parse them.
795 llvm::OwningPtr<AttributeList> MethodAttrs;
796 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
797 MethodAttrs.reset(ParseGNUAttributes());
798
Douglas Gregore8f5a172010-04-07 00:21:17 +0000799 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000800 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000801 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000802 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000803 }
804
Ted Kremenek9e049352010-02-18 23:05:16 +0000805 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000806 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000807 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000808
Steve Naroff84c43102009-02-11 20:43:13 +0000809 // An unnamed colon is valid.
810 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000811 Diag(Tok, diag::err_expected_selector_for_method)
812 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000813 // Skip until we get a ; or {}.
814 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000815 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000818 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000819 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000820 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000821 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000822 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
823 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Chris Lattnerff384912007-10-07 02:00:24 +0000825 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000826 DeclPtrTy Result
827 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000828 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000829 0,
830 CParamInfo.data(), CParamInfo.size(),
831 MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000832 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000833 PD.complete(Result);
834 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000835 }
Steve Narofff28b2642007-09-05 23:30:30 +0000836
Steve Naroff68d331a2007-09-27 14:38:14 +0000837 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000838 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattnerff384912007-10-07 02:00:24 +0000840 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000841 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattnerff384912007-10-07 02:00:24 +0000843 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000844 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000845 Diag(Tok, diag::err_expected_colon);
846 break;
847 }
848 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattnere294d3f2009-04-11 18:57:04 +0000850 ArgInfo.Type = 0;
851 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
852 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
853
Chris Lattnerff384912007-10-07 02:00:24 +0000854 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000855 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000856 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000857 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000858
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000859 // Code completion for the next piece of the selector.
860 if (Tok.is(tok::code_completion)) {
861 ConsumeCodeCompletionToken();
862 KeyIdents.push_back(SelIdent);
863 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
864 mType == tok::minus,
865 /*AtParameterName=*/true,
866 ReturnType,
867 KeyIdents.data(),
868 KeyIdents.size());
869 KeyIdents.pop_back();
870 break;
871 }
872
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000874 Diag(Tok, diag::err_expected_ident); // missing argument name.
875 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattnere294d3f2009-04-11 18:57:04 +0000878 ArgInfo.Name = Tok.getIdentifierInfo();
879 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000880 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattnere294d3f2009-04-11 18:57:04 +0000882 ArgInfos.push_back(ArgInfo);
883 KeyIdents.push_back(SelIdent);
884
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000885 // Code completion for the next piece of the selector.
886 if (Tok.is(tok::code_completion)) {
887 ConsumeCodeCompletionToken();
888 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
889 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000890 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000891 ReturnType,
892 KeyIdents.data(),
893 KeyIdents.size());
894 break;
895 }
896
Chris Lattnerff384912007-10-07 02:00:24 +0000897 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000898 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000899 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000900 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000901 break;
902 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Steve Naroff335eafa2007-11-15 12:35:21 +0000905 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattnerff384912007-10-07 02:00:24 +0000907 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000908 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000909 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000910 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000911 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000912 ConsumeToken();
913 break;
914 }
Chris Lattnerff384912007-10-07 02:00:24 +0000915 DeclSpec DS;
916 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000917 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000918 Declarator ParmDecl(DS, Declarator::PrototypeContext);
919 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000920 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Douglas Gregor23c94db2010-07-02 17:43:08 +0000921 DeclPtrTy Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000922 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
923 ParmDecl.getIdentifierLoc(),
924 Param,
925 0));
926
Chris Lattnerff384912007-10-07 02:00:24 +0000927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Chris Lattnerff384912007-10-07 02:00:24 +0000929 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000930 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000931 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000932 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
933 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000935 if (KeyIdents.size() == 0)
936 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000937 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
938 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000939 DeclPtrTy Result
940 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000941 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000942 &ArgInfos[0],
943 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +0000944 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000945 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000946 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000947
948 // Delete referenced AttributeList objects.
949 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
950 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
951 delete I->ArgAttrs;
952
John McCall54abf7d2009-11-04 02:18:39 +0000953 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000954}
955
Steve Naroffdac269b2007-08-20 21:31:48 +0000956/// objc-protocol-refs:
957/// '<' identifier-list '>'
958///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000959bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000960ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000961 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
962 bool WarnOnDeclarations,
963 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000964 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000966 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattnere13b9592008-07-26 04:03:38 +0000968 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattnere13b9592008-07-26 04:03:38 +0000970 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000971 if (Tok.is(tok::code_completion)) {
972 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
973 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +0000974 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +0000975 }
976
Chris Lattnere13b9592008-07-26 04:03:38 +0000977 if (Tok.isNot(tok::identifier)) {
978 Diag(Tok, diag::err_expected_ident);
979 SkipUntil(tok::greater);
980 return true;
981 }
982 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
983 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000984 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000985 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattnere13b9592008-07-26 04:03:38 +0000987 if (Tok.isNot(tok::comma))
988 break;
989 ConsumeToken();
990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattnere13b9592008-07-26 04:03:38 +0000992 // Consume the '>'.
993 if (Tok.isNot(tok::greater)) {
994 Diag(Tok, diag::err_expected_greater);
995 return true;
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattnere13b9592008-07-26 04:03:38 +0000998 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattnere13b9592008-07-26 04:03:38 +00001000 // Convert the list of protocols identifiers into a list of protocol decls.
1001 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1002 &ProtocolIdents[0], ProtocolIdents.size(),
1003 Protocols);
1004 return false;
1005}
1006
Steve Naroffdac269b2007-08-20 21:31:48 +00001007/// objc-class-instance-variables:
1008/// '{' objc-instance-variable-decl-list[opt] '}'
1009///
1010/// objc-instance-variable-decl-list:
1011/// objc-visibility-spec
1012/// objc-instance-variable-decl ';'
1013/// ';'
1014/// objc-instance-variable-decl-list objc-visibility-spec
1015/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1016/// objc-instance-variable-decl-list ';'
1017///
1018/// objc-visibility-spec:
1019/// @private
1020/// @protected
1021/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001022/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001023///
1024/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001025/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001026///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001027void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001028 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001029 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001030 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001031 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001032
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001033 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001034
Steve Naroffddbff782007-08-21 21:17:12 +00001035 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Steve Naroffddbff782007-08-21 21:17:12 +00001037 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001038 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001039 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Steve Naroffddbff782007-08-21 21:17:12 +00001041 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001042 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001043 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001044 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001045 ConsumeToken();
1046 continue;
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Steve Naroffddbff782007-08-21 21:17:12 +00001049 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001050 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001051 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001052
1053 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001054 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001055 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001056 }
1057
Steve Naroff861cf3e2007-08-23 18:16:40 +00001058 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001059 case tok::objc_private:
1060 case tok::objc_public:
1061 case tok::objc_protected:
1062 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001063 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001064 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001065 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001066 default:
1067 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001068 continue;
1069 }
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001072 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001073 Actions.CodeCompleteOrdinaryName(getCurScope(),
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001074 Action::CCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001075 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001076 }
1077
John McCallbdd563e2009-11-03 02:38:08 +00001078 struct ObjCIvarCallback : FieldCallback {
1079 Parser &P;
1080 DeclPtrTy IDecl;
1081 tok::ObjCKeywordKind visibility;
1082 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1083
1084 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1085 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1086 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1087 }
1088
1089 DeclPtrTy invoke(FieldDeclarator &FD) {
1090 // Install the declarator into the interface decl.
1091 DeclPtrTy Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001092 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001093 FD.D.getDeclSpec().getSourceRange().getBegin(),
1094 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001095 if (Field)
1096 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001097 return Field;
1098 }
1099 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1100
Chris Lattnere1359422008-04-10 06:46:29 +00001101 // Parse all the comma separated declarators.
1102 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001103 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattnerdf195262007-10-09 17:51:17 +00001105 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001106 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001107 } else {
1108 Diag(Tok, diag::err_expected_semi_decl_list);
1109 // Skip to end of block or statement
1110 SkipUntil(tok::r_brace, true, true);
1111 }
1112 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001113 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +00001114 // Call ActOnFields() even if we don't have any decls. This is useful
1115 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001116 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001117 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001118 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001119 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001120}
Steve Naroffdac269b2007-08-20 21:31:48 +00001121
1122/// objc-protocol-declaration:
1123/// objc-protocol-definition
1124/// objc-protocol-forward-reference
1125///
1126/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001127/// @protocol identifier
1128/// objc-protocol-refs[opt]
1129/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001130/// @end
1131///
1132/// objc-protocol-forward-reference:
1133/// @protocol identifier-list ';'
1134///
1135/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001136/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001137/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001138Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1139 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001140 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001141 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1142 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregor083128f2009-11-18 04:49:41 +00001144 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001145 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001146 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001147 }
1148
Chris Lattnerdf195262007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001150 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001151 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001152 }
1153 // Save the protocol name, then consume it.
1154 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1155 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattnerdf195262007-10-09 17:51:17 +00001157 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001158 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001159 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001160 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001161 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Chris Lattnerdf195262007-10-09 17:51:17 +00001164 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001165 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1166 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1167
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001168 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001169 while (1) {
1170 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001171 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001172 Diag(Tok, diag::err_expected_ident);
1173 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001174 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001175 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001176 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1177 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001178 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattnerdf195262007-10-09 17:51:17 +00001180 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001181 break;
1182 }
1183 // Consume the ';'.
1184 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001185 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Steve Naroffe440eb82007-10-10 17:32:04 +00001187 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001188 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001189 ProtocolRefs.size(),
1190 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001193 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001194 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001195
Chris Lattnerb28317a2009-03-28 19:18:32 +00001196 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001197 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001198 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001199 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1200 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001201 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattnerb28317a2009-03-28 19:18:32 +00001203 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001204 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001205 ProtocolRefs.data(),
1206 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001207 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001208 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001209 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001210 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001211}
Steve Naroffdac269b2007-08-20 21:31:48 +00001212
1213/// objc-implementation:
1214/// objc-class-implementation-prologue
1215/// objc-category-implementation-prologue
1216///
1217/// objc-class-implementation-prologue:
1218/// @implementation identifier objc-superclass[opt]
1219/// objc-class-instance-variables[opt]
1220///
1221/// objc-category-implementation-prologue:
1222/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001223Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001224 SourceLocation atLoc) {
1225 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1226 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1227 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001229 // Code completion after '@implementation'.
1230 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001231 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001232 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001233 }
1234
Chris Lattnerdf195262007-10-09 17:51:17 +00001235 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001236 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001237 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001238 }
1239 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001240 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001241 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001242
1243 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244 // we have a category implementation.
1245 SourceLocation lparenLoc = ConsumeParen();
1246 SourceLocation categoryLoc, rparenLoc;
1247 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001249 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001250 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001251 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001252 }
1253
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001255 categoryId = Tok.getIdentifierInfo();
1256 categoryLoc = ConsumeToken();
1257 } else {
1258 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001259 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001260 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001261 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001262 Diag(Tok, diag::err_expected_rparen);
1263 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001264 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001265 }
1266 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001267 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001268 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001269 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001270 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001271 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001272 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001273 }
1274 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001275 SourceLocation superClassLoc;
1276 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001277 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001278 // We have a super class
1279 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001280 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001281 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001282 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001283 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001284 superClassId = Tok.getIdentifierInfo();
1285 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001286 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001287 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001288 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001289 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Steve Naroff60fccee2007-10-29 21:38:07 +00001291 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001292 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001293 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001294 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001295 PendingObjCImpDecl.push_back(ObjCImpDecl);
1296
Chris Lattnerb28317a2009-03-28 19:18:32 +00001297 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001298}
Steve Naroff60fccee2007-10-29 21:38:07 +00001299
Ted Kremenek782f2f52010-01-07 01:20:12 +00001300Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001301 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1302 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001303 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001304 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001305 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001306 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001307 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001308 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001309 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001310 else {
1311 // missing @implementation
1312 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1313 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001314 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001315}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001316
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001317Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1318 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001319 if (PendingObjCImpDecl.empty())
1320 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1321 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001322 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001323 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1324}
1325
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001326/// compatibility-alias-decl:
1327/// @compatibility_alias alias-name class-name ';'
1328///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001329Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001330 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1331 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1332 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001333 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001334 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001335 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001336 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001337 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1338 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001339 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001340 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001341 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001342 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001343 IdentifierInfo *classId = Tok.getIdentifierInfo();
1344 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1345 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001346 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001347 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001348 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001349 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1350 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001351}
1352
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001353/// property-synthesis:
1354/// @synthesize property-ivar-list ';'
1355///
1356/// property-ivar-list:
1357/// property-ivar
1358/// property-ivar-list ',' property-ivar
1359///
1360/// property-ivar:
1361/// identifier
1362/// identifier '=' identifier
1363///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001364Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001365 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1366 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001367 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Douglas Gregorb328c422009-11-18 19:45:45 +00001369 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001370 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001371 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001372 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001373 }
1374
Douglas Gregorb328c422009-11-18 19:45:45 +00001375 if (Tok.isNot(tok::identifier)) {
1376 Diag(Tok, diag::err_synthesized_property_name);
1377 SkipUntil(tok::semi);
1378 return DeclPtrTy();
1379 }
1380
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001381 IdentifierInfo *propertyIvar = 0;
1382 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1383 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001384 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001385 // property '=' ivar-name
1386 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001387
1388 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001389 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001390 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001391 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001392 }
1393
Chris Lattnerdf195262007-10-09 17:51:17 +00001394 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001395 Diag(Tok, diag::err_expected_ident);
1396 break;
1397 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001398 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001399 ConsumeToken(); // consume ivar-name
1400 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001401 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001402 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001403 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001404 break;
1405 ConsumeToken(); // consume ','
1406 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001407 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001408 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001409 SkipUntil(tok::semi);
1410 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001411 else
1412 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001413 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001414}
1415
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416/// property-dynamic:
1417/// @dynamic property-list
1418///
1419/// property-list:
1420/// identifier
1421/// property-list ',' identifier
1422///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001423Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001424 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1425 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1426 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001427 while (true) {
1428 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001429 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001430 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001431 }
1432
1433 if (Tok.isNot(tok::identifier)) {
1434 Diag(Tok, diag::err_expected_ident);
1435 SkipUntil(tok::semi);
1436 return DeclPtrTy();
1437 }
1438
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001439 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1440 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001441 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001442 propertyId, 0);
1443
Chris Lattnerdf195262007-10-09 17:51:17 +00001444 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001445 break;
1446 ConsumeToken(); // consume ','
1447 }
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001448 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001449 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001450 SkipUntil(tok::semi);
1451 }
1452 else
1453 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001454 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001455}
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001457/// objc-throw-statement:
1458/// throw expression[opt];
1459///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001460Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001461 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001462 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001463 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001464 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001465 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001466 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001467 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001468 }
1469 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001470 // consume ';'
1471 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
Douglas Gregor23c94db2010-07-02 17:43:08 +00001472 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001473}
1474
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001475/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001476/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001477///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001478Parser::OwningStmtResult
1479Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001480 ConsumeToken(); // consume synchronized
1481 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001482 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001483 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001484 }
1485 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001486 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001487 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001488 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001489 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001490 }
1491 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001492 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001493 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001494 }
1495 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001496 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001497 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001498 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001499 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001500 // Enter a scope to hold everything within the compound stmt. Compound
1501 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001502 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001503
Sebastian Redl61364dd2008-12-11 19:30:53 +00001504 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001505
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001506 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001507 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001508 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001509 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001510}
1511
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001512/// objc-try-catch-statement:
1513/// @try compound-statement objc-catch-list[opt]
1514/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1515///
1516/// objc-catch-list:
1517/// @catch ( parameter-declaration ) compound-statement
1518/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1519/// catch-parameter-declaration:
1520/// parameter-declaration
1521/// '...' [OBJC2]
1522///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001523Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001524 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001525
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001526 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001527 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001528 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001529 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001530 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001531 StmtVector CatchStmts(Actions);
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001532 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001533 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001534 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001535 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001536 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001537 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001538
Chris Lattnerdf195262007-10-09 17:51:17 +00001539 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001540 // At this point, we need to lookahead to determine if this @ is the start
1541 // of an @catch or @finally. We don't want to consume the @ token if this
1542 // is an @try or @encode or something else.
1543 Token AfterAt = GetLookAheadToken(1);
1544 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1545 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1546 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001547
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001548 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001549 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001550 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001551 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001552 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001553 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001554 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001555 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001556 DeclSpec DS;
1557 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001558 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001559 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001560 // we must accept either 'declarator' or 'abstract-declarator' here.
1561 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1562 ParseDeclarator(ParmDecl);
1563
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001564 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001565 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001566 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001567 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001568 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Steve Naroff93a25952009-04-07 22:56:58 +00001570 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Steve Naroff93a25952009-04-07 22:56:58 +00001572 if (Tok.is(tok::r_paren))
1573 RParenLoc = ConsumeParen();
1574 else // Skip over garbage, until we get to ')'. Eat the ')'.
1575 SkipUntil(tok::r_paren, true, false);
1576
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001577 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001578 if (Tok.is(tok::l_brace))
1579 CatchBody = ParseCompoundStatementBody();
1580 else
1581 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001582 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001583 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001584
1585 OwningStmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
1586 RParenLoc,
1587 FirstPart,
1588 move(CatchBody));
1589 if (!Catch.isInvalid())
1590 CatchStmts.push_back(Catch.release());
1591
Steve Naroff64515f32008-02-05 21:27:35 +00001592 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001593 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1594 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001595 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001596 }
1597 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001598 } else {
1599 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001600 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001601 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001602
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001603 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001604 if (Tok.is(tok::l_brace))
1605 FinallyBody = ParseCompoundStatementBody();
1606 else
1607 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001608 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001609 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001610 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001611 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001612 catch_or_finally_seen = true;
1613 break;
1614 }
1615 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001616 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001617 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001618 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001619 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001620
1621 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody),
1622 move_arg(CatchStmts),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001623 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001624}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001625
Steve Naroff3536b442007-09-06 21:24:23 +00001626/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001627///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001628Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1629 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Chris Lattner49f28ca2009-03-05 08:00:35 +00001631 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1632 PP.getSourceManager(),
1633 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001635 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001636 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001637 if (ObjCImpDecl) {
1638 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001639 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001640 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001641 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001642 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001643
Steve Naroff409be832007-11-11 19:54:21 +00001644 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001645 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001646 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Steve Naroff409be832007-11-11 19:54:21 +00001648 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1649 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Steve Naroff409be832007-11-11 19:54:21 +00001651 // If we didn't find the '{', bail out.
1652 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001653 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001654 }
Steve Naroff409be832007-11-11 19:54:21 +00001655 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Steve Naroff409be832007-11-11 19:54:21 +00001657 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001658 ParseScope BodyScope(this,
1659 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Steve Naroff409be832007-11-11 19:54:21 +00001661 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001662 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001663 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001664
1665 OwningStmtResult FnBody(ParseCompoundStatementBody());
1666
Steve Naroff409be832007-11-11 19:54:21 +00001667 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001668 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001669 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1670 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001671
Steve Naroff32ce8372009-03-02 22:00:56 +00001672 // TODO: Pass argument information.
1673 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Steve Naroff409be832007-11-11 19:54:21 +00001675 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001676 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001677
Steve Naroff71c0a952007-11-13 23:01:27 +00001678 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001679}
Anders Carlsson55085182007-08-21 17:43:55 +00001680
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001681Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001682 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001683 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001684 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001685 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001686 }
1687
1688 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001689 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001690
1691 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001692 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001693
1694 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001695 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001696
Sebastian Redld8c4e152008-12-11 22:33:27 +00001697 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001698 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001699 // If the expression is invalid, skip ahead to the next semicolon. Not
1700 // doing this opens us up to the possibility of infinite loops if
1701 // ParseExpression does not consume any tokens.
1702 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001703 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001704 }
Chris Lattner5d803162009-12-07 16:33:19 +00001705
Steve Naroff64515f32008-02-05 21:27:35 +00001706 // Otherwise, eat the semicolon.
1707 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001708 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001709}
1710
Sebastian Redl1d922962008-12-13 15:32:12 +00001711Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001712 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001713 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001714 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001715 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001716 return ExprError();
1717
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001718 case tok::string_literal: // primary-expression: string-literal
1719 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001720 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001721 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001722 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001723 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001724
Chris Lattner4fef81d2008-08-05 06:19:09 +00001725 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1726 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001727 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001728 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001729 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001730 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001731 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001732 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001733 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001734 }
Anders Carlsson55085182007-08-21 17:43:55 +00001735 }
Anders Carlsson55085182007-08-21 17:43:55 +00001736}
1737
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001738/// \brirg Parse the receiver of an Objective-C++ message send.
1739///
1740/// This routine parses the receiver of a message send in
1741/// Objective-C++ either as a type or as an expression. Note that this
1742/// routine must not be called to parse a send to 'super', since it
1743/// has no way to return such a result.
1744///
1745/// \param IsExpr Whether the receiver was parsed as an expression.
1746///
1747/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1748/// IsExpr is true), the parsed expression. If the receiver was parsed
1749/// as a type (\c IsExpr is false), the parsed type.
1750///
1751/// \returns True if an error occurred during parsing or semantic
1752/// analysis, in which case the arguments do not have valid
1753/// values. Otherwise, returns false for a successful parse.
1754///
1755/// objc-receiver: [C++]
1756/// 'super' [not parsed here]
1757/// expression
1758/// simple-type-specifier
1759/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001760bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
1761 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1762 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1763 TryAnnotateTypeOrScopeToken();
1764
1765 if (!isCXXSimpleTypeSpecifier()) {
1766 // objc-receiver:
1767 // expression
1768 OwningExprResult Receiver = ParseExpression();
1769 if (Receiver.isInvalid())
1770 return true;
1771
1772 IsExpr = true;
1773 TypeOrExpr = Receiver.take();
1774 return false;
1775 }
1776
1777 // objc-receiver:
1778 // typename-specifier
1779 // simple-type-specifier
1780 // expression (that starts with one of the above)
1781 DeclSpec DS;
1782 ParseCXXSimpleTypeSpecifier(DS);
1783
1784 if (Tok.is(tok::l_paren)) {
1785 // If we see an opening parentheses at this point, we are
1786 // actually parsing an expression that starts with a
1787 // function-style cast, e.g.,
1788 //
1789 // postfix-expression:
1790 // simple-type-specifier ( expression-list [opt] )
1791 // typename-specifier ( expression-list [opt] )
1792 //
1793 // Parse the remainder of this case, then the (optional)
1794 // postfix-expression suffix, followed by the (optional)
1795 // right-hand side of the binary expression. We have an
1796 // instance method.
1797 OwningExprResult Receiver = ParseCXXTypeConstructExpression(DS);
1798 if (!Receiver.isInvalid())
1799 Receiver = ParsePostfixExpressionSuffix(move(Receiver));
1800 if (!Receiver.isInvalid())
1801 Receiver = ParseRHSOfBinaryExpression(move(Receiver), prec::Comma);
1802 if (Receiver.isInvalid())
1803 return true;
1804
1805 IsExpr = true;
1806 TypeOrExpr = Receiver.take();
1807 return false;
1808 }
1809
1810 // We have a class message. Turn the simple-type-specifier or
1811 // typename-specifier we parsed into a type and parse the
1812 // remainder of the class message.
1813 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001814 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001815 if (Type.isInvalid())
1816 return true;
1817
1818 IsExpr = false;
1819 TypeOrExpr = Type.get();
1820 return false;
1821}
1822
Douglas Gregor1b730e82010-05-31 14:40:22 +00001823/// \brief Determine whether the parser is currently referring to a an
1824/// Objective-C message send, using a simplified heuristic to avoid overhead.
1825///
1826/// This routine will only return true for a subset of valid message-send
1827/// expressions.
1828bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001829 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001830 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001831 return GetLookAheadToken(1).is(tok::identifier) &&
1832 GetLookAheadToken(2).is(tok::identifier);
1833}
1834
Mike Stump1eb44332009-09-09 15:08:12 +00001835/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001836/// '[' objc-receiver objc-message-args ']'
1837///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001838/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001839/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001840/// expression
1841/// class-name
1842/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001843///
Sebastian Redl1d922962008-12-13 15:32:12 +00001844Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001845 assert(Tok.is(tok::l_square) && "'[' expected");
1846 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1847
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001848 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001849 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001850 ConsumeCodeCompletionToken();
1851 SkipUntil(tok::r_square);
1852 return ExprError();
1853 }
1854
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001855 if (getLang().CPlusPlus) {
1856 // We completely separate the C and C++ cases because C++ requires
1857 // more complicated (read: slower) parsing.
1858
1859 // Handle send to super.
1860 // FIXME: This doesn't benefit from the same typo-correction we
1861 // get in Objective-C.
1862 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001863 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001864 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
1865 ExprArg(Actions));
1866
1867 // Parse the receiver, which is either a type or an expression.
1868 bool IsExpr;
1869 void *TypeOrExpr;
1870 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1871 SkipUntil(tok::r_square);
1872 return ExprError();
1873 }
1874
1875 if (IsExpr)
1876 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
1877 OwningExprResult(Actions, TypeOrExpr));
1878
1879 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1880 TypeOrExpr, ExprArg(Actions));
Chris Lattnerc59cb382010-05-31 18:18:22 +00001881 }
1882
1883 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001884 IdentifierInfo *Name = Tok.getIdentifierInfo();
1885 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor1569f952010-04-21 20:38:13 +00001886 TypeTy *ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001887 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001888 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001889 NextToken().is(tok::period),
1890 ReceiverType)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001891 case Action::ObjCSuperMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001892 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001893 ExprArg(Actions));
Douglas Gregor2725ca82010-04-21 19:57:20 +00001894
Douglas Gregor1569f952010-04-21 20:38:13 +00001895 case Action::ObjCClassMessage:
1896 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001897 SkipUntil(tok::r_square);
1898 return ExprError();
1899 }
1900
Douglas Gregor1569f952010-04-21 20:38:13 +00001901 ConsumeToken(); // the type name
1902
1903 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1904 ReceiverType,
Douglas Gregor2725ca82010-04-21 19:57:20 +00001905 ExprArg(Actions));
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001906
1907 case Action::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001908 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001909 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001910 }
Chris Lattner699b6612008-01-25 18:59:06 +00001911 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001912
1913 // Otherwise, an arbitrary expression can be the receiver of a send.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001914 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001915 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001916 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001917 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001918 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001919
Douglas Gregor2725ca82010-04-21 19:57:20 +00001920 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
1921 move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001922}
Sebastian Redl1d922962008-12-13 15:32:12 +00001923
Douglas Gregor2725ca82010-04-21 19:57:20 +00001924/// \brief Parse the remainder of an Objective-C message following the
1925/// '[' objc-receiver.
1926///
1927/// This routine handles sends to super, class messages (sent to a
1928/// class name), and instance messages (sent to an object), and the
1929/// target is represented by \p SuperLoc, \p ReceiverType, or \p
1930/// ReceiverExpr, respectively. Only one of these parameters may have
1931/// a valid value.
1932///
1933/// \param LBracLoc The location of the opening '['.
1934///
1935/// \param SuperLoc If this is a send to 'super', the location of the
1936/// 'super' keyword that indicates a send to the superclass.
1937///
1938/// \param ReceiverType If this is a class message, the type of the
1939/// class we are sending a message to.
1940///
1941/// \param ReceiverExpr If this is an instance message, the expression
1942/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00001943///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001944/// objc-message-args:
1945/// objc-selector
1946/// objc-keywordarg-list
1947///
1948/// objc-keywordarg-list:
1949/// objc-keywordarg
1950/// objc-keywordarg-list objc-keywordarg
1951///
Mike Stump1eb44332009-09-09 15:08:12 +00001952/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001953/// selector-name[opt] ':' objc-keywordexpr
1954///
1955/// objc-keywordexpr:
1956/// nonempty-expr-list
1957///
1958/// nonempty-expr-list:
1959/// assignment-expression
1960/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001961///
1962Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001963Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00001964 SourceLocation SuperLoc,
1965 TypeTy *ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00001966 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001967 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001968 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001969 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001970 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001971 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001972 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00001973 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr.get(),
Douglas Gregord3c68542009-11-19 01:08:35 +00001974 0, 0);
Douglas Gregordc845342010-05-25 05:58:43 +00001975 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00001976 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001977
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001978 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001979 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001980 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001981
Anders Carlssonff975cf2009-02-14 18:21:46 +00001982 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Steve Naroff68d331a2007-09-27 14:38:14 +00001984 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001985 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001986
Chris Lattnerdf195262007-10-09 17:51:17 +00001987 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001988 while (1) {
1989 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001990 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001991
Chris Lattnerdf195262007-10-09 17:51:17 +00001992 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001993 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001994 // We must manually skip to a ']', otherwise the expression skipper will
1995 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1996 // the enclosing expression.
1997 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001998 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001999 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002000
Steve Naroff68d331a2007-09-27 14:38:14 +00002001 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002002 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002003 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002004 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002005 // We must manually skip to a ']', otherwise the expression skipper will
2006 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2007 // the enclosing expression.
2008 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002009 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002010 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002011
Steve Naroff37387c92007-09-17 20:25:27 +00002012 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002013 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002014
Douglas Gregord3c68542009-11-19 01:08:35 +00002015 // Code completion after each argument.
2016 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002017 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002018 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002019 KeyIdents.data(),
2020 KeyIdents.size());
2021 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002022 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002023 KeyIdents.data(),
2024 KeyIdents.size());
2025 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002026 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr.get(),
Douglas Gregord3c68542009-11-19 01:08:35 +00002027 KeyIdents.data(),
2028 KeyIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00002029 ConsumeCodeCompletionToken();
Douglas Gregord3c68542009-11-19 01:08:35 +00002030 }
2031
Steve Naroff37387c92007-09-17 20:25:27 +00002032 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002033 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002034 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002035 break;
2036 // We have a selector or a colon, continue parsing.
2037 }
2038 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002039 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002040 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002041 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002042 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002043 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002044 // We must manually skip to a ']', otherwise the expression skipper will
2045 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2046 // the enclosing expression.
2047 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002048 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002049 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002050
Steve Naroff49f109c2007-11-15 13:05:42 +00002051 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002052 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002053 }
2054 } else if (!selIdent) {
2055 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002056
Chris Lattner4fef81d2008-08-05 06:19:09 +00002057 // We must manually skip to a ']', otherwise the expression skipper will
2058 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2059 // the enclosing expression.
2060 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002061 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002062 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002063
Chris Lattnerdf195262007-10-09 17:51:17 +00002064 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002065 if (Tok.is(tok::identifier))
2066 Diag(Tok, diag::err_expected_colon);
2067 else
2068 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002069 // We must manually skip to a ']', otherwise the expression skipper will
2070 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2071 // the enclosing expression.
2072 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002073 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002074 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002075
Chris Lattner699b6612008-01-25 18:59:06 +00002076 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002077
Steve Naroff29238a02007-10-05 18:42:47 +00002078 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002079 if (nKeys == 0)
2080 KeyIdents.push_back(selIdent);
2081 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002082
Douglas Gregor2725ca82010-04-21 19:57:20 +00002083 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002084 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002085 LBracLoc, SelectorLoc, RBracLoc,
2086 Action::MultiExprArg(Actions,
2087 KeyExprs.take(),
2088 KeyExprs.size()));
2089 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002090 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002091 LBracLoc, SelectorLoc, RBracLoc,
2092 Action::MultiExprArg(Actions,
2093 KeyExprs.take(),
2094 KeyExprs.size()));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002095 return Actions.ActOnInstanceMessage(getCurScope(), move(ReceiverExpr), Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002096 LBracLoc, SelectorLoc, RBracLoc,
2097 Action::MultiExprArg(Actions,
2098 KeyExprs.take(),
2099 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002100}
2101
Sebastian Redl1d922962008-12-13 15:32:12 +00002102Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00002103 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002104 if (Res.isInvalid()) return move(Res);
2105
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002106 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2107 // expressions. At this point, we know that the only valid thing that starts
2108 // with '@' is an @"".
2109 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002110 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002111 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002112 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002113
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002114 while (Tok.is(tok::at)) {
2115 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002116
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002117 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002118 if (!isTokenStringLiteral())
2119 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002120
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002121 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002122 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002123 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002124
Sebastian Redleffa8d12008-12-10 00:02:53 +00002125 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002126 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002127
2128 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2129 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002130}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002131
2132/// objc-encode-expression:
2133/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00002134Parser::OwningExprResult
2135Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002136 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002137
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002138 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002139
Chris Lattner4fef81d2008-08-05 06:19:09 +00002140 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002141 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2142
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002143 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002144
Douglas Gregor809070a2009-02-18 17:45:20 +00002145 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002146
Anders Carlsson4988ae32007-08-23 15:31:37 +00002147 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002148
Douglas Gregor809070a2009-02-18 17:45:20 +00002149 if (Ty.isInvalid())
2150 return ExprError();
2151
Mike Stump1eb44332009-09-09 15:08:12 +00002152 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002153 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002154}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002155
2156/// objc-protocol-expression
2157/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00002158Parser::OwningExprResult
2159Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002160 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002161
Chris Lattner4fef81d2008-08-05 06:19:09 +00002162 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002163 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2164
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002165 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002166
Chris Lattner4fef81d2008-08-05 06:19:09 +00002167 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002168 return ExprError(Diag(Tok, diag::err_expected_ident));
2169
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002170 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002171 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002172
Anders Carlsson4988ae32007-08-23 15:31:37 +00002173 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002174
Sebastian Redl1d922962008-12-13 15:32:12 +00002175 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2176 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002177}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002178
2179/// objc-selector-expression
2180/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00002181Parser::OwningExprResult
2182Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002183 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002184
Chris Lattner4fef81d2008-08-05 06:19:09 +00002185 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002186 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2187
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002188 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002189 SourceLocation LParenLoc = ConsumeParen();
2190 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002191 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002192 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
2193 return ExprError(Diag(Tok, diag::err_expected_ident));
2194
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002195 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002196 unsigned nColons = 0;
2197 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002198 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002199 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002200 return ExprError(Diag(Tok, diag::err_expected_colon));
2201
Chris Lattnercb53b362007-12-27 19:57:00 +00002202 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002203 ConsumeToken(); // Eat the ':'.
2204 if (Tok.is(tok::r_paren))
2205 break;
2206 // Check for another keyword selector.
2207 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002208 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002209 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002210 if (!SelIdent && Tok.isNot(tok::colon))
2211 break;
2212 }
Steve Naroff887407e2007-12-05 22:21:29 +00002213 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002214 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002215 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002216 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2217 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002218 }