blob: 571c6108364c973af71ede0032e203f7a6860078 [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
Chris Lattner79cae252008-12-08 21:53:24 +000023/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattner4b009652007-07-25 00:24:17 +000024/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-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 Lattner5261d0c2009-03-28 19:18:32 +000031Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
33
Steve Naroff87c329f2007-08-23 18:16:40 +000034 switch (Tok.getObjCKeywordID()) {
Chris Lattner818350c2008-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 Lattner5261d0c2009-03-28 19:18:32 +000054 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Chris Lattner5261d0c2009-03-28 19:18:32 +000062Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000063 ConsumeToken(); // the identifier "class"
64 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
65
66 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000067 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000068 Diag(Tok, diag::err_expected_ident);
69 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +000070 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +000071 }
Chris Lattner4b009652007-07-25 00:24:17 +000072 ClassNames.push_back(Tok.getIdentifierInfo());
73 ConsumeToken();
74
Chris Lattnera1d2bb72007-10-09 17:51:17 +000075 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000076 break;
77
78 ConsumeToken();
79 }
80
81 // Consume the ';'.
82 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner5261d0c2009-03-28 19:18:32 +000083 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +000084
Steve Naroff415c1832007-10-10 17:32:04 +000085 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000086 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000087}
88
Steve Narofffb367882007-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 Lattner5261d0c2009-03-28 19:18:32 +0000117Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Narofffb367882007-08-20 21:31:48 +0000118 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000119 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000120 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121 ConsumeToken(); // the "interface" identifier
122
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000123 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000124 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000125 return DeclPtrTy();
Steve Narofffb367882007-08-20 21:31:48 +0000126 }
127 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
130
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
135
Steve Naroffa7f62782007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Steve Narofffb367882007-08-20 21:31:48 +0000143 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner5261d0c2009-03-28 19:18:32 +0000147 return DeclPtrTy();
Steve Narofffb367882007-08-20 21:31:48 +0000148 }
149 rparenLoc = ConsumeParen();
Chris Lattner45142b92008-07-26 04:07:02 +0000150
Steve Narofffb367882007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattner45142b92008-07-26 04:07:02 +0000152 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000153 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattner45142b92008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000156 return DeclPtrTy();
Chris Lattner45142b92008-07-26 04:07:02 +0000157
Steve Narofffb367882007-08-20 21:31:48 +0000158 if (attrList) // categories don't support attributes.
159 Diag(Tok, diag::err_objc_no_attributes_on_category);
160
Jay Foad9e6bef42009-05-21 09:52:38 +0000161 DeclPtrTy CategoryType =
162 Actions.ActOnStartCategoryInterface(atLoc,
163 nameId, nameLoc,
164 categoryId, categoryLoc,
165 ProtocolRefs.data(),
166 ProtocolRefs.size(),
167 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000168
169 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnera40577e2008-10-20 06:10:06 +0000170 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000171 }
172 // Parse a class interface.
173 IdentifierInfo *superClassId = 0;
174 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000175
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000176 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000177 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000178 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000179 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000180 return DeclPtrTy();
Steve Narofffb367882007-08-20 21:31:48 +0000181 }
182 superClassId = Tok.getIdentifierInfo();
183 superClassLoc = ConsumeToken();
184 }
185 // Next, we need to check for any protocol references.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000186 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Chris Lattnerae1ae492008-07-26 04:13:19 +0000187 SourceLocation EndProtoLoc;
188 if (Tok.is(tok::less) &&
189 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000190 return DeclPtrTy();
Chris Lattnerae1ae492008-07-26 04:13:19 +0000191
Chris Lattner5261d0c2009-03-28 19:18:32 +0000192 DeclPtrTy ClsType =
Chris Lattnerae1ae492008-07-26 04:13:19 +0000193 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
194 superClassId, superClassLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +0000195 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerae1ae492008-07-26 04:13:19 +0000196 EndProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000197
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000198 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000199 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000200
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000201 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnera40577e2008-10-20 06:10:06 +0000202 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000203}
204
205/// objc-interface-decl-list:
206/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000207/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000208/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000209/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000210/// objc-interface-decl-list declaration
211/// objc-interface-decl-list ';'
212///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000213/// objc-method-requirement: [OBJC2]
214/// @required
215/// @optional
216///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000217void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000218 tok::ObjCKeywordKind contextKey) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000219 llvm::SmallVector<DeclPtrTy, 32> allMethods;
220 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattnera17991f2009-03-29 16:50:03 +0000221 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000222 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000223
Chris Lattnera40577e2008-10-20 06:10:06 +0000224 SourceLocation AtEndLoc;
225
Steve Naroff0bbffd82007-08-22 16:35:03 +0000226 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000227 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000228 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000229 DeclPtrTy methodPrototype =
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000230 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000231 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000232 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
233 // method definitions.
Chris Lattnere50cb752009-02-15 22:24:30 +0000234 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
235 "", tok::semi);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000236 continue;
237 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000238
Chris Lattnere48b46b2008-10-20 05:46:22 +0000239 // Ignore excess semicolons.
240 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000241 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000242 continue;
243 }
244
Chris Lattnera40577e2008-10-20 06:10:06 +0000245 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000246 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000247 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000248
249 // If we don't have an @ directive, parse it as a function definition.
250 if (Tok.isNot(tok::at)) {
Chris Lattnerd4c2ec72009-01-09 04:34:13 +0000251 // The code below does not consume '}'s because it is afraid of eating the
252 // end of a namespace. Because of the way this code is structured, an
253 // erroneous r_brace would cause an infinite loop if not handled here.
254 if (Tok.is(tok::r_brace))
255 break;
256
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000257 // FIXME: as the name implies, this rule allows function definitions.
258 // We could pass a flag or check for functions during semantic analysis.
Chris Lattnera17991f2009-03-29 16:50:03 +0000259 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere48b46b2008-10-20 05:46:22 +0000260 continue;
261 }
262
263 // Otherwise, we have an @ directive, eat the @.
264 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000265 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000266
Chris Lattnercba730b2008-10-20 05:57:40 +0000267 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000268 AtEndLoc = AtLoc;
269 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000270 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000271
Chris Lattnera40577e2008-10-20 06:10:06 +0000272 // Eat the identifier.
273 ConsumeToken();
274
Chris Lattnercba730b2008-10-20 05:57:40 +0000275 switch (DirectiveKind) {
276 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000277 // FIXME: If someone forgets an @end on a protocol, this loop will
278 // continue to eat up tons of stuff and spew lots of nonsense errors. It
279 // would probably be better to bail out if we saw an @class or @interface
280 // or something like that.
Chris Lattner727fb1f2008-10-20 07:22:18 +0000281 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000282 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000283 SkipUntil(tok::r_brace, tok::at);
284 break;
285
286 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000287 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000288 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000289 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000290 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000291 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000292 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000293 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000294 break;
295
296 case tok::objc_property:
Chris Lattner727fb1f2008-10-20 07:22:18 +0000297 if (!getLang().ObjC2)
298 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
299
Chris Lattnere48b46b2008-10-20 05:46:22 +0000300 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000301 // Parse property attribute list, if any.
Chris Lattner22f9d262008-10-20 07:24:39 +0000302 if (Tok.is(tok::l_paren))
Chris Lattnere48b46b2008-10-20 05:46:22 +0000303 ParseObjCPropertyAttribute(OCDS);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000304
Chris Lattnere48b46b2008-10-20 05:46:22 +0000305 // Parse all the comma separated declarators.
306 DeclSpec DS;
307 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
308 ParseStructDeclaration(DS, FieldDeclarators);
309
Chris Lattner9019ae52008-10-20 06:15:13 +0000310 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
311 tok::at);
312
Chris Lattnere48b46b2008-10-20 05:46:22 +0000313 // Convert them all to property declarations.
314 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
315 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000316 if (FD.D.getIdentifier() == 0) {
Chris Lattner194e7002008-11-18 07:50:21 +0000317 Diag(AtLoc, diag::err_objc_property_requires_field_name)
318 << FD.D.getSourceRange();
Chris Lattnerbf16a972008-10-20 06:33:53 +0000319 continue;
320 }
Fariborz Jahanian8f314552009-01-17 23:21:10 +0000321 if (FD.BitfieldSize) {
322 Diag(AtLoc, diag::err_objc_property_bitfield)
323 << FD.D.getSourceRange();
324 continue;
325 }
Chris Lattnerbf16a972008-10-20 06:33:53 +0000326
Chris Lattnere48b46b2008-10-20 05:46:22 +0000327 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000328 IdentifierInfo *SelName =
329 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
330
Chris Lattnere48b46b2008-10-20 05:46:22 +0000331 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000332 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000333 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahaniand7c76682009-03-12 22:34:11 +0000334 Selector SetterSel;
335 if (SetterName)
336 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
337 else
338 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
339 PP.getSelectorTable(),
340 FD.D.getIdentifier());
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000341 bool isOverridingProperty = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000342 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
343 GetterSel, SetterSel,
344 interfaceDecl,
345 &isOverridingProperty,
346 MethodImplKind);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000347 if (!isOverridingProperty)
348 allProperties.push_back(Property);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000349 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000350 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000351 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000352 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000353
354 // We break out of the big loop in two cases: when we see @end or when we see
355 // EOF. In the former case, eat the @end. In the later case, emit an error.
356 if (Tok.isObjCAtKeyword(tok::objc_end))
357 ConsumeToken(); // the "end" identifier
358 else
359 Diag(Tok, diag::err_objc_missing_end);
360
Chris Lattnercba730b2008-10-20 05:57:40 +0000361 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000362 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000363 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +0000364 allMethods.data(), allMethods.size(),
365 allProperties.data(), allProperties.size(),
366 allTUVariables.data(), allTUVariables.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000367}
368
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000369/// Parse property attribute declarations.
370///
371/// property-attr-decl: '(' property-attrlist ')'
372/// property-attrlist:
373/// property-attribute
374/// property-attrlist ',' property-attribute
375/// property-attribute:
376/// getter '=' identifier
377/// setter '=' identifier ':'
378/// readonly
379/// readwrite
380/// assign
381/// retain
382/// copy
383/// nonatomic
384///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000385void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner22f9d262008-10-20 07:24:39 +0000386 assert(Tok.getKind() == tok::l_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000387 SourceLocation LHSLoc = ConsumeParen(); // consume '('
388
Chris Lattner1e5cc722008-10-20 07:15:22 +0000389 while (1) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000390 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner727fb1f2008-10-20 07:22:18 +0000391
392 // If this is not an identifier at all, bail out early.
393 if (II == 0) {
394 MatchRHSPunctuation(tok::r_paren, LHSLoc);
395 return;
396 }
397
Chris Lattner9ba0b222008-10-20 07:37:22 +0000398 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
399
Chris Lattner05fb7c82008-11-20 04:42:34 +0000400 if (II->isStr("readonly"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000401 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000402 else if (II->isStr("assign"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000403 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000404 else if (II->isStr("readwrite"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000405 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000406 else if (II->isStr("retain"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000407 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000408 else if (II->isStr("copy"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000409 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000410 else if (II->isStr("nonatomic"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000412 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner2cc2b872008-10-20 07:39:53 +0000413 // getter/setter require extra treatment.
Chris Lattner9ba0b222008-10-20 07:37:22 +0000414 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
415 tok::r_paren))
Chris Lattner35cd4b92008-10-20 07:00:43 +0000416 return;
Chris Lattner9ba0b222008-10-20 07:37:22 +0000417
Chris Lattner22f9d262008-10-20 07:24:39 +0000418 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000419 Diag(Tok, diag::err_expected_ident);
Chris Lattner22f9d262008-10-20 07:24:39 +0000420 SkipUntil(tok::r_paren);
421 return;
422 }
423
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000424 if (II->getName()[0] == 's') {
Chris Lattner22f9d262008-10-20 07:24:39 +0000425 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
426 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000427 ConsumeToken(); // consume method name
428
429 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
430 tok::r_paren))
Chris Lattner22f9d262008-10-20 07:24:39 +0000431 return;
Chris Lattner22f9d262008-10-20 07:24:39 +0000432 } else {
433 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
434 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000435 ConsumeToken(); // consume method name
Chris Lattner22f9d262008-10-20 07:24:39 +0000436 }
Chris Lattner2cc2b872008-10-20 07:39:53 +0000437 } else {
Chris Lattner5e58ac22008-11-19 07:49:38 +0000438 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000439 SkipUntil(tok::r_paren);
440 return;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000441 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000442
Chris Lattner9ba0b222008-10-20 07:37:22 +0000443 if (Tok.isNot(tok::comma))
444 break;
Chris Lattner35cd4b92008-10-20 07:00:43 +0000445
Chris Lattner9ba0b222008-10-20 07:37:22 +0000446 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000447 }
Chris Lattner9ba0b222008-10-20 07:37:22 +0000448
449 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000450}
451
Steve Naroff81f1bba2007-09-06 21:24:23 +0000452/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000453/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000454/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000455///
456/// objc-instance-method: '-'
457/// objc-class-method: '+'
458///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000459/// objc-method-attributes: [OBJC2]
460/// __attribute__((deprecated))
461///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000462Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
463 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000464 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000465
466 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000467 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000468
Chris Lattner5261d0c2009-03-28 19:18:32 +0000469 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000470 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000471 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000472 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473}
474
475/// objc-selector:
476/// identifier
477/// one of
478/// enum struct union if else while do for switch case default
479/// break continue return goto asm sizeof typeof __alignof
480/// unsigned long const short volatile signed restrict _Complex
481/// in out inout bycopy byref oneway int char float double void _Bool
482///
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +0000483IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000484 switch (Tok.getKind()) {
485 default:
486 return 0;
487 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000488 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000489 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000490 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000491 case tok::kw_break:
492 case tok::kw_case:
493 case tok::kw_catch:
494 case tok::kw_char:
495 case tok::kw_class:
496 case tok::kw_const:
497 case tok::kw_const_cast:
498 case tok::kw_continue:
499 case tok::kw_default:
500 case tok::kw_delete:
501 case tok::kw_do:
502 case tok::kw_double:
503 case tok::kw_dynamic_cast:
504 case tok::kw_else:
505 case tok::kw_enum:
506 case tok::kw_explicit:
507 case tok::kw_export:
508 case tok::kw_extern:
509 case tok::kw_false:
510 case tok::kw_float:
511 case tok::kw_for:
512 case tok::kw_friend:
513 case tok::kw_goto:
514 case tok::kw_if:
515 case tok::kw_inline:
516 case tok::kw_int:
517 case tok::kw_long:
518 case tok::kw_mutable:
519 case tok::kw_namespace:
520 case tok::kw_new:
521 case tok::kw_operator:
522 case tok::kw_private:
523 case tok::kw_protected:
524 case tok::kw_public:
525 case tok::kw_register:
526 case tok::kw_reinterpret_cast:
527 case tok::kw_restrict:
528 case tok::kw_return:
529 case tok::kw_short:
530 case tok::kw_signed:
531 case tok::kw_sizeof:
532 case tok::kw_static:
533 case tok::kw_static_cast:
534 case tok::kw_struct:
535 case tok::kw_switch:
536 case tok::kw_template:
537 case tok::kw_this:
538 case tok::kw_throw:
539 case tok::kw_true:
540 case tok::kw_try:
541 case tok::kw_typedef:
542 case tok::kw_typeid:
543 case tok::kw_typename:
544 case tok::kw_typeof:
545 case tok::kw_union:
546 case tok::kw_unsigned:
547 case tok::kw_using:
548 case tok::kw_virtual:
549 case tok::kw_void:
550 case tok::kw_volatile:
551 case tok::kw_wchar_t:
552 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000553 case tok::kw__Bool:
554 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000555 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000556 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000557 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000558 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000559 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000560}
561
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000562/// objc-for-collection-in: 'in'
563///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000564bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000565 // FIXME: May have to do additional look-ahead to only allow for
566 // valid tokens following an 'in'; such as an identifier, unary operators,
567 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000568 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000569 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000570}
571
Ted Kremenek42730c52008-01-07 19:49:32 +0000572/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000573/// qualifier list and builds their bitmask representation in the input
574/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000575///
576/// objc-type-qualifiers:
577/// objc-type-qualifier
578/// objc-type-qualifiers objc-type-qualifier
579///
Ted Kremenek42730c52008-01-07 19:49:32 +0000580void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000581 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000582 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000583 return;
584
585 const IdentifierInfo *II = Tok.getIdentifierInfo();
586 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000587 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000588 continue;
589
Ted Kremenek42730c52008-01-07 19:49:32 +0000590 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000591 switch (i) {
592 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000593 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
594 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
595 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
596 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
597 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
598 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000599 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000600 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000601 ConsumeToken();
602 II = 0;
603 break;
604 }
605
606 // If this wasn't a recognized qualifier, bail out.
607 if (II) return;
608 }
609}
610
611/// objc-type-name:
612/// '(' objc-type-qualifiers[opt] type-name ')'
613/// '(' objc-type-qualifiers[opt] ')'
614///
Ted Kremenek42730c52008-01-07 19:49:32 +0000615Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000616 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000617
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000618 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnerb5769332008-08-23 01:48:03 +0000619 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000620
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000621 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000623
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000624 TypeTy *Ty = 0;
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000625 if (isTypeSpecifierQualifier()) {
626 TypeResult TypeSpec = ParseTypeName();
627 if (!TypeSpec.isInvalid())
628 Ty = TypeSpec.get();
629 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000630
Steve Naroffc6235e82008-10-21 14:15:04 +0000631 if (Tok.is(tok::r_paren))
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000632 ConsumeParen();
633 else if (Tok.getLocation() == TypeStartLoc) {
634 // If we didn't eat any tokens, then this isn't a type.
Chris Lattnerf006a222008-11-18 07:48:38 +0000635 Diag(Tok, diag::err_expected_type);
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000636 SkipUntil(tok::r_paren);
637 } else {
638 // Otherwise, we found *something*, but didn't get a ')' in the right
639 // place. Emit an error then return what we have as the type.
640 MatchRHSPunctuation(tok::r_paren, LParenLoc);
641 }
Steve Naroff304ed392007-09-05 23:30:30 +0000642 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000643}
644
645/// objc-method-decl:
646/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000647/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000648/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000649/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000650///
651/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000652/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000653/// objc-keyword-selector objc-keyword-decl
654///
655/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000656/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
657/// objc-selector ':' objc-keyword-attributes[opt] identifier
658/// ':' objc-type-name objc-keyword-attributes[opt] identifier
659/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000660///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000661/// objc-parmlist:
662/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000663///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000664/// objc-parms:
665/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000666///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000667/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000668/// , ...
669///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000670/// objc-keyword-attributes: [OBJC2]
671/// __attribute__((unused))
672///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000673Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
674 tok::TokenKind mType,
675 DeclPtrTy IDecl,
676 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000677 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000678 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000679 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000680 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000681 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000682
Steve Naroff3774dd92007-10-26 20:53:56 +0000683 SourceLocation selLoc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +0000684 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000685
Steve Naroff1a781092009-02-11 20:43:13 +0000686 // An unnamed colon is valid.
687 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattnerf006a222008-11-18 07:48:38 +0000688 Diag(Tok, diag::err_expected_selector_for_method)
689 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnerb5769332008-08-23 01:48:03 +0000690 // Skip until we get a ; or {}.
691 SkipUntil(tok::r_brace);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000692 return DeclPtrTy();
Chris Lattnerb5769332008-08-23 01:48:03 +0000693 }
694
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000695 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000696 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000697 // If attributes exist after the method, parse them.
698 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000699 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000700 MethodAttrs = ParseAttributes();
701
702 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000703 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000704 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek61ccd5c2009-05-04 17:04:30 +0000705 0, CargNames, MethodAttrs,
706 MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000707 }
Steve Naroff304ed392007-09-05 23:30:30 +0000708
Steve Naroff4ed9d662007-09-27 14:38:14 +0000709 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattner612a2f72009-04-11 18:57:04 +0000710 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Chris Lattnerd031a452007-10-07 02:00:24 +0000711
Chris Lattnerd031a452007-10-07 02:00:24 +0000712 while (1) {
Chris Lattner612a2f72009-04-11 18:57:04 +0000713 Action::ObjCArgInfo ArgInfo;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000714
Chris Lattnerd031a452007-10-07 02:00:24 +0000715 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000716 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000717 Diag(Tok, diag::err_expected_colon);
718 break;
719 }
720 ConsumeToken(); // Eat the ':'.
Steve Naroff304ed392007-09-05 23:30:30 +0000721
Chris Lattner612a2f72009-04-11 18:57:04 +0000722 ArgInfo.Type = 0;
723 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
724 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
725
Chris Lattnerd031a452007-10-07 02:00:24 +0000726 // If attributes exist before the argument name, parse them.
Chris Lattner612a2f72009-04-11 18:57:04 +0000727 ArgInfo.ArgAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000728 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner612a2f72009-04-11 18:57:04 +0000729 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000730
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000731 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000732 Diag(Tok, diag::err_expected_ident); // missing argument name.
733 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000734 }
Chris Lattner612a2f72009-04-11 18:57:04 +0000735
736 ArgInfo.Name = Tok.getIdentifierInfo();
737 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerd031a452007-10-07 02:00:24 +0000738 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000739
Chris Lattner612a2f72009-04-11 18:57:04 +0000740 ArgInfos.push_back(ArgInfo);
741 KeyIdents.push_back(SelIdent);
742
Chris Lattnerd031a452007-10-07 02:00:24 +0000743 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000744 SourceLocation Loc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +0000745 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000746 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000747 break;
748 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000749 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000750
Steve Naroff29fe7462007-11-15 12:35:21 +0000751 bool isVariadic = false;
752
Chris Lattnerd031a452007-10-07 02:00:24 +0000753 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000754 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000755 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000756 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000757 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000758 ConsumeToken();
759 break;
760 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000761 DeclSpec DS;
762 ParseDeclarationSpecifiers(DS);
763 // Parse the declarator.
764 Declarator ParmDecl(DS, Declarator::PrototypeContext);
765 ParseDeclarator(ParmDecl);
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000766 CargNames.push_back(ParmDecl);
Chris Lattnerd031a452007-10-07 02:00:24 +0000767 }
768
769 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000770 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000771 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000772 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000773 MethodAttrs = ParseAttributes();
774
775 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
776 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000777 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000778 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek61ccd5c2009-05-04 17:04:30 +0000779 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff29fe7462007-11-15 12:35:21 +0000780 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000781}
782
Steve Narofffb367882007-08-20 21:31:48 +0000783/// objc-protocol-refs:
784/// '<' identifier-list '>'
785///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000786bool Parser::
Chris Lattner5261d0c2009-03-28 19:18:32 +0000787ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000788 bool WarnOnDeclarations, SourceLocation &EndLoc) {
789 assert(Tok.is(tok::less) && "expected <");
790
791 ConsumeToken(); // the "<"
792
793 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
794
795 while (1) {
796 if (Tok.isNot(tok::identifier)) {
797 Diag(Tok, diag::err_expected_ident);
798 SkipUntil(tok::greater);
799 return true;
800 }
801 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
802 Tok.getLocation()));
803 ConsumeToken();
804
805 if (Tok.isNot(tok::comma))
806 break;
807 ConsumeToken();
808 }
809
810 // Consume the '>'.
811 if (Tok.isNot(tok::greater)) {
812 Diag(Tok, diag::err_expected_greater);
813 return true;
814 }
815
816 EndLoc = ConsumeAnyToken();
817
818 // Convert the list of protocols identifiers into a list of protocol decls.
819 Actions.FindProtocolDeclaration(WarnOnDeclarations,
820 &ProtocolIdents[0], ProtocolIdents.size(),
821 Protocols);
822 return false;
823}
824
Steve Narofffb367882007-08-20 21:31:48 +0000825/// objc-class-instance-variables:
826/// '{' objc-instance-variable-decl-list[opt] '}'
827///
828/// objc-instance-variable-decl-list:
829/// objc-visibility-spec
830/// objc-instance-variable-decl ';'
831/// ';'
832/// objc-instance-variable-decl-list objc-visibility-spec
833/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
834/// objc-instance-variable-decl-list ';'
835///
836/// objc-visibility-spec:
837/// @private
838/// @protected
839/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000840/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000841///
842/// objc-instance-variable-decl:
843/// struct-declaration
844///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000845void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000846 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000847 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5261d0c2009-03-28 19:18:32 +0000848 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000849 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
850
Douglas Gregor5eca99e2009-01-12 18:45:55 +0000851 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +0000852
Steve Naroffc4474992007-08-21 21:17:12 +0000853 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000854
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000855 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000856 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000857 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000858 // Each iteration of this loop reads one objc-instance-variable-decl.
859
860 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000861 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000862 Diag(Tok, diag::ext_extra_struct_semi);
863 ConsumeToken();
864 continue;
865 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000866
Steve Naroffc4474992007-08-21 21:17:12 +0000867 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000868 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000869 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000870 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000871 case tok::objc_private:
872 case tok::objc_public:
873 case tok::objc_protected:
874 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000875 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000876 ConsumeToken();
877 continue;
878 default:
879 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000880 continue;
881 }
882 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000883
884 // Parse all the comma separated declarators.
885 DeclSpec DS;
886 FieldDeclarators.clear();
887 ParseStructDeclaration(DS, FieldDeclarators);
888
889 // Convert them all to fields.
890 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
891 FieldDeclarator &FD = FieldDeclarators[i];
892 // Install the declarator into interfaceDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000893 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
894 DS.getSourceRange().getBegin(),
895 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000896 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000897 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000898
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000899 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000900 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000901 } else {
902 Diag(Tok, diag::err_expected_semi_decl_list);
903 // Skip to end of block or statement
904 SkipUntil(tok::r_brace, true, true);
905 }
906 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000907 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000908 // Call ActOnFields() even if we don't have any decls. This is useful
909 // for code rewriting tools that need to be aware of the empty list.
910 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +0000911 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000912 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000913 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000914}
Steve Narofffb367882007-08-20 21:31:48 +0000915
916/// objc-protocol-declaration:
917/// objc-protocol-definition
918/// objc-protocol-forward-reference
919///
920/// objc-protocol-definition:
921/// @protocol identifier
922/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000923/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000924/// @end
925///
926/// objc-protocol-forward-reference:
927/// @protocol identifier-list ';'
928///
929/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000930/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000931/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000932Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
933 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000934 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000935 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
936 ConsumeToken(); // the "protocol" identifier
937
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000938 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000939 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000940 return DeclPtrTy();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000941 }
942 // Save the protocol name, then consume it.
943 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
944 SourceLocation nameLoc = ConsumeToken();
945
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000946 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000947 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000948 ConsumeToken();
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000949 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
950 attrList);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000951 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000952
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000953 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000954 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
955 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
956
Steve Naroff72f17fb2007-08-22 22:17:26 +0000957 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000958 while (1) {
959 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000960 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000961 Diag(Tok, diag::err_expected_ident);
962 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000963 return DeclPtrTy();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000964 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000965 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
966 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000967 ConsumeToken(); // the identifier
968
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000969 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000970 break;
971 }
972 // Consume the ';'.
973 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000974 return DeclPtrTy();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000975
Steve Naroff415c1832007-10-10 17:32:04 +0000976 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000977 &ProtocolRefs[0],
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000978 ProtocolRefs.size(),
979 attrList);
Chris Lattnere705e5e2008-07-21 22:17:28 +0000980 }
981
Steve Naroff72f17fb2007-08-22 22:17:26 +0000982 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000983 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000984
Chris Lattner5261d0c2009-03-28 19:18:32 +0000985 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000986 if (Tok.is(tok::less) &&
Chris Lattnere6dcfc22009-04-12 08:43:13 +0000987 ParseObjCProtocolReferences(ProtocolRefs, false, EndProtoLoc))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000988 return DeclPtrTy();
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000989
Chris Lattner5261d0c2009-03-28 19:18:32 +0000990 DeclPtrTy ProtoType =
Chris Lattner2bdedd62008-07-26 04:03:38 +0000991 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +0000992 ProtocolRefs.data(),
993 ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000994 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000995 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000996 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +0000997}
Steve Narofffb367882007-08-20 21:31:48 +0000998
999/// objc-implementation:
1000/// objc-class-implementation-prologue
1001/// objc-category-implementation-prologue
1002///
1003/// objc-class-implementation-prologue:
1004/// @implementation identifier objc-superclass[opt]
1005/// objc-class-instance-variables[opt]
1006///
1007/// objc-category-implementation-prologue:
1008/// @implementation identifier ( identifier )
Chris Lattner5261d0c2009-03-28 19:18:32 +00001009Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001010 SourceLocation atLoc) {
1011 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1012 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1013 ConsumeToken(); // the "implementation" identifier
1014
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001016 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001017 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001018 }
1019 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001020 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001021 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1022
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001023 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001024 // we have a category implementation.
1025 SourceLocation lparenLoc = ConsumeParen();
1026 SourceLocation categoryLoc, rparenLoc;
1027 IdentifierInfo *categoryId = 0;
1028
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001029 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001030 categoryId = Tok.getIdentifierInfo();
1031 categoryLoc = ConsumeToken();
1032 } else {
1033 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001034 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001035 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001036 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001037 Diag(Tok, diag::err_expected_rparen);
1038 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner5261d0c2009-03-28 19:18:32 +00001039 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001040 }
1041 rparenLoc = ConsumeParen();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001042 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001043 atLoc, nameId, nameLoc, categoryId,
1044 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001045 ObjCImpDecl = ImplCatType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001046 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001047 }
1048 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001049 SourceLocation superClassLoc;
1050 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001051 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001052 // We have a super class
1053 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001054 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001055 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001056 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001057 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001058 superClassId = Tok.getIdentifierInfo();
1059 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001061 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001062 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001063 superClassId, superClassLoc);
1064
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001065 if (Tok.is(tok::l_brace)) // we have ivars
1066 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001067 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001068
Chris Lattner5261d0c2009-03-28 19:18:32 +00001069 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +00001070}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001071
Chris Lattner5261d0c2009-03-28 19:18:32 +00001072Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001073 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1074 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner5261d0c2009-03-28 19:18:32 +00001075 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001076 ConsumeToken(); // the "end" identifier
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +00001077 if (ObjCImpDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001079 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +00001080 }
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001081 else
1082 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +00001083 return Result;
Steve Narofffb367882007-08-20 21:31:48 +00001084}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001085
1086/// compatibility-alias-decl:
1087/// @compatibility_alias alias-name class-name ';'
1088///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001089Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001090 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1091 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1092 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001093 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001094 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001095 return DeclPtrTy();
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001096 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001097 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1098 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001099 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001100 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001101 return DeclPtrTy();
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001102 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001103 IdentifierInfo *classId = Tok.getIdentifierInfo();
1104 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1105 if (Tok.isNot(tok::semi)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001106 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001107 return DeclPtrTy();
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001108 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001109 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1110 classId, classLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001111}
1112
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001113/// property-synthesis:
1114/// @synthesize property-ivar-list ';'
1115///
1116/// property-ivar-list:
1117/// property-ivar
1118/// property-ivar-list ',' property-ivar
1119///
1120/// property-ivar:
1121/// identifier
1122/// identifier '=' identifier
1123///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001124Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001125 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1126 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001127 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001128 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001129 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001130 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001131 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001132
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001133 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001134 IdentifierInfo *propertyIvar = 0;
1135 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1136 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001137 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001138 // property '=' ivar-name
1139 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001140 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001141 Diag(Tok, diag::err_expected_ident);
1142 break;
1143 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001144 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001145 ConsumeToken(); // consume ivar-name
1146 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001147 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1148 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001150 break;
1151 ConsumeToken(); // consume ','
1152 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001153 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001154 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001155 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +00001156}
1157
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001158/// property-dynamic:
1159/// @dynamic property-list
1160///
1161/// property-list:
1162/// identifier
1163/// property-list ',' identifier
1164///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001165Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001166 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1167 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1168 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001169 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001170 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001171 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001172 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001173 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001174 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1175 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1176 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1177 propertyId, 0);
1178
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001179 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001180 break;
1181 ConsumeToken(); // consume ','
1182 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001183 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001184 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001185 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001186}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001187
1188/// objc-throw-statement:
1189/// throw expression[opt];
1190///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001191Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl62261042008-12-09 20:22:58 +00001192 OwningExprResult Res(Actions);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001193 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001195 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001196 if (Res.isInvalid()) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001197 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001198 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001199 }
1200 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001201 ConsumeToken(); // consume ';'
Steve Naroff590fe482009-02-11 20:05:44 +00001202 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001203}
1204
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001205/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001206/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001207///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001208Parser::OwningStmtResult
1209Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001210 ConsumeToken(); // consume synchronized
1211 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001212 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl15bf1452008-12-11 20:12:42 +00001213 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001214 }
1215 ConsumeParen(); // '('
Sebastian Redl14ca7412008-12-11 21:36:32 +00001216 OwningExprResult Res(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001217 if (Res.isInvalid()) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001218 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001219 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001220 }
1221 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001222 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001223 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001224 }
1225 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001226 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001227 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001228 return StmtError();
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001229 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001230 // Enter a scope to hold everything within the compound stmt. Compound
1231 // statements can always hold declarations.
Douglas Gregor95d40792008-12-10 06:34:36 +00001232 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff70337ac2008-06-04 20:36:13 +00001233
Sebastian Redl10c32952008-12-11 19:30:53 +00001234 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001235
Douglas Gregor95d40792008-12-10 06:34:36 +00001236 BodyScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001237 if (SynchBody.isInvalid())
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001238 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl81db6682009-02-05 15:02:23 +00001239 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001240}
1241
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001242/// objc-try-catch-statement:
1243/// @try compound-statement objc-catch-list[opt]
1244/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1245///
1246/// objc-catch-list:
1247/// @catch ( parameter-declaration ) compound-statement
1248/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1249/// catch-parameter-declaration:
1250/// parameter-declaration
1251/// '...' [OBJC2]
1252///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001253Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001254 bool catch_or_finally_seen = false;
Sebastian Redl15bf1452008-12-11 20:12:42 +00001255
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001256 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001257 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001258 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001259 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001260 }
Sebastian Redl62261042008-12-09 20:22:58 +00001261 OwningStmtResult CatchStmts(Actions);
1262 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor95d40792008-12-10 06:34:36 +00001263 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl10c32952008-12-11 19:30:53 +00001264 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor95d40792008-12-10 06:34:36 +00001265 TryScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001266 if (TryBody.isInvalid())
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001267 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl6008ac32008-11-25 22:21:31 +00001268
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001269 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001270 // At this point, we need to lookahead to determine if this @ is the start
1271 // of an @catch or @finally. We don't want to consume the @ token if this
1272 // is an @try or @encode or something else.
1273 Token AfterAt = GetLookAheadToken(1);
1274 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1275 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1276 break;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001277
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001278 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001279 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001280 DeclPtrTy FirstPart;
Fariborz Jahanian06798362007-11-01 23:59:59 +00001281 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001282 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001283 ConsumeParen();
Steve Naroff590fe482009-02-11 20:05:44 +00001284 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001285 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001286 DeclSpec DS;
1287 ParseDeclarationSpecifiers(DS);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001288 // For some odd reason, the name of the exception variable is
Steve Naroff0e8b96a2009-03-03 19:52:17 +00001289 // optional. As a result, we need to use "PrototypeContext", because
1290 // we must accept either 'declarator' or 'abstract-declarator' here.
1291 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1292 ParseDeclarator(ParmDecl);
1293
1294 // Inform the actions module about the parameter declarator, so it
1295 // gets added to the current scope.
1296 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffc949a462008-02-05 21:27:35 +00001297 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001298 ConsumeToken(); // consume '...'
Steve Naroff61e613c2009-04-07 22:56:58 +00001299
1300 SourceLocation RParenLoc;
Chris Lattner8027be62008-02-14 19:27:54 +00001301
Steve Naroff61e613c2009-04-07 22:56:58 +00001302 if (Tok.is(tok::r_paren))
1303 RParenLoc = ConsumeParen();
1304 else // Skip over garbage, until we get to ')'. Eat the ')'.
1305 SkipUntil(tok::r_paren, true, false);
1306
Sebastian Redl62261042008-12-09 20:22:58 +00001307 OwningStmtResult CatchBody(Actions, true);
Chris Lattner8027be62008-02-14 19:27:54 +00001308 if (Tok.is(tok::l_brace))
1309 CatchBody = ParseCompoundStatementBody();
1310 else
1311 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001312 if (CatchBody.isInvalid())
Fariborz Jahanian06798362007-11-01 23:59:59 +00001313 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001314 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +00001315 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl81db6682009-02-05 15:02:23 +00001316 move(CatchStmts));
Steve Naroffc949a462008-02-05 21:27:35 +00001317 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +00001318 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1319 << "@catch clause";
Sebastian Redl15bf1452008-12-11 20:12:42 +00001320 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001321 }
1322 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001323 } else {
1324 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001325 ConsumeToken(); // consume finally
Douglas Gregor95d40792008-12-10 06:34:36 +00001326 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001327
Sebastian Redl62261042008-12-09 20:22:58 +00001328 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner8027be62008-02-14 19:27:54 +00001329 if (Tok.is(tok::l_brace))
1330 FinallyBody = ParseCompoundStatementBody();
1331 else
1332 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001333 if (FinallyBody.isInvalid())
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001334 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001335 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001336 move(FinallyBody));
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001337 catch_or_finally_seen = true;
1338 break;
1339 }
1340 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001341 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001342 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001343 return StmtError();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001344 }
Sebastian Redl81db6682009-02-05 15:02:23 +00001345 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1346 move(FinallyStmt));
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001347}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001348
Steve Naroff81f1bba2007-09-06 21:24:23 +00001349/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001350///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001351Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1352 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner9a9936f2009-03-05 02:03:49 +00001353
Chris Lattnerc309ade2009-03-05 08:00:35 +00001354 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1355 PP.getSourceManager(),
1356 "parsing Objective-C method");
Chris Lattner9a9936f2009-03-05 02:03:49 +00001357
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001358 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001359 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001360 ConsumeToken();
1361
Steve Naroff9191a9e82007-11-11 19:54:21 +00001362 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001363 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001364 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001365
1366 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1367 SkipUntil(tok::l_brace, true, true);
1368
1369 // If we didn't find the '{', bail out.
1370 if (Tok.isNot(tok::l_brace))
Chris Lattner5261d0c2009-03-28 19:18:32 +00001371 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001372 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001373 SourceLocation BraceLoc = Tok.getLocation();
1374
1375 // Enter a scope for the method body.
Douglas Gregor95d40792008-12-10 06:34:36 +00001376 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001377
1378 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001379 // specified Declarator for the method.
Steve Naroff8df46022009-02-28 16:59:13 +00001380 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl10c32952008-12-11 19:30:53 +00001381
1382 OwningStmtResult FnBody(ParseCompoundStatementBody());
1383
Steve Naroff9191a9e82007-11-11 19:54:21 +00001384 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001385 if (FnBody.isInvalid())
Sebastian Redl76b9ddb2008-12-21 12:04:03 +00001386 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1387 MultiStmtArg(Actions), false);
Sebastian Redl91f9b0a2008-12-13 16:23:55 +00001388
Steve Naroffb50441b2009-03-02 22:00:56 +00001389 // TODO: Pass argument information.
1390 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1391
Steve Naroff9191a9e82007-11-11 19:54:21 +00001392 // Leave the function body scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001393 BodyScope.Exit();
Sebastian Redl91f9b0a2008-12-13 16:23:55 +00001394
Steve Naroff18c83382007-11-13 23:01:27 +00001395 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001396}
Anders Carlssona66cad42007-08-21 17:43:55 +00001397
Sebastian Redl15bf1452008-12-11 20:12:42 +00001398Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffc949a462008-02-05 21:27:35 +00001399 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001400 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001401 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1402 return ParseObjCThrowStmt(AtLoc);
1403 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1404 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001405 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001406 if (Res.isInvalid()) {
Steve Naroffc949a462008-02-05 21:27:35 +00001407 // If the expression is invalid, skip ahead to the next semicolon. Not
1408 // doing this opens us up to the possibility of infinite loops if
1409 // ParseExpression does not consume any tokens.
1410 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001411 return StmtError();
Steve Naroffc949a462008-02-05 21:27:35 +00001412 }
1413 // Otherwise, eat the semicolon.
1414 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson18ca4772009-05-17 21:11:30 +00001415 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffc949a462008-02-05 21:27:35 +00001416}
1417
Sebastian Redla2deb432008-12-13 15:32:12 +00001418Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001419 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001420 case tok::string_literal: // primary-expression: string-literal
1421 case tok::wide_string_literal:
Sebastian Redla2deb432008-12-13 15:32:12 +00001422 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerddd3e632007-12-12 01:04:12 +00001423 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001424 if (Tok.getIdentifierInfo() == 0)
Sebastian Redla2deb432008-12-13 15:32:12 +00001425 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl14ca7412008-12-11 21:36:32 +00001426
Chris Lattnerf9311a92008-08-05 06:19:09 +00001427 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1428 case tok::objc_encode:
Sebastian Redla2deb432008-12-13 15:32:12 +00001429 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001430 case tok::objc_protocol:
Sebastian Redla2deb432008-12-13 15:32:12 +00001431 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001432 case tok::objc_selector:
Sebastian Redla2deb432008-12-13 15:32:12 +00001433 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001434 default:
Sebastian Redla2deb432008-12-13 15:32:12 +00001435 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001436 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001437 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001438}
1439
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001440/// objc-message-expr:
1441/// '[' objc-receiver objc-message-args ']'
1442///
1443/// objc-receiver:
1444/// expression
1445/// class-name
1446/// type-name
Sebastian Redla2deb432008-12-13 15:32:12 +00001447Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnered27a532008-01-25 18:59:06 +00001448 assert(Tok.is(tok::l_square) && "'[' expected");
1449 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1450
1451 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001452 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001453 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanian00740212009-04-08 19:50:10 +00001454 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1455 SourceLocation NameLoc = ConsumeToken();
1456 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1457 ExprArg(Actions));
1458 }
Chris Lattnered27a532008-01-25 18:59:06 +00001459 }
1460
Sebastian Redl14ca7412008-12-11 21:36:32 +00001461 OwningExprResult Res(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001462 if (Res.isInvalid()) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001463 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001464 return move(Res);
Chris Lattnered27a532008-01-25 18:59:06 +00001465 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001466
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001467 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +00001468 0, move(Res));
Chris Lattnered27a532008-01-25 18:59:06 +00001469}
Sebastian Redla2deb432008-12-13 15:32:12 +00001470
Chris Lattnered27a532008-01-25 18:59:06 +00001471/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1472/// the rest of a message expression.
Sebastian Redla2deb432008-12-13 15:32:12 +00001473///
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001474/// objc-message-args:
1475/// objc-selector
1476/// objc-keywordarg-list
1477///
1478/// objc-keywordarg-list:
1479/// objc-keywordarg
1480/// objc-keywordarg-list objc-keywordarg
1481///
1482/// objc-keywordarg:
1483/// selector-name[opt] ':' objc-keywordexpr
1484///
1485/// objc-keywordexpr:
1486/// nonempty-expr-list
1487///
1488/// nonempty-expr-list:
1489/// assignment-expression
1490/// nonempty-expr-list , assignment-expression
Sebastian Redla2deb432008-12-13 15:32:12 +00001491///
1492Parser::OwningExprResult
Chris Lattnered27a532008-01-25 18:59:06 +00001493Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +00001494 SourceLocation NameLoc,
Chris Lattnered27a532008-01-25 18:59:06 +00001495 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-12-13 15:32:12 +00001496 ExprArg ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001497 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001498 SourceLocation Loc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001499 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001500
Anders Carlssonb42900d2009-02-14 18:21:46 +00001501 SourceLocation SelectorLoc = Loc;
1502
Steve Naroff4ed9d662007-09-27 14:38:14 +00001503 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001504 ExprVector KeyExprs(Actions);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001505
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001506 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001507 while (1) {
1508 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001509 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001510
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001511 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001512 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001513 // We must manually skip to a ']', otherwise the expression skipper will
1514 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1515 // the enclosing expression.
1516 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001517 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001518 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001519
Steve Naroff4ed9d662007-09-27 14:38:14 +00001520 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001521 /// Parse the expression after ':'
Sebastian Redl14ca7412008-12-11 21:36:32 +00001522 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001523 if (Res.isInvalid()) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001524 // We must manually skip to a ']', otherwise the expression skipper will
1525 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1526 // the enclosing expression.
1527 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001528 return move(Res);
Steve Naroff253118b2007-09-17 20:25:27 +00001529 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001530
Steve Naroff253118b2007-09-17 20:25:27 +00001531 // We have a valid expression.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001532 KeyExprs.push_back(Res.release());
Sebastian Redla2deb432008-12-13 15:32:12 +00001533
Steve Naroff253118b2007-09-17 20:25:27 +00001534 // Check for another keyword selector.
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001535 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001536 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001537 break;
1538 // We have a selector or a colon, continue parsing.
1539 }
1540 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001541 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001542 ConsumeToken(); // Eat the ','.
1543 /// Parse the expression after ','
Sebastian Redl14ca7412008-12-11 21:36:32 +00001544 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001545 if (Res.isInvalid()) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001546 // We must manually skip to a ']', otherwise the expression skipper will
1547 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1548 // the enclosing expression.
1549 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001550 return move(Res);
Steve Naroff9f176d12007-11-15 13:05:42 +00001551 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001552
Steve Naroff9f176d12007-11-15 13:05:42 +00001553 // We have a valid expression.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001554 KeyExprs.push_back(Res.release());
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001555 }
1556 } else if (!selIdent) {
1557 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redla2deb432008-12-13 15:32:12 +00001558
Chris Lattnerf9311a92008-08-05 06:19:09 +00001559 // We must manually skip to a ']', otherwise the expression skipper will
1560 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1561 // the enclosing expression.
1562 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001563 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001564 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001565
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001566 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001567 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001568 // We must manually skip to a ']', otherwise the expression skipper will
1569 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1570 // the enclosing expression.
1571 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001572 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001573 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001574
Chris Lattnered27a532008-01-25 18:59:06 +00001575 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redla2deb432008-12-13 15:32:12 +00001576
Steve Narofff9e80db2007-10-05 18:42:47 +00001577 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001578 if (nKeys == 0)
1579 KeyIdents.push_back(selIdent);
1580 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redla2deb432008-12-13 15:32:12 +00001581
Chris Lattnerd031a452007-10-07 02:00:24 +00001582 // We've just parsed a keyword message.
Sebastian Redla2deb432008-12-13 15:32:12 +00001583 if (ReceiverName)
1584 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonb42900d2009-02-14 18:21:46 +00001585 LBracLoc, NameLoc, SelectorLoc,
1586 RBracLoc,
Sebastian Redla2deb432008-12-13 15:32:12 +00001587 KeyExprs.take(), KeyExprs.size()));
1588 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonb42900d2009-02-14 18:21:46 +00001589 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redla2deb432008-12-13 15:32:12 +00001590 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001591}
1592
Sebastian Redla2deb432008-12-13 15:32:12 +00001593Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl39d4f022008-12-11 22:51:44 +00001594 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redla2deb432008-12-13 15:32:12 +00001595 if (Res.isInvalid()) return move(Res);
1596
Chris Lattnerddd3e632007-12-12 01:04:12 +00001597 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1598 // expressions. At this point, we know that the only valid thing that starts
1599 // with '@' is an @"".
1600 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001601 ExprVector AtStrings(Actions);
Chris Lattnerddd3e632007-12-12 01:04:12 +00001602 AtLocs.push_back(AtLoc);
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001603 AtStrings.push_back(Res.release());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001604
Chris Lattnerddd3e632007-12-12 01:04:12 +00001605 while (Tok.is(tok::at)) {
1606 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001607
Sebastian Redl62261042008-12-09 20:22:58 +00001608 // Invalid unless there is a string literal.
Chris Lattnerca7e5cb2009-02-18 05:56:09 +00001609 if (!isTokenStringLiteral())
1610 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerddd3e632007-12-12 01:04:12 +00001611
Chris Lattnerca7e5cb2009-02-18 05:56:09 +00001612 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001613 if (Lit.isInvalid())
Sebastian Redla2deb432008-12-13 15:32:12 +00001614 return move(Lit);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001615
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001616 AtStrings.push_back(Lit.release());
Chris Lattnerddd3e632007-12-12 01:04:12 +00001617 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001618
1619 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1620 AtStrings.size()));
Anders Carlssona66cad42007-08-21 17:43:55 +00001621}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001622
1623/// objc-encode-expression:
1624/// @encode ( type-name )
Sebastian Redla2deb432008-12-13 15:32:12 +00001625Parser::OwningExprResult
1626Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001627 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redla2deb432008-12-13 15:32:12 +00001628
Anders Carlsson8be1d402007-08-22 15:14:15 +00001629 SourceLocation EncLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001630
Chris Lattnerf9311a92008-08-05 06:19:09 +00001631 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001632 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1633
Anders Carlsson8be1d402007-08-22 15:14:15 +00001634 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla2deb432008-12-13 15:32:12 +00001635
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001636 TypeResult Ty = ParseTypeName();
Sebastian Redla2deb432008-12-13 15:32:12 +00001637
Anders Carlsson92faeb82007-08-23 15:31:37 +00001638 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redla2deb432008-12-13 15:32:12 +00001639
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001640 if (Ty.isInvalid())
1641 return ExprError();
1642
1643 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1644 Ty.get(), RParenLoc));
Anders Carlsson8be1d402007-08-22 15:14:15 +00001645}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001646
1647/// objc-protocol-expression
1648/// @protocol ( protocol-name )
Sebastian Redla2deb432008-12-13 15:32:12 +00001649Parser::OwningExprResult
1650Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001651 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001652
Chris Lattnerf9311a92008-08-05 06:19:09 +00001653 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001654 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1655
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001656 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla2deb432008-12-13 15:32:12 +00001657
Chris Lattnerf9311a92008-08-05 06:19:09 +00001658 if (Tok.isNot(tok::identifier))
Sebastian Redla2deb432008-12-13 15:32:12 +00001659 return ExprError(Diag(Tok, diag::err_expected_ident));
1660
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001661 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001662 ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001663
Anders Carlsson92faeb82007-08-23 15:31:37 +00001664 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001665
Sebastian Redla2deb432008-12-13 15:32:12 +00001666 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1667 LParenLoc, RParenLoc));
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001668}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001669
1670/// objc-selector-expression
1671/// @selector '(' objc-keyword-selector ')'
Sebastian Redla2deb432008-12-13 15:32:12 +00001672Parser::OwningExprResult
1673Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001674 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001675
Chris Lattnerf9311a92008-08-05 06:19:09 +00001676 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001677 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1678
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001679 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001680 SourceLocation LParenLoc = ConsumeParen();
1681 SourceLocation sLoc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001682 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redla2deb432008-12-13 15:32:12 +00001683 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1684 return ExprError(Diag(Tok, diag::err_expected_ident));
1685
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001686 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001687 unsigned nColons = 0;
1688 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001689 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001690 if (Tok.isNot(tok::colon))
Sebastian Redla2deb432008-12-13 15:32:12 +00001691 return ExprError(Diag(Tok, diag::err_expected_colon));
1692
Chris Lattner847f5c12007-12-27 19:57:00 +00001693 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001694 ConsumeToken(); // Eat the ':'.
1695 if (Tok.is(tok::r_paren))
1696 break;
1697 // Check for another keyword selector.
1698 SourceLocation Loc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001699 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001700 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001701 if (!SelIdent && Tok.isNot(tok::colon))
1702 break;
1703 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001704 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001705 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001706 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redla2deb432008-12-13 15:32:12 +00001707 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1708 LParenLoc, RParenLoc));
Gabor Greifa823dd12007-10-19 15:38:32 +00001709 }