blob: b4f417a15de6e03fe99304170141c440dfaf27e8 [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
Chris Lattner891dca62008-12-08 21:53:24 +000023/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000024/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000026/// [OBJC] objc-class-declaration
27/// [OBJC] objc-alias-declaration
28/// [OBJC] objc-protocol-definition
29/// [OBJC] objc-method-definition
30/// [OBJC] '@' 'end'
Chris Lattnerb28317a2009-03-28 19:18:32 +000031Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
33
Steve Naroff861cf3e2007-08-23 18:16:40 +000034 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000035 case tok::objc_class:
36 return ParseObjCAtClassDeclaration(AtLoc);
37 case tok::objc_interface:
38 return ParseObjCAtInterfaceDeclaration(AtLoc);
39 case tok::objc_protocol:
40 return ParseObjCAtProtocolDeclaration(AtLoc);
41 case tok::objc_implementation:
42 return ParseObjCAtImplementationDeclaration(AtLoc);
43 case tok::objc_end:
44 return ParseObjCAtEndDeclaration(AtLoc);
45 case tok::objc_compatibility_alias:
46 return ParseObjCAtAliasDeclaration(AtLoc);
47 case tok::objc_synthesize:
48 return ParseObjCPropertySynthesize(AtLoc);
49 case tok::objc_dynamic:
50 return ParseObjCPropertyDynamic(AtLoc);
51 default:
52 Diag(AtLoc, diag::err_unexpected_at);
53 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000054 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Chris Lattnerb28317a2009-03-28 19:18:32 +000062Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ConsumeToken(); // the identifier "class"
64 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
65
66 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000067 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 Diag(Tok, diag::err_expected_ident);
69 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000070 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
Reid Spencer5f016e22007-07-11 17:01:13 +000072 ClassNames.push_back(Tok.getIdentifierInfo());
73 ConsumeToken();
74
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000076 break;
77
78 ConsumeToken();
79 }
80
81 // Consume the ';'.
82 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000083 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000084
Steve Naroffe440eb82007-10-10 17:32:04 +000085 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000086 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Steve Naroffdac269b2007-08-20 21:31:48 +000089///
90/// objc-interface:
91/// objc-class-interface-attributes[opt] objc-class-interface
92/// objc-category-interface
93///
94/// objc-class-interface:
95/// '@' 'interface' identifier objc-superclass[opt]
96/// objc-protocol-refs[opt]
97/// objc-class-instance-variables[opt]
98/// objc-interface-decl-list
99/// @end
100///
101/// objc-category-interface:
102/// '@' 'interface' identifier '(' identifier[opt] ')'
103/// objc-protocol-refs[opt]
104/// objc-interface-decl-list
105/// @end
106///
107/// objc-superclass:
108/// ':' identifier
109///
110/// objc-class-interface-attributes:
111/// __attribute__((visibility("default")))
112/// __attribute__((visibility("hidden")))
113/// __attribute__((deprecated))
114/// __attribute__((unavailable))
115/// __attribute__((objc_exception)) - used by NSException on 64-bit
116///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000117Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000118 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000119 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000120 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121 ConsumeToken(); // the "interface" identifier
122
Chris Lattnerdf195262007-10-09 17:51:17 +0000123 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000124 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000125 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000126 }
127 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
130
Chris Lattnerdf195262007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000147 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000148 }
149 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000150
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000152 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000153 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000156 return DeclPtrTy();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000157
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 if (attrList) // categories don't support attributes.
159 Diag(Tok, diag::err_objc_no_attributes_on_category);
160
Chris Lattnerb28317a2009-03-28 19:18:32 +0000161 DeclPtrTy CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000162 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000163 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000164 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000165
166 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000167 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 // Parse a class interface.
170 IdentifierInfo *superClassId = 0;
171 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000172
Chris Lattnerdf195262007-10-09 17:51:17 +0000173 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000177 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000183 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Chris Lattner06036d32008-07-26 04:13:19 +0000184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 return DeclPtrTy();
Chris Lattner06036d32008-07-26 04:13:19 +0000188
Chris Lattnerb28317a2009-03-28 19:18:32 +0000189 DeclPtrTy ClsType =
Chris Lattner06036d32008-07-26 04:13:19 +0000190 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
191 superClassId, superClassLoc,
192 &ProtocolRefs[0], ProtocolRefs.size(),
193 EndProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000194
Chris Lattnerdf195262007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000197
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000199 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000200}
201
202/// objc-interface-decl-list:
203/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000204/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000205/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000206/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000207/// objc-interface-decl-list declaration
208/// objc-interface-decl-list ';'
209///
Steve Naroff294494e2007-08-22 16:35:03 +0000210/// objc-method-requirement: [OBJC2]
211/// @required
212/// @optional
213///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000214void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000215 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 llvm::SmallVector<DeclPtrTy, 32> allMethods;
217 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000218 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000219 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000220
Chris Lattnerbc662af2008-10-20 06:10:06 +0000221 SourceLocation AtEndLoc;
222
Steve Naroff294494e2007-08-22 16:35:03 +0000223 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000224 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000225 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000226 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000227 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000228 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000229 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
230 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000231 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
232 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000233 continue;
234 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000235
Chris Lattnere82a10f2008-10-20 05:46:22 +0000236 // Ignore excess semicolons.
237 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000238 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000239 continue;
240 }
241
Chris Lattnerbc662af2008-10-20 06:10:06 +0000242 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000243 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000244 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000245
246 // If we don't have an @ directive, parse it as a function definition.
247 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000248 // The code below does not consume '}'s because it is afraid of eating the
249 // end of a namespace. Because of the way this code is structured, an
250 // erroneous r_brace would cause an infinite loop if not handled here.
251 if (Tok.is(tok::r_brace))
252 break;
253
Steve Naroff4985ace2007-08-22 18:35:33 +0000254 // FIXME: as the name implies, this rule allows function definitions.
255 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000256 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000257 continue;
258 }
259
260 // Otherwise, we have an @ directive, eat the @.
261 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000262 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000263
Chris Lattnera2449b22008-10-20 05:57:40 +0000264 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000265 AtEndLoc = AtLoc;
266 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000267 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000268
Chris Lattnerbc662af2008-10-20 06:10:06 +0000269 // Eat the identifier.
270 ConsumeToken();
271
Chris Lattnera2449b22008-10-20 05:57:40 +0000272 switch (DirectiveKind) {
273 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000274 // FIXME: If someone forgets an @end on a protocol, this loop will
275 // continue to eat up tons of stuff and spew lots of nonsense errors. It
276 // would probably be better to bail out if we saw an @class or @interface
277 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000278 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000279 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000280 SkipUntil(tok::r_brace, tok::at);
281 break;
282
283 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000284 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000285 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000286 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000287 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000288 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000289 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000290 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 break;
292
293 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000294 if (!getLang().ObjC2)
295 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
296
Chris Lattnere82a10f2008-10-20 05:46:22 +0000297 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000298 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000299 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000300 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000301
Chris Lattnere82a10f2008-10-20 05:46:22 +0000302 // Parse all the comma separated declarators.
303 DeclSpec DS;
304 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
305 ParseStructDeclaration(DS, FieldDeclarators);
306
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000307 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
308 tok::at);
309
Chris Lattnere82a10f2008-10-20 05:46:22 +0000310 // Convert them all to property declarations.
311 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
312 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000313 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000314 Diag(AtLoc, diag::err_objc_property_requires_field_name)
315 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000316 continue;
317 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000318 if (FD.BitfieldSize) {
319 Diag(AtLoc, diag::err_objc_property_bitfield)
320 << FD.D.getSourceRange();
321 continue;
322 }
Chris Lattnerda3253d2008-10-20 06:33:53 +0000323
Chris Lattnere82a10f2008-10-20 05:46:22 +0000324 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000325 IdentifierInfo *SelName =
326 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
327
Chris Lattnere82a10f2008-10-20 05:46:22 +0000328 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000329 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000330 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahanian2e050f12009-03-12 22:34:11 +0000331 Selector SetterSel;
332 if (SetterName)
333 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
334 else
335 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
336 PP.getSelectorTable(),
337 FD.D.getIdentifier());
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000338 bool isOverridingProperty = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000339 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
340 GetterSel, SetterSel,
341 interfaceDecl,
342 &isOverridingProperty,
343 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000344 if (!isOverridingProperty)
345 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000346 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000347 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000348 }
Steve Naroff294494e2007-08-22 16:35:03 +0000349 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000350
351 // We break out of the big loop in two cases: when we see @end or when we see
352 // EOF. In the former case, eat the @end. In the later case, emit an error.
353 if (Tok.isObjCAtKeyword(tok::objc_end))
354 ConsumeToken(); // the "end" identifier
355 else
356 Diag(Tok, diag::err_objc_missing_end);
357
Chris Lattnera2449b22008-10-20 05:57:40 +0000358 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000359 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000360 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Chris Lattner682bf922009-03-29 16:50:03 +0000361 &allMethods[0], allMethods.size(),
362 &allProperties[0], allProperties.size(),
363 &allTUVariables[0], allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000364}
365
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000366/// Parse property attribute declarations.
367///
368/// property-attr-decl: '(' property-attrlist ')'
369/// property-attrlist:
370/// property-attribute
371/// property-attrlist ',' property-attribute
372/// property-attribute:
373/// getter '=' identifier
374/// setter '=' identifier ':'
375/// readonly
376/// readwrite
377/// assign
378/// retain
379/// copy
380/// nonatomic
381///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000382void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000383 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000384 SourceLocation LHSLoc = ConsumeParen(); // consume '('
385
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000386 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000387 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000388
389 // If this is not an identifier at all, bail out early.
390 if (II == 0) {
391 MatchRHSPunctuation(tok::r_paren, LHSLoc);
392 return;
393 }
394
Chris Lattner156b0612008-10-20 07:37:22 +0000395 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
396
Chris Lattner92e62b02008-11-20 04:42:34 +0000397 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000398 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000399 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000400 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000401 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000403 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000404 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000405 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000406 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000407 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000409 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000410 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000411 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
412 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000413 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000414
Chris Lattner8ca329c2008-10-20 07:24:39 +0000415 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000416 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000417 SkipUntil(tok::r_paren);
418 return;
419 }
420
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000421 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000422 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
423 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000424 ConsumeToken(); // consume method name
425
426 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
427 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000428 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000429 } else {
430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
431 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000432 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000433 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000434 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000435 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000436 SkipUntil(tok::r_paren);
437 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000438 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000439
Chris Lattner156b0612008-10-20 07:37:22 +0000440 if (Tok.isNot(tok::comma))
441 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000442
Chris Lattner156b0612008-10-20 07:37:22 +0000443 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000444 }
Chris Lattner156b0612008-10-20 07:37:22 +0000445
446 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000447}
448
Steve Naroff3536b442007-09-06 21:24:23 +0000449/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000450/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000451/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000452///
453/// objc-instance-method: '-'
454/// objc-class-method: '+'
455///
Steve Naroff4985ace2007-08-22 18:35:33 +0000456/// objc-method-attributes: [OBJC2]
457/// __attribute__((deprecated))
458///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000459Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
460 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000461 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000462
463 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000464 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000465
Chris Lattnerb28317a2009-03-28 19:18:32 +0000466 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000467 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000468 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000469 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000470}
471
472/// objc-selector:
473/// identifier
474/// one of
475/// enum struct union if else while do for switch case default
476/// break continue return goto asm sizeof typeof __alignof
477/// unsigned long const short volatile signed restrict _Complex
478/// in out inout bycopy byref oneway int char float double void _Bool
479///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000480IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000481 switch (Tok.getKind()) {
482 default:
483 return 0;
484 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000485 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000486 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000487 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000488 case tok::kw_break:
489 case tok::kw_case:
490 case tok::kw_catch:
491 case tok::kw_char:
492 case tok::kw_class:
493 case tok::kw_const:
494 case tok::kw_const_cast:
495 case tok::kw_continue:
496 case tok::kw_default:
497 case tok::kw_delete:
498 case tok::kw_do:
499 case tok::kw_double:
500 case tok::kw_dynamic_cast:
501 case tok::kw_else:
502 case tok::kw_enum:
503 case tok::kw_explicit:
504 case tok::kw_export:
505 case tok::kw_extern:
506 case tok::kw_false:
507 case tok::kw_float:
508 case tok::kw_for:
509 case tok::kw_friend:
510 case tok::kw_goto:
511 case tok::kw_if:
512 case tok::kw_inline:
513 case tok::kw_int:
514 case tok::kw_long:
515 case tok::kw_mutable:
516 case tok::kw_namespace:
517 case tok::kw_new:
518 case tok::kw_operator:
519 case tok::kw_private:
520 case tok::kw_protected:
521 case tok::kw_public:
522 case tok::kw_register:
523 case tok::kw_reinterpret_cast:
524 case tok::kw_restrict:
525 case tok::kw_return:
526 case tok::kw_short:
527 case tok::kw_signed:
528 case tok::kw_sizeof:
529 case tok::kw_static:
530 case tok::kw_static_cast:
531 case tok::kw_struct:
532 case tok::kw_switch:
533 case tok::kw_template:
534 case tok::kw_this:
535 case tok::kw_throw:
536 case tok::kw_true:
537 case tok::kw_try:
538 case tok::kw_typedef:
539 case tok::kw_typeid:
540 case tok::kw_typename:
541 case tok::kw_typeof:
542 case tok::kw_union:
543 case tok::kw_unsigned:
544 case tok::kw_using:
545 case tok::kw_virtual:
546 case tok::kw_void:
547 case tok::kw_volatile:
548 case tok::kw_wchar_t:
549 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000550 case tok::kw__Bool:
551 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000552 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000553 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000554 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000555 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000556 }
Steve Naroff294494e2007-08-22 16:35:03 +0000557}
558
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000559/// objc-for-collection-in: 'in'
560///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000561bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000562 // FIXME: May have to do additional look-ahead to only allow for
563 // valid tokens following an 'in'; such as an identifier, unary operators,
564 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000565 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000566 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000567}
568
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000570/// qualifier list and builds their bitmask representation in the input
571/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000572///
573/// objc-type-qualifiers:
574/// objc-type-qualifier
575/// objc-type-qualifiers objc-type-qualifier
576///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000577void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000578 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000579 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000580 return;
581
582 const IdentifierInfo *II = Tok.getIdentifierInfo();
583 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000585 continue;
586
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000588 switch (i) {
589 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000590 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
591 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
592 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
593 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
594 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
595 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000596 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000598 ConsumeToken();
599 II = 0;
600 break;
601 }
602
603 // If this wasn't a recognized qualifier, bail out.
604 if (II) return;
605 }
606}
607
608/// objc-type-name:
609/// '(' objc-type-qualifiers[opt] type-name ')'
610/// '(' objc-type-qualifiers[opt] ')'
611///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000613 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000614
Chris Lattner4a76b292008-10-22 03:52:06 +0000615 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000616 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000617
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000618 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000620
Chris Lattner4a76b292008-10-22 03:52:06 +0000621 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000622 if (isTypeSpecifierQualifier()) {
623 TypeResult TypeSpec = ParseTypeName();
624 if (!TypeSpec.isInvalid())
625 Ty = TypeSpec.get();
626 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000627
Steve Naroffd7333c22008-10-21 14:15:04 +0000628 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000629 ConsumeParen();
630 else if (Tok.getLocation() == TypeStartLoc) {
631 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000632 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000633 SkipUntil(tok::r_paren);
634 } else {
635 // Otherwise, we found *something*, but didn't get a ')' in the right
636 // place. Emit an error then return what we have as the type.
637 MatchRHSPunctuation(tok::r_paren, LParenLoc);
638 }
Steve Narofff28b2642007-09-05 23:30:30 +0000639 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000640}
641
642/// objc-method-decl:
643/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000644/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000645/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000646/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000647///
648/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000649/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000650/// objc-keyword-selector objc-keyword-decl
651///
652/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000653/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
654/// objc-selector ':' objc-keyword-attributes[opt] identifier
655/// ':' objc-type-name objc-keyword-attributes[opt] identifier
656/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000657///
Steve Naroff4985ace2007-08-22 18:35:33 +0000658/// objc-parmlist:
659/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000660///
Steve Naroff4985ace2007-08-22 18:35:33 +0000661/// objc-parms:
662/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000663///
Steve Naroff4985ace2007-08-22 18:35:33 +0000664/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000665/// , ...
666///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000667/// objc-keyword-attributes: [OBJC2]
668/// __attribute__((unused))
669///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000670Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
671 tok::TokenKind mType,
672 DeclPtrTy IDecl,
673 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnere8904e92008-08-23 01:48:03 +0000674 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000675 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000677 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000678 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000679
Steve Naroffbef11852007-10-26 20:53:56 +0000680 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000681 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000682
Steve Naroff84c43102009-02-11 20:43:13 +0000683 // An unnamed colon is valid.
684 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000685 Diag(Tok, diag::err_expected_selector_for_method)
686 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000687 // Skip until we get a ; or {}.
688 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000689 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000690 }
691
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000692 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000693 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000694 // If attributes exist after the method, parse them.
695 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000696 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000697 MethodAttrs = ParseAttributes();
698
699 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000700 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000701 mType, IDecl, DSRet, ReturnType, Sel,
Chris Lattnere294d3f2009-04-11 18:57:04 +0000702 0, CargNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000703 MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000704 }
Steve Narofff28b2642007-09-05 23:30:30 +0000705
Steve Naroff68d331a2007-09-27 14:38:14 +0000706 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000707 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Chris Lattnerff384912007-10-07 02:00:24 +0000708
Chris Lattnerff384912007-10-07 02:00:24 +0000709 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000710 Action::ObjCArgInfo ArgInfo;
Steve Naroff68d331a2007-09-27 14:38:14 +0000711
Chris Lattnerff384912007-10-07 02:00:24 +0000712 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000713 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000714 Diag(Tok, diag::err_expected_colon);
715 break;
716 }
717 ConsumeToken(); // Eat the ':'.
Steve Narofff28b2642007-09-05 23:30:30 +0000718
Chris Lattnere294d3f2009-04-11 18:57:04 +0000719 ArgInfo.Type = 0;
720 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
721 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
722
Chris Lattnerff384912007-10-07 02:00:24 +0000723 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000724 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000725 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000726 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000727
Chris Lattnerdf195262007-10-09 17:51:17 +0000728 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000729 Diag(Tok, diag::err_expected_ident); // missing argument name.
730 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000731 }
Chris Lattnere294d3f2009-04-11 18:57:04 +0000732
733 ArgInfo.Name = Tok.getIdentifierInfo();
734 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000735 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000736
Chris Lattnere294d3f2009-04-11 18:57:04 +0000737 ArgInfos.push_back(ArgInfo);
738 KeyIdents.push_back(SelIdent);
739
Chris Lattnerff384912007-10-07 02:00:24 +0000740 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000741 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000742 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000743 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000744 break;
745 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000746 }
Chris Lattnerff384912007-10-07 02:00:24 +0000747
Steve Naroff335eafa2007-11-15 12:35:21 +0000748 bool isVariadic = false;
749
Chris Lattnerff384912007-10-07 02:00:24 +0000750 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000751 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000752 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000753 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000754 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000755 ConsumeToken();
756 break;
757 }
Chris Lattnerff384912007-10-07 02:00:24 +0000758 DeclSpec DS;
759 ParseDeclarationSpecifiers(DS);
760 // Parse the declarator.
761 Declarator ParmDecl(DS, Declarator::PrototypeContext);
762 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000763 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000764 }
765
766 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000767 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000768 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000769 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000770 MethodAttrs = ParseAttributes();
771
772 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
773 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000774 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000775 mType, IDecl, DSRet, ReturnType, Sel,
Chris Lattnere294d3f2009-04-11 18:57:04 +0000776 &ArgInfos[0], CargNames,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000777 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000778 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000779}
780
Steve Naroffdac269b2007-08-20 21:31:48 +0000781/// objc-protocol-refs:
782/// '<' identifier-list '>'
783///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000784bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000785ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Chris Lattnere13b9592008-07-26 04:03:38 +0000786 bool WarnOnDeclarations, SourceLocation &EndLoc) {
787 assert(Tok.is(tok::less) && "expected <");
788
789 ConsumeToken(); // the "<"
790
791 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
792
793 while (1) {
794 if (Tok.isNot(tok::identifier)) {
795 Diag(Tok, diag::err_expected_ident);
796 SkipUntil(tok::greater);
797 return true;
798 }
799 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
800 Tok.getLocation()));
801 ConsumeToken();
802
803 if (Tok.isNot(tok::comma))
804 break;
805 ConsumeToken();
806 }
807
808 // Consume the '>'.
809 if (Tok.isNot(tok::greater)) {
810 Diag(Tok, diag::err_expected_greater);
811 return true;
812 }
813
814 EndLoc = ConsumeAnyToken();
815
816 // Convert the list of protocols identifiers into a list of protocol decls.
817 Actions.FindProtocolDeclaration(WarnOnDeclarations,
818 &ProtocolIdents[0], ProtocolIdents.size(),
819 Protocols);
820 return false;
821}
822
Steve Naroffdac269b2007-08-20 21:31:48 +0000823/// objc-class-instance-variables:
824/// '{' objc-instance-variable-decl-list[opt] '}'
825///
826/// objc-instance-variable-decl-list:
827/// objc-visibility-spec
828/// objc-instance-variable-decl ';'
829/// ';'
830/// objc-instance-variable-decl-list objc-visibility-spec
831/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
832/// objc-instance-variable-decl-list ';'
833///
834/// objc-visibility-spec:
835/// @private
836/// @protected
837/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000838/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000839///
840/// objc-instance-variable-decl:
841/// struct-declaration
842///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000843void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000844 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000845 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000846 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000847 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
848
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000849 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000850
Steve Naroffddbff782007-08-21 21:17:12 +0000851 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000852
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000853 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000854 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000855 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000856 // Each iteration of this loop reads one objc-instance-variable-decl.
857
858 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000859 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000860 Diag(Tok, diag::ext_extra_struct_semi);
861 ConsumeToken();
862 continue;
863 }
Chris Lattnere1359422008-04-10 06:46:29 +0000864
Steve Naroffddbff782007-08-21 21:17:12 +0000865 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000866 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000867 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000868 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000869 case tok::objc_private:
870 case tok::objc_public:
871 case tok::objc_protected:
872 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000873 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000874 ConsumeToken();
875 continue;
876 default:
877 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000878 continue;
879 }
880 }
Chris Lattnere1359422008-04-10 06:46:29 +0000881
882 // Parse all the comma separated declarators.
883 DeclSpec DS;
884 FieldDeclarators.clear();
885 ParseStructDeclaration(DS, FieldDeclarators);
886
887 // Convert them all to fields.
888 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
889 FieldDeclarator &FD = FieldDeclarators[i];
890 // Install the declarator into interfaceDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000891 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
892 DS.getSourceRange().getBegin(),
893 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000894 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000895 }
Steve Naroff3536b442007-09-06 21:24:23 +0000896
Chris Lattnerdf195262007-10-09 17:51:17 +0000897 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000898 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000899 } else {
900 Diag(Tok, diag::err_expected_semi_decl_list);
901 // Skip to end of block or statement
902 SkipUntil(tok::r_brace, true, true);
903 }
904 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000905 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000906 // Call ActOnFields() even if we don't have any decls. This is useful
907 // for code rewriting tools that need to be aware of the empty list.
908 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
909 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000910 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000911 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000912}
Steve Naroffdac269b2007-08-20 21:31:48 +0000913
914/// objc-protocol-declaration:
915/// objc-protocol-definition
916/// objc-protocol-forward-reference
917///
918/// objc-protocol-definition:
919/// @protocol identifier
920/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000921/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000922/// @end
923///
924/// objc-protocol-forward-reference:
925/// @protocol identifier-list ';'
926///
927/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000928/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000929/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000930Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
931 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000932 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000933 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
934 ConsumeToken(); // the "protocol" identifier
935
Chris Lattnerdf195262007-10-09 17:51:17 +0000936 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000937 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000938 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000939 }
940 // Save the protocol name, then consume it.
941 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
942 SourceLocation nameLoc = ConsumeToken();
943
Chris Lattnerdf195262007-10-09 17:51:17 +0000944 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000945 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000946 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000947 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
948 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000949 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000950
Chris Lattnerdf195262007-10-09 17:51:17 +0000951 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000952 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
953 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
954
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000955 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000956 while (1) {
957 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000958 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000959 Diag(Tok, diag::err_expected_ident);
960 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000961 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000962 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000963 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
964 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000965 ConsumeToken(); // the identifier
966
Chris Lattnerdf195262007-10-09 17:51:17 +0000967 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000968 break;
969 }
970 // Consume the ';'.
971 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000972 return DeclPtrTy();
Chris Lattner7caeabd2008-07-21 22:17:28 +0000973
Steve Naroffe440eb82007-10-10 17:32:04 +0000974 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000975 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000976 ProtocolRefs.size(),
977 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000978 }
979
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000980 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000981 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000982
Chris Lattnerb28317a2009-03-28 19:18:32 +0000983 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000984 if (Tok.is(tok::less) &&
Chris Lattner58fe03b2009-04-12 08:43:13 +0000985 ParseObjCProtocolReferences(ProtocolRefs, false, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000986 return DeclPtrTy();
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000987
Chris Lattnerb28317a2009-03-28 19:18:32 +0000988 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +0000989 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
990 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000991 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000992 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000993 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000994}
Steve Naroffdac269b2007-08-20 21:31:48 +0000995
996/// objc-implementation:
997/// objc-class-implementation-prologue
998/// objc-category-implementation-prologue
999///
1000/// objc-class-implementation-prologue:
1001/// @implementation identifier objc-superclass[opt]
1002/// objc-class-instance-variables[opt]
1003///
1004/// objc-category-implementation-prologue:
1005/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001006Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001007 SourceLocation atLoc) {
1008 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1009 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1010 ConsumeToken(); // the "implementation" identifier
1011
Chris Lattnerdf195262007-10-09 17:51:17 +00001012 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001013 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001014 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001015 }
1016 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001017 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001018 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1019
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021 // we have a category implementation.
1022 SourceLocation lparenLoc = ConsumeParen();
1023 SourceLocation categoryLoc, rparenLoc;
1024 IdentifierInfo *categoryId = 0;
1025
Chris Lattnerdf195262007-10-09 17:51:17 +00001026 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001027 categoryId = Tok.getIdentifierInfo();
1028 categoryLoc = ConsumeToken();
1029 } else {
1030 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001031 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001032 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001033 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001034 Diag(Tok, diag::err_expected_rparen);
1035 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001036 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001037 }
1038 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001039 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001040 atLoc, nameId, nameLoc, categoryId,
1041 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001042 ObjCImpDecl = ImplCatType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001043 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001044 }
1045 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001046 SourceLocation superClassLoc;
1047 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001048 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001049 // We have a super class
1050 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001051 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001052 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001053 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001054 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001055 superClassId = Tok.getIdentifierInfo();
1056 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001057 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001058 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001059 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001060 superClassId, superClassLoc);
1061
Steve Naroff60fccee2007-10-29 21:38:07 +00001062 if (Tok.is(tok::l_brace)) // we have ivars
1063 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001065
Chris Lattnerb28317a2009-03-28 19:18:32 +00001066 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001067}
Steve Naroff60fccee2007-10-29 21:38:07 +00001068
Chris Lattnerb28317a2009-03-28 19:18:32 +00001069Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001070 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1071 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001072 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001073 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001074 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001075 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001076 ObjCImpDecl = DeclPtrTy();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001077 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001078 else
1079 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001080 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001081}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001082
1083/// compatibility-alias-decl:
1084/// @compatibility_alias alias-name class-name ';'
1085///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001086Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001087 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1088 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1089 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001091 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001092 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001093 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001094 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1095 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001097 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001098 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001099 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001100 IdentifierInfo *classId = Tok.getIdentifierInfo();
1101 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1102 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001103 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001104 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001105 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001106 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1107 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001108}
1109
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001110/// property-synthesis:
1111/// @synthesize property-ivar-list ';'
1112///
1113/// property-ivar-list:
1114/// property-ivar
1115/// property-ivar-list ',' property-ivar
1116///
1117/// property-ivar:
1118/// identifier
1119/// identifier '=' identifier
1120///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001121Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001122 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1123 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001124 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001125 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001126 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001127 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001128 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001129
Chris Lattnerdf195262007-10-09 17:51:17 +00001130 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001131 IdentifierInfo *propertyIvar = 0;
1132 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1133 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001134 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001135 // property '=' ivar-name
1136 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001137 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001138 Diag(Tok, diag::err_expected_ident);
1139 break;
1140 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001141 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001142 ConsumeToken(); // consume ivar-name
1143 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001144 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1145 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001146 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001147 break;
1148 ConsumeToken(); // consume ','
1149 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001150 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001151 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001152 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001153}
1154
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001155/// property-dynamic:
1156/// @dynamic property-list
1157///
1158/// property-list:
1159/// identifier
1160/// property-list ',' identifier
1161///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001162Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001163 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1164 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1165 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001166 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001167 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001168 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001170 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001171 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1172 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1173 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1174 propertyId, 0);
1175
Chris Lattnerdf195262007-10-09 17:51:17 +00001176 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001177 break;
1178 ConsumeToken(); // consume ','
1179 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001180 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001181 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001182 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001183}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001184
1185/// objc-throw-statement:
1186/// throw expression[opt];
1187///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001188Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001189 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001190 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001191 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001192 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001193 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001194 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001195 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001196 }
1197 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001198 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001199 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001200}
1201
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001202/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001203/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001204///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001205Parser::OwningStmtResult
1206Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001207 ConsumeToken(); // consume synchronized
1208 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001209 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001210 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001211 }
1212 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001213 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001214 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001215 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001216 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001217 }
1218 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001219 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001220 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001221 }
1222 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001223 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001224 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001225 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001226 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001227 // Enter a scope to hold everything within the compound stmt. Compound
1228 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001229 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001230
Sebastian Redl61364dd2008-12-11 19:30:53 +00001231 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001232
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001233 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001234 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001235 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001236 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001237}
1238
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001239/// objc-try-catch-statement:
1240/// @try compound-statement objc-catch-list[opt]
1241/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1242///
1243/// objc-catch-list:
1244/// @catch ( parameter-declaration ) compound-statement
1245/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1246/// catch-parameter-declaration:
1247/// parameter-declaration
1248/// '...' [OBJC2]
1249///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001250Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001251 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001252
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001253 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001255 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001256 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001257 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001258 OwningStmtResult CatchStmts(Actions);
1259 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001260 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001261 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001262 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001263 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001264 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001265
Chris Lattnerdf195262007-10-09 17:51:17 +00001266 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001267 // At this point, we need to lookahead to determine if this @ is the start
1268 // of an @catch or @finally. We don't want to consume the @ token if this
1269 // is an @try or @encode or something else.
1270 Token AfterAt = GetLookAheadToken(1);
1271 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1272 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1273 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001274
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001275 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001276 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001277 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001278 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001279 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001280 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001281 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001282 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001283 DeclSpec DS;
1284 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001285 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001286 // optional. As a result, we need to use "PrototypeContext", because
1287 // we must accept either 'declarator' or 'abstract-declarator' here.
1288 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1289 ParseDeclarator(ParmDecl);
1290
1291 // Inform the actions module about the parameter declarator, so it
1292 // gets added to the current scope.
1293 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001294 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001295 ConsumeToken(); // consume '...'
Steve Naroff93a25952009-04-07 22:56:58 +00001296
1297 SourceLocation RParenLoc;
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001298
Steve Naroff93a25952009-04-07 22:56:58 +00001299 if (Tok.is(tok::r_paren))
1300 RParenLoc = ConsumeParen();
1301 else // Skip over garbage, until we get to ')'. Eat the ')'.
1302 SkipUntil(tok::r_paren, true, false);
1303
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001304 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001305 if (Tok.is(tok::l_brace))
1306 CatchBody = ParseCompoundStatementBody();
1307 else
1308 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001309 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001310 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001312 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001313 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001314 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001315 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1316 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001317 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001318 }
1319 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001320 } else {
1321 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001322 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001323 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001324
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001325 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001326 if (Tok.is(tok::l_brace))
1327 FinallyBody = ParseCompoundStatementBody();
1328 else
1329 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001330 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001331 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001332 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001333 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334 catch_or_finally_seen = true;
1335 break;
1336 }
1337 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001338 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001339 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001340 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001341 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001342 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1343 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001344}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001345
Steve Naroff3536b442007-09-06 21:24:23 +00001346/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001348Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1349 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001350
Chris Lattner49f28ca2009-03-05 08:00:35 +00001351 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1352 PP.getSourceManager(),
1353 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001354
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001356 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001357 ConsumeToken();
1358
Steve Naroff409be832007-11-11 19:54:21 +00001359 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001360 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001361 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001362
1363 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1364 SkipUntil(tok::l_brace, true, true);
1365
1366 // If we didn't find the '{', bail out.
1367 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001368 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001369 }
Steve Naroff409be832007-11-11 19:54:21 +00001370 SourceLocation BraceLoc = Tok.getLocation();
1371
1372 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001373 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001374
1375 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001376 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001377 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001378
1379 OwningStmtResult FnBody(ParseCompoundStatementBody());
1380
Steve Naroff409be832007-11-11 19:54:21 +00001381 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001382 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001383 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1384 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001385
Steve Naroff32ce8372009-03-02 22:00:56 +00001386 // TODO: Pass argument information.
1387 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1388
Steve Naroff409be832007-11-11 19:54:21 +00001389 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001390 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001391
Steve Naroff71c0a952007-11-13 23:01:27 +00001392 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001393}
Anders Carlsson55085182007-08-21 17:43:55 +00001394
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001395Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001396 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001397 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001398 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1399 return ParseObjCThrowStmt(AtLoc);
1400 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1401 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001402 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001403 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001404 // If the expression is invalid, skip ahead to the next semicolon. Not
1405 // doing this opens us up to the possibility of infinite loops if
1406 // ParseExpression does not consume any tokens.
1407 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001408 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001409 }
1410 // Otherwise, eat the semicolon.
1411 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001412 return Actions.ActOnExprStmt(move(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001413}
1414
Sebastian Redl1d922962008-12-13 15:32:12 +00001415Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001416 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001417 case tok::string_literal: // primary-expression: string-literal
1418 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001419 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001420 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001421 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001422 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001423
Chris Lattner4fef81d2008-08-05 06:19:09 +00001424 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1425 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001426 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001427 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001428 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001429 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001430 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001431 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001432 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001433 }
Anders Carlsson55085182007-08-21 17:43:55 +00001434 }
Anders Carlsson55085182007-08-21 17:43:55 +00001435}
1436
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001437/// objc-message-expr:
1438/// '[' objc-receiver objc-message-args ']'
1439///
1440/// objc-receiver:
1441/// expression
1442/// class-name
1443/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001444Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001445 assert(Tok.is(tok::l_square) && "'[' expected");
1446 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1447
1448 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001449 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001450 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001451 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1452 SourceLocation NameLoc = ConsumeToken();
1453 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1454 ExprArg(Actions));
1455 }
Chris Lattner699b6612008-01-25 18:59:06 +00001456 }
1457
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001458 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001459 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001460 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001461 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001462 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001463
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001464 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001465 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001466}
Sebastian Redl1d922962008-12-13 15:32:12 +00001467
Chris Lattner699b6612008-01-25 18:59:06 +00001468/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1469/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001470///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001471/// objc-message-args:
1472/// objc-selector
1473/// objc-keywordarg-list
1474///
1475/// objc-keywordarg-list:
1476/// objc-keywordarg
1477/// objc-keywordarg-list objc-keywordarg
1478///
1479/// objc-keywordarg:
1480/// selector-name[opt] ':' objc-keywordexpr
1481///
1482/// objc-keywordexpr:
1483/// nonempty-expr-list
1484///
1485/// nonempty-expr-list:
1486/// assignment-expression
1487/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001488///
1489Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001490Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001491 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001492 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001493 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001494 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001495 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001496 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001497
Anders Carlssonff975cf2009-02-14 18:21:46 +00001498 SourceLocation SelectorLoc = Loc;
1499
Steve Naroff68d331a2007-09-27 14:38:14 +00001500 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001501 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001502
Chris Lattnerdf195262007-10-09 17:51:17 +00001503 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001504 while (1) {
1505 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001506 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001507
Chris Lattnerdf195262007-10-09 17:51:17 +00001508 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001509 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001510 // We must manually skip to a ']', otherwise the expression skipper will
1511 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1512 // the enclosing expression.
1513 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001514 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001515 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001516
Steve Naroff68d331a2007-09-27 14:38:14 +00001517 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001518 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001519 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001520 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001521 // We must manually skip to a ']', otherwise the expression skipper will
1522 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1523 // the enclosing expression.
1524 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001525 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001526 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001527
Steve Naroff37387c92007-09-17 20:25:27 +00001528 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001529 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001530
Steve Naroff37387c92007-09-17 20:25:27 +00001531 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001532 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001533 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001534 break;
1535 // We have a selector or a colon, continue parsing.
1536 }
1537 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001538 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001539 ConsumeToken(); // Eat the ','.
1540 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001541 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001542 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001543 // We must manually skip to a ']', otherwise the expression skipper will
1544 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1545 // the enclosing expression.
1546 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001547 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001548 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001549
Steve Naroff49f109c2007-11-15 13:05:42 +00001550 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001551 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001552 }
1553 } else if (!selIdent) {
1554 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001555
Chris Lattner4fef81d2008-08-05 06:19:09 +00001556 // We must manually skip to a ']', otherwise the expression skipper will
1557 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1558 // the enclosing expression.
1559 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001560 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001561 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001562
Chris Lattnerdf195262007-10-09 17:51:17 +00001563 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001564 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001565 // We must manually skip to a ']', otherwise the expression skipper will
1566 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1567 // the enclosing expression.
1568 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001569 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001570 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001571
Chris Lattner699b6612008-01-25 18:59:06 +00001572 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001573
Steve Naroff29238a02007-10-05 18:42:47 +00001574 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001575 if (nKeys == 0)
1576 KeyIdents.push_back(selIdent);
1577 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001578
Chris Lattnerff384912007-10-07 02:00:24 +00001579 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001580 if (ReceiverName)
1581 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001582 LBracLoc, NameLoc, SelectorLoc,
1583 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001584 KeyExprs.take(), KeyExprs.size()));
1585 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001586 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001587 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001588}
1589
Sebastian Redl1d922962008-12-13 15:32:12 +00001590Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001591 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001592 if (Res.isInvalid()) return move(Res);
1593
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001594 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1595 // expressions. At this point, we know that the only valid thing that starts
1596 // with '@' is an @"".
1597 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001598 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001599 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001600 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001601
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001602 while (Tok.is(tok::at)) {
1603 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001604
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001605 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001606 if (!isTokenStringLiteral())
1607 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001608
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001609 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001610 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001611 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001612
Sebastian Redleffa8d12008-12-10 00:02:53 +00001613 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001614 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001615
1616 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1617 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001618}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001619
1620/// objc-encode-expression:
1621/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001622Parser::OwningExprResult
1623Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001624 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001625
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001626 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001627
Chris Lattner4fef81d2008-08-05 06:19:09 +00001628 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001629 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1630
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001631 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001632
Douglas Gregor809070a2009-02-18 17:45:20 +00001633 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001634
Anders Carlsson4988ae32007-08-23 15:31:37 +00001635 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001636
Douglas Gregor809070a2009-02-18 17:45:20 +00001637 if (Ty.isInvalid())
1638 return ExprError();
1639
1640 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1641 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001642}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001643
1644/// objc-protocol-expression
1645/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001646Parser::OwningExprResult
1647Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001648 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001649
Chris Lattner4fef81d2008-08-05 06:19:09 +00001650 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001651 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1652
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001653 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001654
Chris Lattner4fef81d2008-08-05 06:19:09 +00001655 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001656 return ExprError(Diag(Tok, diag::err_expected_ident));
1657
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001658 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001659 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001660
Anders Carlsson4988ae32007-08-23 15:31:37 +00001661 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001662
Sebastian Redl1d922962008-12-13 15:32:12 +00001663 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1664 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001665}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001666
1667/// objc-selector-expression
1668/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001669Parser::OwningExprResult
1670Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001671 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001672
Chris Lattner4fef81d2008-08-05 06:19:09 +00001673 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001674 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1675
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001676 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001677 SourceLocation LParenLoc = ConsumeParen();
1678 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001679 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001680 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1681 return ExprError(Diag(Tok, diag::err_expected_ident));
1682
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001683 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001684 unsigned nColons = 0;
1685 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001686 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001687 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001688 return ExprError(Diag(Tok, diag::err_expected_colon));
1689
Chris Lattnercb53b362007-12-27 19:57:00 +00001690 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001691 ConsumeToken(); // Eat the ':'.
1692 if (Tok.is(tok::r_paren))
1693 break;
1694 // Check for another keyword selector.
1695 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001696 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001697 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001698 if (!SelIdent && Tok.isNot(tok::colon))
1699 break;
1700 }
Steve Naroff887407e2007-12-05 22:21:29 +00001701 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001702 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001703 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001704 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1705 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001706 }