blob: 8525c49d72666e2e8c8fdee8c03d1c7160532430 [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
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000053 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
Mike Stump1eb44332009-09-09 15:08:12 +000058/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000059/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000060///
Chris Lattnerb28317a2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000064 llvm::SmallVector<SourceLocation, 8> ClassLocs;
65
Mike Stump1eb44332009-09-09 15:08:12 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000068 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 Diag(Tok, diag::err_expected_ident);
70 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000071 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Reid Spencer5f016e22007-07-11 17:01:13 +000073 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000074 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000075 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattnerdf195262007-10-09 17:51:17 +000077 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000078 break;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ConsumeToken();
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Reid Spencer5f016e22007-07-11 17:01:13 +000083 // Consume the ';'.
84 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000085 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenekc09cba62009-11-17 23:12:20 +000087 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
88 ClassLocs.data(),
89 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000090}
91
Steve Naroffdac269b2007-08-20 21:31:48 +000092///
93/// objc-interface:
94/// objc-class-interface-attributes[opt] objc-class-interface
95/// objc-category-interface
96///
97/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +000098/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000099/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000101/// objc-interface-decl-list
102/// @end
103///
104/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
107/// objc-interface-decl-list
108/// @end
109///
110/// objc-superclass:
111/// ':' identifier
112///
113/// objc-class-interface-attributes:
114/// __attribute__((visibility("default")))
115/// __attribute__((visibility("hidden")))
116/// __attribute__((deprecated))
117/// __attribute__((unavailable))
118/// __attribute__((objc_exception)) - used by NSException on 64-bit
119///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000120Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000121 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000122 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
124 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000126 // Code completion after '@interface'.
127 if (Tok.is(tok::code_completion)) {
128 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
129 ConsumeToken();
130 }
131
Chris Lattnerdf195262007-10-09 17:51:17 +0000132 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000133 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000134 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000135 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000136
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000138 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000139 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chris Lattnerdf195262007-10-09 17:51:17 +0000141 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 SourceLocation lparenLoc = ConsumeParen();
143 SourceLocation categoryLoc, rparenLoc;
144 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000146 if (Tok.is(tok::code_completion)) {
147 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
148 ConsumeToken();
149 }
150
Steve Naroff527fe232007-08-23 19:56:30 +0000151 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000152 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000153 categoryId = Tok.getIdentifierInfo();
154 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000155 } else if (!getLang().ObjC2) {
156 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000159 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 Diag(Tok, diag::err_expected_rparen);
161 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000163 }
164 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Steve Naroffdac269b2007-08-20 21:31:48 +0000166 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000167 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000168 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000169 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000170 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000171 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
172 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000173 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Steve Naroffdac269b2007-08-20 21:31:48 +0000175 if (attrList) // categories don't support attributes.
176 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Jay Foadbeaaccd2009-05-21 09:52:38 +0000178 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000179 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000180 nameId, nameLoc,
181 categoryId, categoryLoc,
182 ProtocolRefs.data(),
183 ProtocolRefs.size(),
184 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000186 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000187 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000188 }
189 // Parse a class interface.
190 IdentifierInfo *superClassId = 0;
191 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000192
Chris Lattnerdf195262007-10-09 17:51:17 +0000193 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000194 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000195
196 // Code completion of superclass names.
197 if (Tok.is(tok::code_completion)) {
198 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
199 ConsumeToken();
200 }
201
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000204 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000205 }
206 superClassId = Tok.getIdentifierInfo();
207 superClassLoc = ConsumeToken();
208 }
209 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000211 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
212 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000213 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000214 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
215 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
218 DeclPtrTy ClsType =
219 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000220 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000221 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000222 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattnerdf195262007-10-09 17:51:17 +0000224 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000225 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000226
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000227 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000228 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000229}
230
John McCalld0014542009-12-03 22:31:13 +0000231/// The Objective-C property callback. This should be defined where
232/// it's used, but instead it's been lifted to here to support VS2005.
233struct Parser::ObjCPropertyCallback : FieldCallback {
234 Parser &P;
235 DeclPtrTy IDecl;
236 llvm::SmallVectorImpl<DeclPtrTy> &Props;
237 ObjCDeclSpec &OCDS;
238 SourceLocation AtLoc;
239 tok::ObjCKeywordKind MethodImplKind;
240
241 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
242 llvm::SmallVectorImpl<DeclPtrTy> &Props,
243 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
244 tok::ObjCKeywordKind MethodImplKind) :
245 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
246 MethodImplKind(MethodImplKind) {
247 }
248
249 DeclPtrTy invoke(FieldDeclarator &FD) {
250 if (FD.D.getIdentifier() == 0) {
251 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
252 << FD.D.getSourceRange();
253 return DeclPtrTy();
254 }
255 if (FD.BitfieldSize) {
256 P.Diag(AtLoc, diag::err_objc_property_bitfield)
257 << FD.D.getSourceRange();
258 return DeclPtrTy();
259 }
260
261 // Install the property declarator into interfaceDecl.
262 IdentifierInfo *SelName =
263 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
264
265 Selector GetterSel =
266 P.PP.getSelectorTable().getNullarySelector(SelName);
267 IdentifierInfo *SetterName = OCDS.getSetterName();
268 Selector SetterSel;
269 if (SetterName)
270 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
271 else
272 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
273 P.PP.getSelectorTable(),
274 FD.D.getIdentifier());
275 bool isOverridingProperty = false;
276 DeclPtrTy Property =
277 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
278 GetterSel, SetterSel, IDecl,
279 &isOverridingProperty,
280 MethodImplKind);
281 if (!isOverridingProperty)
282 Props.push_back(Property);
283
284 return Property;
285 }
286};
287
Steve Naroffdac269b2007-08-20 21:31:48 +0000288/// objc-interface-decl-list:
289/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000290/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000291/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000292/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000293/// objc-interface-decl-list declaration
294/// objc-interface-decl-list ';'
295///
Steve Naroff294494e2007-08-22 16:35:03 +0000296/// objc-method-requirement: [OBJC2]
297/// @required
298/// @optional
299///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000300void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000301 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000302 llvm::SmallVector<DeclPtrTy, 32> allMethods;
303 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000304 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000305 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattnerbc662af2008-10-20 06:10:06 +0000307 SourceLocation AtEndLoc;
308
Steve Naroff294494e2007-08-22 16:35:03 +0000309 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000310 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000311 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000312 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000313 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000314 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000315 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
316 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000317 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
318 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000319 continue;
320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattnere82a10f2008-10-20 05:46:22 +0000322 // Ignore excess semicolons.
323 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000324 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000325 continue;
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattnerbc662af2008-10-20 06:10:06 +0000328 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000330 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattnere82a10f2008-10-20 05:46:22 +0000332 // If we don't have an @ directive, parse it as a function definition.
333 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000334 // The code below does not consume '}'s because it is afraid of eating the
335 // end of a namespace. Because of the way this code is structured, an
336 // erroneous r_brace would cause an infinite loop if not handled here.
337 if (Tok.is(tok::r_brace))
338 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Steve Naroff4985ace2007-08-22 18:35:33 +0000340 // FIXME: as the name implies, this rule allows function definitions.
341 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000342 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000343 continue;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnere82a10f2008-10-20 05:46:22 +0000346 // Otherwise, we have an @ directive, eat the @.
347 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000348 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattnera2449b22008-10-20 05:57:40 +0000350 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000351 AtEndLoc = AtLoc;
352 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattnerbc662af2008-10-20 06:10:06 +0000355 // Eat the identifier.
356 ConsumeToken();
357
Chris Lattnera2449b22008-10-20 05:57:40 +0000358 switch (DirectiveKind) {
359 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000360 // FIXME: If someone forgets an @end on a protocol, this loop will
361 // continue to eat up tons of stuff and spew lots of nonsense errors. It
362 // would probably be better to bail out if we saw an @class or @interface
363 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000364 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000365 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000366 SkipUntil(tok::r_brace, tok::at);
367 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattnera2449b22008-10-20 05:57:40 +0000369 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000370 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000371 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000372 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000373 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000374 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000375 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000376 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000377 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattnera2449b22008-10-20 05:57:40 +0000379 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000380 if (!getLang().ObjC2)
381 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
382
Chris Lattnere82a10f2008-10-20 05:46:22 +0000383 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000384 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000385 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000386 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
387 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000388
John McCalld0014542009-12-03 22:31:13 +0000389 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
390 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000391
Chris Lattnere82a10f2008-10-20 05:46:22 +0000392 // Parse all the comma separated declarators.
393 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000394 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000396 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
397 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000398 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000399 }
Steve Naroff294494e2007-08-22 16:35:03 +0000400 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000401
402 // We break out of the big loop in two cases: when we see @end or when we see
403 // EOF. In the former case, eat the @end. In the later case, emit an error.
404 if (Tok.isObjCAtKeyword(tok::objc_end))
405 ConsumeToken(); // the "end" identifier
406 else
407 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattnera2449b22008-10-20 05:57:40 +0000409 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000410 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000411 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000412 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000413 allProperties.data(), allProperties.size(),
414 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000415}
416
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000417/// Parse property attribute declarations.
418///
419/// property-attr-decl: '(' property-attrlist ')'
420/// property-attrlist:
421/// property-attribute
422/// property-attrlist ',' property-attribute
423/// property-attribute:
424/// getter '=' identifier
425/// setter '=' identifier ':'
426/// readonly
427/// readwrite
428/// assign
429/// retain
430/// copy
431/// nonatomic
432///
Douglas Gregor4ad96852009-11-19 07:41:15 +0000433void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
434 DeclPtrTy *Methods,
435 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000436 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000437 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000439 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000440 if (Tok.is(tok::code_completion)) {
Douglas Gregora93b1082009-11-18 23:08:07 +0000441 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroffece8e712009-10-08 21:55:05 +0000442 ConsumeToken();
443 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000444 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000446 // If this is not an identifier at all, bail out early.
447 if (II == 0) {
448 MatchRHSPunctuation(tok::r_paren, LHSLoc);
449 return;
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Chris Lattner156b0612008-10-20 07:37:22 +0000452 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner92e62b02008-11-20 04:42:34 +0000454 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000455 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000456 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000457 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000458 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000459 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000460 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000461 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000462 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000463 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000464 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000465 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000466 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000467 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000468 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
469 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000470 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Douglas Gregor4ad96852009-11-19 07:41:15 +0000472 if (Tok.is(tok::code_completion)) {
473 if (II->getNameStart()[0] == 's')
474 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
475 Methods, NumMethods);
476 else
477 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
478 Methods, NumMethods);
479 ConsumeToken();
480 }
481
Chris Lattner8ca329c2008-10-20 07:24:39 +0000482 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000483 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000484 SkipUntil(tok::r_paren);
485 return;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Daniel Dunbare013d682009-10-18 20:26:12 +0000488 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000489 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
490 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000491 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner156b0612008-10-20 07:37:22 +0000493 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
494 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000495 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000496 } else {
497 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
498 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000499 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000500 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000501 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000502 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000503 SkipUntil(tok::r_paren);
504 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner156b0612008-10-20 07:37:22 +0000507 if (Tok.isNot(tok::comma))
508 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner156b0612008-10-20 07:37:22 +0000510 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattner156b0612008-10-20 07:37:22 +0000513 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000514}
515
Steve Naroff3536b442007-09-06 21:24:23 +0000516/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000517/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000518/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000519///
520/// objc-instance-method: '-'
521/// objc-class-method: '+'
522///
Steve Naroff4985ace2007-08-22 18:35:33 +0000523/// objc-method-attributes: [OBJC2]
524/// __attribute__((deprecated))
525///
Mike Stump1eb44332009-09-09 15:08:12 +0000526Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000527 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000528 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000529
Mike Stump1eb44332009-09-09 15:08:12 +0000530 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000531 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattnerb28317a2009-03-28 19:18:32 +0000533 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000534 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000535 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000536 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000537}
538
539/// objc-selector:
540/// identifier
541/// one of
542/// enum struct union if else while do for switch case default
543/// break continue return goto asm sizeof typeof __alignof
544/// unsigned long const short volatile signed restrict _Complex
545/// in out inout bycopy byref oneway int char float double void _Bool
546///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000547IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000548 switch (Tok.getKind()) {
549 default:
550 return 0;
551 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000552 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000553 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000554 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000555 case tok::kw_break:
556 case tok::kw_case:
557 case tok::kw_catch:
558 case tok::kw_char:
559 case tok::kw_class:
560 case tok::kw_const:
561 case tok::kw_const_cast:
562 case tok::kw_continue:
563 case tok::kw_default:
564 case tok::kw_delete:
565 case tok::kw_do:
566 case tok::kw_double:
567 case tok::kw_dynamic_cast:
568 case tok::kw_else:
569 case tok::kw_enum:
570 case tok::kw_explicit:
571 case tok::kw_export:
572 case tok::kw_extern:
573 case tok::kw_false:
574 case tok::kw_float:
575 case tok::kw_for:
576 case tok::kw_friend:
577 case tok::kw_goto:
578 case tok::kw_if:
579 case tok::kw_inline:
580 case tok::kw_int:
581 case tok::kw_long:
582 case tok::kw_mutable:
583 case tok::kw_namespace:
584 case tok::kw_new:
585 case tok::kw_operator:
586 case tok::kw_private:
587 case tok::kw_protected:
588 case tok::kw_public:
589 case tok::kw_register:
590 case tok::kw_reinterpret_cast:
591 case tok::kw_restrict:
592 case tok::kw_return:
593 case tok::kw_short:
594 case tok::kw_signed:
595 case tok::kw_sizeof:
596 case tok::kw_static:
597 case tok::kw_static_cast:
598 case tok::kw_struct:
599 case tok::kw_switch:
600 case tok::kw_template:
601 case tok::kw_this:
602 case tok::kw_throw:
603 case tok::kw_true:
604 case tok::kw_try:
605 case tok::kw_typedef:
606 case tok::kw_typeid:
607 case tok::kw_typename:
608 case tok::kw_typeof:
609 case tok::kw_union:
610 case tok::kw_unsigned:
611 case tok::kw_using:
612 case tok::kw_virtual:
613 case tok::kw_void:
614 case tok::kw_volatile:
615 case tok::kw_wchar_t:
616 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000617 case tok::kw__Bool:
618 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000619 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000620 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000621 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000622 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000623 }
Steve Naroff294494e2007-08-22 16:35:03 +0000624}
625
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000626/// objc-for-collection-in: 'in'
627///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000628bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000629 // FIXME: May have to do additional look-ahead to only allow for
630 // valid tokens following an 'in'; such as an identifier, unary operators,
631 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000632 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000633 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000634}
635
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000636/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000637/// qualifier list and builds their bitmask representation in the input
638/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000639///
640/// objc-type-qualifiers:
641/// objc-type-qualifier
642/// objc-type-qualifiers objc-type-qualifier
643///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000644void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000645 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000646 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000647 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Chris Lattnere8b724d2007-12-12 06:56:32 +0000649 const IdentifierInfo *II = Tok.getIdentifierInfo();
650 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000652 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000654 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000655 switch (i) {
656 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000657 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
658 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
659 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
660 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
661 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
662 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000663 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000664 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000665 ConsumeToken();
666 II = 0;
667 break;
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Chris Lattnere8b724d2007-12-12 06:56:32 +0000670 // If this wasn't a recognized qualifier, bail out.
671 if (II) return;
672 }
673}
674
675/// objc-type-name:
676/// '(' objc-type-qualifiers[opt] type-name ')'
677/// '(' objc-type-qualifiers[opt] ')'
678///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000679Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000680 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattner4a76b292008-10-22 03:52:06 +0000682 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000683 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000685 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000686 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000687
Chris Lattner4a76b292008-10-22 03:52:06 +0000688 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000689 if (isTypeSpecifierQualifier()) {
690 TypeResult TypeSpec = ParseTypeName();
691 if (!TypeSpec.isInvalid())
692 Ty = TypeSpec.get();
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Steve Naroffd7333c22008-10-21 14:15:04 +0000695 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000696 ConsumeParen();
697 else if (Tok.getLocation() == TypeStartLoc) {
698 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000699 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000700 SkipUntil(tok::r_paren);
701 } else {
702 // Otherwise, we found *something*, but didn't get a ')' in the right
703 // place. Emit an error then return what we have as the type.
704 MatchRHSPunctuation(tok::r_paren, LParenLoc);
705 }
Steve Narofff28b2642007-09-05 23:30:30 +0000706 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000707}
708
709/// objc-method-decl:
710/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000711/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000712/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000713/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000714///
715/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000716/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000717/// objc-keyword-selector objc-keyword-decl
718///
719/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000720/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
721/// objc-selector ':' objc-keyword-attributes[opt] identifier
722/// ':' objc-type-name objc-keyword-attributes[opt] identifier
723/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000724///
Steve Naroff4985ace2007-08-22 18:35:33 +0000725/// objc-parmlist:
726/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000727///
Steve Naroff4985ace2007-08-22 18:35:33 +0000728/// objc-parms:
729/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000730///
Steve Naroff4985ace2007-08-22 18:35:33 +0000731/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000732/// , ...
733///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000734/// objc-keyword-attributes: [OBJC2]
735/// __attribute__((unused))
736///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000737Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
738 tok::TokenKind mType,
739 DeclPtrTy IDecl,
740 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000741 ParsingDeclRAIIObject PD(*this);
742
Chris Lattnere8904e92008-08-23 01:48:03 +0000743 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000744 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000746 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000747 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Steve Naroffbef11852007-10-26 20:53:56 +0000749 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000750 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000751
Steve Naroff84c43102009-02-11 20:43:13 +0000752 // An unnamed colon is valid.
753 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000754 Diag(Tok, diag::err_expected_selector_for_method)
755 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000756 // Skip until we get a ; or {}.
757 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000758 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000761 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000762 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000763 // If attributes exist after the method, parse them.
764 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000765 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000766 MethodAttrs = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattnerff384912007-10-07 02:00:24 +0000768 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000769 DeclPtrTy Result
770 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000771 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000772 0, CargNames, MethodAttrs,
773 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000774 PD.complete(Result);
775 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000776 }
Steve Narofff28b2642007-09-05 23:30:30 +0000777
Steve Naroff68d331a2007-09-27 14:38:14 +0000778 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000779 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Chris Lattnerff384912007-10-07 02:00:24 +0000781 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000782 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattnerff384912007-10-07 02:00:24 +0000784 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000785 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000786 Diag(Tok, diag::err_expected_colon);
787 break;
788 }
789 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattnere294d3f2009-04-11 18:57:04 +0000791 ArgInfo.Type = 0;
792 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
793 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
794
Chris Lattnerff384912007-10-07 02:00:24 +0000795 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000796 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000797 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000798 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000799
Chris Lattnerdf195262007-10-09 17:51:17 +0000800 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000801 Diag(Tok, diag::err_expected_ident); // missing argument name.
802 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Chris Lattnere294d3f2009-04-11 18:57:04 +0000805 ArgInfo.Name = Tok.getIdentifierInfo();
806 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000807 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnere294d3f2009-04-11 18:57:04 +0000809 ArgInfos.push_back(ArgInfo);
810 KeyIdents.push_back(SelIdent);
811
Chris Lattnerff384912007-10-07 02:00:24 +0000812 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000813 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000814 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000815 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000816 break;
817 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Steve Naroff335eafa2007-11-15 12:35:21 +0000820 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Chris Lattnerff384912007-10-07 02:00:24 +0000822 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000823 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000824 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000825 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000826 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000827 ConsumeToken();
828 break;
829 }
Chris Lattnerff384912007-10-07 02:00:24 +0000830 DeclSpec DS;
831 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000832 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000833 Declarator ParmDecl(DS, Declarator::PrototypeContext);
834 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000835 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Chris Lattnerff384912007-10-07 02:00:24 +0000838 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000839 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000840 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000841 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000842 MethodAttrs = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000844 if (KeyIdents.size() == 0)
845 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000846 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
847 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000848 DeclPtrTy Result
849 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000850 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000851 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000852 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000853 PD.complete(Result);
854 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000855}
856
Steve Naroffdac269b2007-08-20 21:31:48 +0000857/// objc-protocol-refs:
858/// '<' identifier-list '>'
859///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000860bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000861ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000862 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
863 bool WarnOnDeclarations,
864 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000865 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000867 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Chris Lattnere13b9592008-07-26 04:03:38 +0000869 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattnere13b9592008-07-26 04:03:38 +0000871 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000872 if (Tok.is(tok::code_completion)) {
873 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
874 ProtocolIdents.size());
875 ConsumeToken();
876 }
877
Chris Lattnere13b9592008-07-26 04:03:38 +0000878 if (Tok.isNot(tok::identifier)) {
879 Diag(Tok, diag::err_expected_ident);
880 SkipUntil(tok::greater);
881 return true;
882 }
883 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
884 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000885 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000886 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Chris Lattnere13b9592008-07-26 04:03:38 +0000888 if (Tok.isNot(tok::comma))
889 break;
890 ConsumeToken();
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Chris Lattnere13b9592008-07-26 04:03:38 +0000893 // Consume the '>'.
894 if (Tok.isNot(tok::greater)) {
895 Diag(Tok, diag::err_expected_greater);
896 return true;
897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Chris Lattnere13b9592008-07-26 04:03:38 +0000899 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattnere13b9592008-07-26 04:03:38 +0000901 // Convert the list of protocols identifiers into a list of protocol decls.
902 Actions.FindProtocolDeclaration(WarnOnDeclarations,
903 &ProtocolIdents[0], ProtocolIdents.size(),
904 Protocols);
905 return false;
906}
907
Steve Naroffdac269b2007-08-20 21:31:48 +0000908/// objc-class-instance-variables:
909/// '{' objc-instance-variable-decl-list[opt] '}'
910///
911/// objc-instance-variable-decl-list:
912/// objc-visibility-spec
913/// objc-instance-variable-decl ';'
914/// ';'
915/// objc-instance-variable-decl-list objc-visibility-spec
916/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
917/// objc-instance-variable-decl-list ';'
918///
919/// objc-visibility-spec:
920/// @private
921/// @protected
922/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000923/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000924///
925/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000926/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000927///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000928void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000929 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000930 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000931 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000932
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000933 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000934
Steve Naroffddbff782007-08-21 21:17:12 +0000935 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000937 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000938 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000939 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000940 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Steve Naroffddbff782007-08-21 21:17:12 +0000942 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000943 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000944 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000945 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +0000946 ConsumeToken();
947 continue;
948 }
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Steve Naroffddbff782007-08-21 21:17:12 +0000950 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000951 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000952 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000953 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000954 case tok::objc_private:
955 case tok::objc_public:
956 case tok::objc_protected:
957 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000958 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000959 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000960 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000961 default:
962 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000963 continue;
964 }
965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
John McCallbdd563e2009-11-03 02:38:08 +0000967 struct ObjCIvarCallback : FieldCallback {
968 Parser &P;
969 DeclPtrTy IDecl;
970 tok::ObjCKeywordKind visibility;
971 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
972
973 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
974 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
975 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
976 }
977
978 DeclPtrTy invoke(FieldDeclarator &FD) {
979 // Install the declarator into the interface decl.
980 DeclPtrTy Field
981 = P.Actions.ActOnIvar(P.CurScope,
982 FD.D.getDeclSpec().getSourceRange().getBegin(),
983 IDecl, FD.D, FD.BitfieldSize, visibility);
984 AllIvarDecls.push_back(Field);
985 return Field;
986 }
987 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
988
Chris Lattnere1359422008-04-10 06:46:29 +0000989 // Parse all the comma separated declarators.
990 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000991 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Chris Lattnerdf195262007-10-09 17:51:17 +0000993 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000994 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000995 } else {
996 Diag(Tok, diag::err_expected_semi_decl_list);
997 // Skip to end of block or statement
998 SkipUntil(tok::r_brace, true, true);
999 }
1000 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001001 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +00001002 // Call ActOnFields() even if we don't have any decls. This is useful
1003 // for code rewriting tools that need to be aware of the empty list.
1004 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001005 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001006 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001007 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001008}
Steve Naroffdac269b2007-08-20 21:31:48 +00001009
1010/// objc-protocol-declaration:
1011/// objc-protocol-definition
1012/// objc-protocol-forward-reference
1013///
1014/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001015/// @protocol identifier
1016/// objc-protocol-refs[opt]
1017/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001018/// @end
1019///
1020/// objc-protocol-forward-reference:
1021/// @protocol identifier-list ';'
1022///
1023/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001024/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001025/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001026Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1027 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001028 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001029 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1030 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Douglas Gregor083128f2009-11-18 04:49:41 +00001032 if (Tok.is(tok::code_completion)) {
1033 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1034 ConsumeToken();
1035 }
1036
Chris Lattnerdf195262007-10-09 17:51:17 +00001037 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001038 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001039 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001040 }
1041 // Save the protocol name, then consume it.
1042 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1043 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattnerdf195262007-10-09 17:51:17 +00001045 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001046 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001047 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001048 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001049 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Chris Lattnerdf195262007-10-09 17:51:17 +00001052 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001053 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1054 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1055
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001056 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001057 while (1) {
1058 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001059 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001060 Diag(Tok, diag::err_expected_ident);
1061 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001062 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001063 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001064 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1065 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001066 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Chris Lattnerdf195262007-10-09 17:51:17 +00001068 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001069 break;
1070 }
1071 // Consume the ';'.
1072 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001073 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Steve Naroffe440eb82007-10-10 17:32:04 +00001075 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001076 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001077 ProtocolRefs.size(),
1078 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001081 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001082 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001083
Chris Lattnerb28317a2009-03-28 19:18:32 +00001084 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001085 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001086 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001087 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1088 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001089 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattnerb28317a2009-03-28 19:18:32 +00001091 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001092 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001093 ProtocolRefs.data(),
1094 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001095 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001096 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001097 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001098}
Steve Naroffdac269b2007-08-20 21:31:48 +00001099
1100/// objc-implementation:
1101/// objc-class-implementation-prologue
1102/// objc-category-implementation-prologue
1103///
1104/// objc-class-implementation-prologue:
1105/// @implementation identifier objc-superclass[opt]
1106/// objc-class-instance-variables[opt]
1107///
1108/// objc-category-implementation-prologue:
1109/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001110Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001111 SourceLocation atLoc) {
1112 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1113 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1114 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001116 // Code completion after '@implementation'.
1117 if (Tok.is(tok::code_completion)) {
1118 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1119 ConsumeToken();
1120 }
1121
Chris Lattnerdf195262007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001124 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001125 }
1126 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001128 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001129
1130 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001131 // we have a category implementation.
1132 SourceLocation lparenLoc = ConsumeParen();
1133 SourceLocation categoryLoc, rparenLoc;
1134 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001136 if (Tok.is(tok::code_completion)) {
1137 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1138 ConsumeToken();
1139 }
1140
Chris Lattnerdf195262007-10-09 17:51:17 +00001141 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001142 categoryId = Tok.getIdentifierInfo();
1143 categoryLoc = ConsumeToken();
1144 } else {
1145 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001146 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001147 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001148 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001149 Diag(Tok, diag::err_expected_rparen);
1150 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001151 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001152 }
1153 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001154 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001155 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001156 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001157 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001158 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001159 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001160 }
1161 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001162 SourceLocation superClassLoc;
1163 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001164 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001165 // We have a super class
1166 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001168 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001169 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001170 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001171 superClassId = Tok.getIdentifierInfo();
1172 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001173 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001174 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001175 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001176 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Steve Naroff60fccee2007-10-29 21:38:07 +00001178 if (Tok.is(tok::l_brace)) // we have ivars
1179 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001180 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001181 PendingObjCImpDecl.push_back(ObjCImpDecl);
1182
Chris Lattnerb28317a2009-03-28 19:18:32 +00001183 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001184}
Steve Naroff60fccee2007-10-29 21:38:07 +00001185
Chris Lattnerb28317a2009-03-28 19:18:32 +00001186Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001187 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1188 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001189 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001190 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001191 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001192 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001193 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001194 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001195 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001196 else
1197 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001198 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001199}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001200
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001201Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001202 if (PendingObjCImpDecl.empty())
1203 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1204 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1205 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1206 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1207}
1208
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001209/// compatibility-alias-decl:
1210/// @compatibility_alias alias-name class-name ';'
1211///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001212Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001213 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1214 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1215 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001216 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001217 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001218 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001219 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001220 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1221 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001222 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001223 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001224 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001225 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001226 IdentifierInfo *classId = Tok.getIdentifierInfo();
1227 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1228 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001229 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001230 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001231 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001232 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1233 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001234}
1235
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001236/// property-synthesis:
1237/// @synthesize property-ivar-list ';'
1238///
1239/// property-ivar-list:
1240/// property-ivar
1241/// property-ivar-list ',' property-ivar
1242///
1243/// property-ivar:
1244/// identifier
1245/// identifier '=' identifier
1246///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001247Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001248 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1249 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001250 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Douglas Gregorb328c422009-11-18 19:45:45 +00001252 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001253 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001254 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001255 ConsumeToken();
1256 }
1257
Douglas Gregorb328c422009-11-18 19:45:45 +00001258 if (Tok.isNot(tok::identifier)) {
1259 Diag(Tok, diag::err_synthesized_property_name);
1260 SkipUntil(tok::semi);
1261 return DeclPtrTy();
1262 }
1263
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001264 IdentifierInfo *propertyIvar = 0;
1265 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1266 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001267 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001268 // property '=' ivar-name
1269 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001270
1271 if (Tok.is(tok::code_completion)) {
1272 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1273 ObjCImpDecl);
1274 ConsumeToken();
1275 }
1276
Chris Lattnerdf195262007-10-09 17:51:17 +00001277 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001278 Diag(Tok, diag::err_expected_ident);
1279 break;
1280 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001281 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001282 ConsumeToken(); // consume ivar-name
1283 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001284 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1285 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001286 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001287 break;
1288 ConsumeToken(); // consume ','
1289 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001290 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001291 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001292 SkipUntil(tok::semi);
1293 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001294 else
1295 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001296 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001297}
1298
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001299/// property-dynamic:
1300/// @dynamic property-list
1301///
1302/// property-list:
1303/// identifier
1304/// property-list ',' identifier
1305///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001306Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001307 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1308 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1309 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001310 while (true) {
1311 if (Tok.is(tok::code_completion)) {
1312 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1313 ConsumeToken();
1314 }
1315
1316 if (Tok.isNot(tok::identifier)) {
1317 Diag(Tok, diag::err_expected_ident);
1318 SkipUntil(tok::semi);
1319 return DeclPtrTy();
1320 }
1321
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001322 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1323 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1324 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1325 propertyId, 0);
1326
Chris Lattnerdf195262007-10-09 17:51:17 +00001327 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001328 break;
1329 ConsumeToken(); // consume ','
1330 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001331 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001332 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001333 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001334}
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001336/// objc-throw-statement:
1337/// throw expression[opt];
1338///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001339Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001340 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001341 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001342 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001343 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001344 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001345 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001346 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001347 }
1348 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001349 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001350 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001351}
1352
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001353/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001354/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001355///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001356Parser::OwningStmtResult
1357Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001358 ConsumeToken(); // consume synchronized
1359 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001360 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001361 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001362 }
1363 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001364 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001365 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001366 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001367 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001368 }
1369 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001370 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001371 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001372 }
1373 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001374 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001375 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001376 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001377 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001378 // Enter a scope to hold everything within the compound stmt. Compound
1379 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001380 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001381
Sebastian Redl61364dd2008-12-11 19:30:53 +00001382 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001383
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001384 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001385 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001386 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001387 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001388}
1389
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001390/// objc-try-catch-statement:
1391/// @try compound-statement objc-catch-list[opt]
1392/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1393///
1394/// objc-catch-list:
1395/// @catch ( parameter-declaration ) compound-statement
1396/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1397/// catch-parameter-declaration:
1398/// parameter-declaration
1399/// '...' [OBJC2]
1400///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001401Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001402 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001403
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001404 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001405 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001406 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001407 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001408 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001409 OwningStmtResult CatchStmts(Actions);
1410 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001411 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001412 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001413 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001414 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001415 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001416
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001418 // At this point, we need to lookahead to determine if this @ is the start
1419 // of an @catch or @finally. We don't want to consume the @ token if this
1420 // is an @try or @encode or something else.
1421 Token AfterAt = GetLookAheadToken(1);
1422 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1423 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1424 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001425
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001426 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001427 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001428 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001429 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001430 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001431 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001432 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001433 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001434 DeclSpec DS;
1435 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001436 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001437 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001438 // we must accept either 'declarator' or 'abstract-declarator' here.
1439 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1440 ParseDeclarator(ParmDecl);
1441
1442 // Inform the actions module about the parameter declarator, so it
1443 // gets added to the current scope.
1444 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001445 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001446 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Steve Naroff93a25952009-04-07 22:56:58 +00001448 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Steve Naroff93a25952009-04-07 22:56:58 +00001450 if (Tok.is(tok::r_paren))
1451 RParenLoc = ConsumeParen();
1452 else // Skip over garbage, until we get to ')'. Eat the ')'.
1453 SkipUntil(tok::r_paren, true, false);
1454
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001455 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001456 if (Tok.is(tok::l_brace))
1457 CatchBody = ParseCompoundStatementBody();
1458 else
1459 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001460 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001461 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001462 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001463 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001464 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001465 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001466 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1467 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001468 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001469 }
1470 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001471 } else {
1472 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001473 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001474 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001475
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001476 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001477 if (Tok.is(tok::l_brace))
1478 FinallyBody = ParseCompoundStatementBody();
1479 else
1480 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001481 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001482 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001483 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001484 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001485 catch_or_finally_seen = true;
1486 break;
1487 }
1488 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001489 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001490 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001491 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001492 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001493 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1494 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001495}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001496
Steve Naroff3536b442007-09-06 21:24:23 +00001497/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001498///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001499Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1500 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Chris Lattner49f28ca2009-03-05 08:00:35 +00001502 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1503 PP.getSourceManager(),
1504 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001506 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001507 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001508 if (ObjCImpDecl) {
1509 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001510 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001511 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001512 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001513 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001514
Steve Naroff409be832007-11-11 19:54:21 +00001515 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001516 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001517 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Steve Naroff409be832007-11-11 19:54:21 +00001519 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1520 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Steve Naroff409be832007-11-11 19:54:21 +00001522 // If we didn't find the '{', bail out.
1523 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001524 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001525 }
Steve Naroff409be832007-11-11 19:54:21 +00001526 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Steve Naroff409be832007-11-11 19:54:21 +00001528 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001529 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Steve Naroff409be832007-11-11 19:54:21 +00001531 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001532 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001533 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001534
1535 OwningStmtResult FnBody(ParseCompoundStatementBody());
1536
Steve Naroff409be832007-11-11 19:54:21 +00001537 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001538 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001539 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1540 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001541
Steve Naroff32ce8372009-03-02 22:00:56 +00001542 // TODO: Pass argument information.
1543 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Steve Naroff409be832007-11-11 19:54:21 +00001545 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001546 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001547
Steve Naroff71c0a952007-11-13 23:01:27 +00001548 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001549}
Anders Carlsson55085182007-08-21 17:43:55 +00001550
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001551Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001552 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001553 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001554 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1555 return ParseObjCThrowStmt(AtLoc);
1556 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1557 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001558 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001559 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001560 // If the expression is invalid, skip ahead to the next semicolon. Not
1561 // doing this opens us up to the possibility of infinite loops if
1562 // ParseExpression does not consume any tokens.
1563 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001564 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001565 }
1566 // Otherwise, eat the semicolon.
1567 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001568 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001569}
1570
Sebastian Redl1d922962008-12-13 15:32:12 +00001571Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001572 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001573 case tok::string_literal: // primary-expression: string-literal
1574 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001575 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001576 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001577 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001578 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001579
Chris Lattner4fef81d2008-08-05 06:19:09 +00001580 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1581 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001582 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001583 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001584 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001585 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001586 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001587 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001588 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001589 }
Anders Carlsson55085182007-08-21 17:43:55 +00001590 }
Anders Carlsson55085182007-08-21 17:43:55 +00001591}
1592
Mike Stump1eb44332009-09-09 15:08:12 +00001593/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001594/// '[' objc-receiver objc-message-args ']'
1595///
1596/// objc-receiver:
1597/// expression
1598/// class-name
1599/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001600Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001601 assert(Tok.is(tok::l_square) && "'[' expected");
1602 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1603
1604 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001605 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001606 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001607 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1608 SourceLocation NameLoc = ConsumeToken();
1609 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1610 ExprArg(Actions));
1611 }
Chris Lattner699b6612008-01-25 18:59:06 +00001612 }
1613
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001614 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001615 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001616 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001617 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001618 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001619
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001620 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001621 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001622}
Sebastian Redl1d922962008-12-13 15:32:12 +00001623
Chris Lattner699b6612008-01-25 18:59:06 +00001624/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1625/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001626///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001627/// objc-message-args:
1628/// objc-selector
1629/// objc-keywordarg-list
1630///
1631/// objc-keywordarg-list:
1632/// objc-keywordarg
1633/// objc-keywordarg-list objc-keywordarg
1634///
Mike Stump1eb44332009-09-09 15:08:12 +00001635/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001636/// selector-name[opt] ':' objc-keywordexpr
1637///
1638/// objc-keywordexpr:
1639/// nonempty-expr-list
1640///
1641/// nonempty-expr-list:
1642/// assignment-expression
1643/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001644///
1645Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001646Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001647 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001648 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001649 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001650 if (Tok.is(tok::code_completion)) {
1651 if (ReceiverName)
Douglas Gregord3c68542009-11-19 01:08:35 +00001652 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1653 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001654 else
Douglas Gregord3c68542009-11-19 01:08:35 +00001655 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1656 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001657 ConsumeToken();
1658 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001659
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001660 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001661 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001662 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001663
Anders Carlssonff975cf2009-02-14 18:21:46 +00001664 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Steve Naroff68d331a2007-09-27 14:38:14 +00001666 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001667 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001668
Chris Lattnerdf195262007-10-09 17:51:17 +00001669 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001670 while (1) {
1671 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001672 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001673
Chris Lattnerdf195262007-10-09 17:51:17 +00001674 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001675 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001676 // We must manually skip to a ']', otherwise the expression skipper will
1677 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1678 // the enclosing expression.
1679 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001680 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001681 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001682
Steve Naroff68d331a2007-09-27 14:38:14 +00001683 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001684 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001685 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001686 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001687 // We must manually skip to a ']', otherwise the expression skipper will
1688 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1689 // the enclosing expression.
1690 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001691 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001692 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001693
Steve Naroff37387c92007-09-17 20:25:27 +00001694 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001695 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001696
Douglas Gregord3c68542009-11-19 01:08:35 +00001697 // Code completion after each argument.
1698 if (Tok.is(tok::code_completion)) {
1699 if (ReceiverName)
1700 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1701 KeyIdents.data(),
1702 KeyIdents.size());
1703 else
1704 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1705 KeyIdents.data(),
1706 KeyIdents.size());
1707 ConsumeToken();
1708 }
1709
Steve Naroff37387c92007-09-17 20:25:27 +00001710 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001711 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001712 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001713 break;
1714 // We have a selector or a colon, continue parsing.
1715 }
1716 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001717 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001718 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001719 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001720 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001721 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001722 // We must manually skip to a ']', otherwise the expression skipper will
1723 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1724 // the enclosing expression.
1725 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001726 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001727 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001728
Steve Naroff49f109c2007-11-15 13:05:42 +00001729 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001730 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001731 }
1732 } else if (!selIdent) {
1733 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001734
Chris Lattner4fef81d2008-08-05 06:19:09 +00001735 // We must manually skip to a ']', otherwise the expression skipper will
1736 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1737 // the enclosing expression.
1738 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001739 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001740 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001741
Chris Lattnerdf195262007-10-09 17:51:17 +00001742 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001743 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001744 // We must manually skip to a ']', otherwise the expression skipper will
1745 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1746 // the enclosing expression.
1747 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001748 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001749 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001750
Chris Lattner699b6612008-01-25 18:59:06 +00001751 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001752
Steve Naroff29238a02007-10-05 18:42:47 +00001753 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001754 if (nKeys == 0)
1755 KeyIdents.push_back(selIdent);
1756 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001757
Chris Lattnerff384912007-10-07 02:00:24 +00001758 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001759 if (ReceiverName)
1760 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001761 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001762 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001763 KeyExprs.take(), KeyExprs.size()));
1764 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001765 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001766 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001767}
1768
Sebastian Redl1d922962008-12-13 15:32:12 +00001769Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001770 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001771 if (Res.isInvalid()) return move(Res);
1772
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001773 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1774 // expressions. At this point, we know that the only valid thing that starts
1775 // with '@' is an @"".
1776 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001777 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001778 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001779 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001780
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001781 while (Tok.is(tok::at)) {
1782 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001783
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001784 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001785 if (!isTokenStringLiteral())
1786 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001787
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001788 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001789 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001790 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001791
Sebastian Redleffa8d12008-12-10 00:02:53 +00001792 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001793 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001794
1795 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1796 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001797}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001798
1799/// objc-encode-expression:
1800/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001801Parser::OwningExprResult
1802Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001803 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001804
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001805 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001806
Chris Lattner4fef81d2008-08-05 06:19:09 +00001807 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001808 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1809
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001810 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001811
Douglas Gregor809070a2009-02-18 17:45:20 +00001812 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001813
Anders Carlsson4988ae32007-08-23 15:31:37 +00001814 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001815
Douglas Gregor809070a2009-02-18 17:45:20 +00001816 if (Ty.isInvalid())
1817 return ExprError();
1818
Mike Stump1eb44332009-09-09 15:08:12 +00001819 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001820 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001821}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001822
1823/// objc-protocol-expression
1824/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001825Parser::OwningExprResult
1826Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001827 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001828
Chris Lattner4fef81d2008-08-05 06:19:09 +00001829 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001830 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1831
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001832 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001833
Chris Lattner4fef81d2008-08-05 06:19:09 +00001834 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001835 return ExprError(Diag(Tok, diag::err_expected_ident));
1836
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001837 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001838 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001839
Anders Carlsson4988ae32007-08-23 15:31:37 +00001840 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001841
Sebastian Redl1d922962008-12-13 15:32:12 +00001842 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1843 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001844}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001845
1846/// objc-selector-expression
1847/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001848Parser::OwningExprResult
1849Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001850 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001851
Chris Lattner4fef81d2008-08-05 06:19:09 +00001852 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001853 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1854
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001855 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001856 SourceLocation LParenLoc = ConsumeParen();
1857 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001858 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001859 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1860 return ExprError(Diag(Tok, diag::err_expected_ident));
1861
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001862 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001863 unsigned nColons = 0;
1864 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001865 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001866 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001867 return ExprError(Diag(Tok, diag::err_expected_colon));
1868
Chris Lattnercb53b362007-12-27 19:57:00 +00001869 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001870 ConsumeToken(); // Eat the ':'.
1871 if (Tok.is(tok::r_paren))
1872 break;
1873 // Check for another keyword selector.
1874 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001875 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001876 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001877 if (!SelIdent && Tok.isNot(tok::colon))
1878 break;
1879 }
Steve Naroff887407e2007-12-05 22:21:29 +00001880 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001881 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001882 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001883 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1884 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001885 }