blob: 42ec8572513f6a77b88b2dd1d969f7ece3bd9192 [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
Chris Lattner5261d0c2009-03-28 19:18:32 +0000161 DeclPtrTy CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000162 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000163 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner45142b92008-07-26 04:07:02 +0000164 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000165
166 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnera40577e2008-10-20 06:10:06 +0000167 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000168 }
169 // Parse a class interface.
170 IdentifierInfo *superClassId = 0;
171 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000172
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000173 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000174 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000177 return DeclPtrTy();
Steve Narofffb367882007-08-20 21:31:48 +0000178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000183 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Chris Lattnerae1ae492008-07-26 04:13:19 +0000184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000187 return DeclPtrTy();
Chris Lattnerae1ae492008-07-26 04:13:19 +0000188
Chris Lattner5261d0c2009-03-28 19:18:32 +0000189 DeclPtrTy ClsType =
Chris Lattnerae1ae492008-07-26 04:13:19 +0000190 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
191 superClassId, superClassLoc,
192 &ProtocolRefs[0], ProtocolRefs.size(),
193 EndProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000194
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000197
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnera40577e2008-10-20 06:10:06 +0000199 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000200}
201
202/// objc-interface-decl-list:
203/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000204/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000206/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000207/// objc-interface-decl-list declaration
208/// objc-interface-decl-list ';'
209///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000210/// objc-method-requirement: [OBJC2]
211/// @required
212/// @optional
213///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000214void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000215 tok::ObjCKeywordKind contextKey) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000216 llvm::SmallVector<DeclPtrTy, 32> allMethods;
217 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattnera17991f2009-03-29 16:50:03 +0000218 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000219 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000220
Chris Lattnera40577e2008-10-20 06:10:06 +0000221 SourceLocation AtEndLoc;
222
Steve Naroff0bbffd82007-08-22 16:35:03 +0000223 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000224 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000225 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000226 DeclPtrTy methodPrototype =
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000227 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000228 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000229 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
230 // method definitions.
Chris Lattnere50cb752009-02-15 22:24:30 +0000231 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
232 "", tok::semi);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 continue;
234 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000235
Chris Lattnere48b46b2008-10-20 05:46:22 +0000236 // Ignore excess semicolons.
237 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000239 continue;
240 }
241
Chris Lattnera40577e2008-10-20 06:10:06 +0000242 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000243 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000244 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000245
246 // If we don't have an @ directive, parse it as a function definition.
247 if (Tok.isNot(tok::at)) {
Chris Lattnerd4c2ec72009-01-09 04:34:13 +0000248 // The code below does not consume '}'s because it is afraid of eating the
249 // end of a namespace. Because of the way this code is structured, an
250 // erroneous r_brace would cause an infinite loop if not handled here.
251 if (Tok.is(tok::r_brace))
252 break;
253
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000254 // FIXME: as the name implies, this rule allows function definitions.
255 // We could pass a flag or check for functions during semantic analysis.
Chris Lattnera17991f2009-03-29 16:50:03 +0000256 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere48b46b2008-10-20 05:46:22 +0000257 continue;
258 }
259
260 // Otherwise, we have an @ directive, eat the @.
261 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000262 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000263
Chris Lattnercba730b2008-10-20 05:57:40 +0000264 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000265 AtEndLoc = AtLoc;
266 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000267 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000268
Chris Lattnera40577e2008-10-20 06:10:06 +0000269 // Eat the identifier.
270 ConsumeToken();
271
Chris Lattnercba730b2008-10-20 05:57:40 +0000272 switch (DirectiveKind) {
273 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000274 // FIXME: If someone forgets an @end on a protocol, this loop will
275 // continue to eat up tons of stuff and spew lots of nonsense errors. It
276 // would probably be better to bail out if we saw an @class or @interface
277 // or something like that.
Chris Lattner727fb1f2008-10-20 07:22:18 +0000278 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000279 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000280 SkipUntil(tok::r_brace, tok::at);
281 break;
282
283 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000284 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000285 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000286 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000287 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000288 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000289 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000290 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000291 break;
292
293 case tok::objc_property:
Chris Lattner727fb1f2008-10-20 07:22:18 +0000294 if (!getLang().ObjC2)
295 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
296
Chris Lattnere48b46b2008-10-20 05:46:22 +0000297 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000298 // Parse property attribute list, if any.
Chris Lattner22f9d262008-10-20 07:24:39 +0000299 if (Tok.is(tok::l_paren))
Chris Lattnere48b46b2008-10-20 05:46:22 +0000300 ParseObjCPropertyAttribute(OCDS);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000301
Chris Lattnere48b46b2008-10-20 05:46:22 +0000302 // Parse all the comma separated declarators.
303 DeclSpec DS;
304 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
305 ParseStructDeclaration(DS, FieldDeclarators);
306
Chris Lattner9019ae52008-10-20 06:15:13 +0000307 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
308 tok::at);
309
Chris Lattnere48b46b2008-10-20 05:46:22 +0000310 // Convert them all to property declarations.
311 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
312 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000313 if (FD.D.getIdentifier() == 0) {
Chris Lattner194e7002008-11-18 07:50:21 +0000314 Diag(AtLoc, diag::err_objc_property_requires_field_name)
315 << FD.D.getSourceRange();
Chris Lattnerbf16a972008-10-20 06:33:53 +0000316 continue;
317 }
Fariborz Jahanian8f314552009-01-17 23:21:10 +0000318 if (FD.BitfieldSize) {
319 Diag(AtLoc, diag::err_objc_property_bitfield)
320 << FD.D.getSourceRange();
321 continue;
322 }
Chris Lattnerbf16a972008-10-20 06:33:53 +0000323
Chris Lattnere48b46b2008-10-20 05:46:22 +0000324 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000325 IdentifierInfo *SelName =
326 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
327
Chris Lattnere48b46b2008-10-20 05:46:22 +0000328 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000329 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000330 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahaniand7c76682009-03-12 22:34:11 +0000331 Selector SetterSel;
332 if (SetterName)
333 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
334 else
335 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
336 PP.getSelectorTable(),
337 FD.D.getIdentifier());
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000338 bool isOverridingProperty = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000339 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
340 GetterSel, SetterSel,
341 interfaceDecl,
342 &isOverridingProperty,
343 MethodImplKind);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000344 if (!isOverridingProperty)
345 allProperties.push_back(Property);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000346 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000347 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000348 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000349 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000350
351 // We break out of the big loop in two cases: when we see @end or when we see
352 // EOF. In the former case, eat the @end. In the later case, emit an error.
353 if (Tok.isObjCAtKeyword(tok::objc_end))
354 ConsumeToken(); // the "end" identifier
355 else
356 Diag(Tok, diag::err_objc_missing_end);
357
Chris Lattnercba730b2008-10-20 05:57:40 +0000358 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000359 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000360 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Chris Lattnera17991f2009-03-29 16:50:03 +0000361 &allMethods[0], allMethods.size(),
362 &allProperties[0], allProperties.size(),
363 &allTUVariables[0], allTUVariables.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000364}
365
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000366/// Parse property attribute declarations.
367///
368/// property-attr-decl: '(' property-attrlist ')'
369/// property-attrlist:
370/// property-attribute
371/// property-attrlist ',' property-attribute
372/// property-attribute:
373/// getter '=' identifier
374/// setter '=' identifier ':'
375/// readonly
376/// readwrite
377/// assign
378/// retain
379/// copy
380/// nonatomic
381///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000382void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner22f9d262008-10-20 07:24:39 +0000383 assert(Tok.getKind() == tok::l_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000384 SourceLocation LHSLoc = ConsumeParen(); // consume '('
385
Chris Lattner1e5cc722008-10-20 07:15:22 +0000386 while (1) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000387 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner727fb1f2008-10-20 07:22:18 +0000388
389 // If this is not an identifier at all, bail out early.
390 if (II == 0) {
391 MatchRHSPunctuation(tok::r_paren, LHSLoc);
392 return;
393 }
394
Chris Lattner9ba0b222008-10-20 07:37:22 +0000395 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
396
Chris Lattner05fb7c82008-11-20 04:42:34 +0000397 if (II->isStr("readonly"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000398 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000399 else if (II->isStr("assign"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000400 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000401 else if (II->isStr("readwrite"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000403 else if (II->isStr("retain"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000404 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000405 else if (II->isStr("copy"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000406 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000407 else if (II->isStr("nonatomic"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000409 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner2cc2b872008-10-20 07:39:53 +0000410 // getter/setter require extra treatment.
Chris Lattner9ba0b222008-10-20 07:37:22 +0000411 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
412 tok::r_paren))
Chris Lattner35cd4b92008-10-20 07:00:43 +0000413 return;
Chris Lattner9ba0b222008-10-20 07:37:22 +0000414
Chris Lattner22f9d262008-10-20 07:24:39 +0000415 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000416 Diag(Tok, diag::err_expected_ident);
Chris Lattner22f9d262008-10-20 07:24:39 +0000417 SkipUntil(tok::r_paren);
418 return;
419 }
420
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000421 if (II->getName()[0] == 's') {
Chris Lattner22f9d262008-10-20 07:24:39 +0000422 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
423 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000424 ConsumeToken(); // consume method name
425
426 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
427 tok::r_paren))
Chris Lattner22f9d262008-10-20 07:24:39 +0000428 return;
Chris Lattner22f9d262008-10-20 07:24:39 +0000429 } else {
430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
431 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000432 ConsumeToken(); // consume method name
Chris Lattner22f9d262008-10-20 07:24:39 +0000433 }
Chris Lattner2cc2b872008-10-20 07:39:53 +0000434 } else {
Chris Lattner5e58ac22008-11-19 07:49:38 +0000435 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000436 SkipUntil(tok::r_paren);
437 return;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000438 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000439
Chris Lattner9ba0b222008-10-20 07:37:22 +0000440 if (Tok.isNot(tok::comma))
441 break;
Chris Lattner35cd4b92008-10-20 07:00:43 +0000442
Chris Lattner9ba0b222008-10-20 07:37:22 +0000443 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000444 }
Chris Lattner9ba0b222008-10-20 07:37:22 +0000445
446 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000447}
448
Steve Naroff81f1bba2007-09-06 21:24:23 +0000449/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000450/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000451/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000452///
453/// objc-instance-method: '-'
454/// objc-class-method: '+'
455///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000456/// objc-method-attributes: [OBJC2]
457/// __attribute__((deprecated))
458///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000459Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
460 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000461 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000462
463 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000464 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000465
Chris Lattner5261d0c2009-03-28 19:18:32 +0000466 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000467 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000468 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000469 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470}
471
472/// objc-selector:
473/// identifier
474/// one of
475/// enum struct union if else while do for switch case default
476/// break continue return goto asm sizeof typeof __alignof
477/// unsigned long const short volatile signed restrict _Complex
478/// in out inout bycopy byref oneway int char float double void _Bool
479///
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +0000480IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000481 switch (Tok.getKind()) {
482 default:
483 return 0;
484 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000485 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000486 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000487 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000488 case tok::kw_break:
489 case tok::kw_case:
490 case tok::kw_catch:
491 case tok::kw_char:
492 case tok::kw_class:
493 case tok::kw_const:
494 case tok::kw_const_cast:
495 case tok::kw_continue:
496 case tok::kw_default:
497 case tok::kw_delete:
498 case tok::kw_do:
499 case tok::kw_double:
500 case tok::kw_dynamic_cast:
501 case tok::kw_else:
502 case tok::kw_enum:
503 case tok::kw_explicit:
504 case tok::kw_export:
505 case tok::kw_extern:
506 case tok::kw_false:
507 case tok::kw_float:
508 case tok::kw_for:
509 case tok::kw_friend:
510 case tok::kw_goto:
511 case tok::kw_if:
512 case tok::kw_inline:
513 case tok::kw_int:
514 case tok::kw_long:
515 case tok::kw_mutable:
516 case tok::kw_namespace:
517 case tok::kw_new:
518 case tok::kw_operator:
519 case tok::kw_private:
520 case tok::kw_protected:
521 case tok::kw_public:
522 case tok::kw_register:
523 case tok::kw_reinterpret_cast:
524 case tok::kw_restrict:
525 case tok::kw_return:
526 case tok::kw_short:
527 case tok::kw_signed:
528 case tok::kw_sizeof:
529 case tok::kw_static:
530 case tok::kw_static_cast:
531 case tok::kw_struct:
532 case tok::kw_switch:
533 case tok::kw_template:
534 case tok::kw_this:
535 case tok::kw_throw:
536 case tok::kw_true:
537 case tok::kw_try:
538 case tok::kw_typedef:
539 case tok::kw_typeid:
540 case tok::kw_typename:
541 case tok::kw_typeof:
542 case tok::kw_union:
543 case tok::kw_unsigned:
544 case tok::kw_using:
545 case tok::kw_virtual:
546 case tok::kw_void:
547 case tok::kw_volatile:
548 case tok::kw_wchar_t:
549 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000550 case tok::kw__Bool:
551 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000552 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000553 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000554 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000555 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000556 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000557}
558
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000559/// objc-for-collection-in: 'in'
560///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000561bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000562 // FIXME: May have to do additional look-ahead to only allow for
563 // valid tokens following an 'in'; such as an identifier, unary operators,
564 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000565 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000566 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000567}
568
Ted Kremenek42730c52008-01-07 19:49:32 +0000569/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000570/// qualifier list and builds their bitmask representation in the input
571/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000572///
573/// objc-type-qualifiers:
574/// objc-type-qualifier
575/// objc-type-qualifiers objc-type-qualifier
576///
Ted Kremenek42730c52008-01-07 19:49:32 +0000577void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000578 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000579 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000580 return;
581
582 const IdentifierInfo *II = Tok.getIdentifierInfo();
583 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000585 continue;
586
Ted Kremenek42730c52008-01-07 19:49:32 +0000587 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000588 switch (i) {
589 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000590 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
591 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
592 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
593 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
594 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
595 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000596 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000598 ConsumeToken();
599 II = 0;
600 break;
601 }
602
603 // If this wasn't a recognized qualifier, bail out.
604 if (II) return;
605 }
606}
607
608/// objc-type-name:
609/// '(' objc-type-qualifiers[opt] type-name ')'
610/// '(' objc-type-qualifiers[opt] ')'
611///
Ted Kremenek42730c52008-01-07 19:49:32 +0000612Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000613 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000614
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000615 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnerb5769332008-08-23 01:48:03 +0000616 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000617
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000618 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000620
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000621 TypeTy *Ty = 0;
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000622 if (isTypeSpecifierQualifier()) {
623 TypeResult TypeSpec = ParseTypeName();
624 if (!TypeSpec.isInvalid())
625 Ty = TypeSpec.get();
626 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000627
Steve Naroffc6235e82008-10-21 14:15:04 +0000628 if (Tok.is(tok::r_paren))
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000629 ConsumeParen();
630 else if (Tok.getLocation() == TypeStartLoc) {
631 // If we didn't eat any tokens, then this isn't a type.
Chris Lattnerf006a222008-11-18 07:48:38 +0000632 Diag(Tok, diag::err_expected_type);
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000633 SkipUntil(tok::r_paren);
634 } else {
635 // Otherwise, we found *something*, but didn't get a ')' in the right
636 // place. Emit an error then return what we have as the type.
637 MatchRHSPunctuation(tok::r_paren, LParenLoc);
638 }
Steve Naroff304ed392007-09-05 23:30:30 +0000639 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000640}
641
642/// objc-method-decl:
643/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000644/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000645/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000646/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000647///
648/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000649/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000650/// objc-keyword-selector objc-keyword-decl
651///
652/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000653/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
654/// objc-selector ':' objc-keyword-attributes[opt] identifier
655/// ':' objc-type-name objc-keyword-attributes[opt] identifier
656/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000657///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000658/// objc-parmlist:
659/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000660///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000661/// objc-parms:
662/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000663///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000664/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000665/// , ...
666///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000667/// objc-keyword-attributes: [OBJC2]
668/// __attribute__((unused))
669///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000670Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
671 tok::TokenKind mType,
672 DeclPtrTy IDecl,
673 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000674 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000675 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000676 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000677 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000678 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000679
Steve Naroff3774dd92007-10-26 20:53:56 +0000680 SourceLocation selLoc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +0000681 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000682
Steve Naroff1a781092009-02-11 20:43:13 +0000683 // An unnamed colon is valid.
684 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattnerf006a222008-11-18 07:48:38 +0000685 Diag(Tok, diag::err_expected_selector_for_method)
686 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnerb5769332008-08-23 01:48:03 +0000687 // Skip until we get a ; or {}.
688 SkipUntil(tok::r_brace);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000689 return DeclPtrTy();
Chris Lattnerb5769332008-08-23 01:48:03 +0000690 }
691
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000692 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000693 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000694 // If attributes exist after the method, parse them.
695 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000696 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000697 MethodAttrs = ParseAttributes();
698
699 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000700 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000701 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000702 0, 0, 0, CargNames,
703 MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 }
Steve Naroff304ed392007-09-05 23:30:30 +0000705
Steve Naroff4ed9d662007-09-27 14:38:14 +0000706 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
707 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000708 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000709 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000710
711 Action::TypeTy *TypeInfo;
712 while (1) {
713 KeyIdents.push_back(SelIdent);
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 ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000721 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000722 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000723 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000724 else
725 TypeInfo = 0;
726 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000727 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000728
Chris Lattnerd031a452007-10-07 02:00:24 +0000729 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000730 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000731 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000732
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000733 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000734 Diag(Tok, diag::err_expected_ident); // missing argument name.
735 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000736 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000737 ArgNames.push_back(Tok.getIdentifierInfo());
738 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000739
Chris Lattnerd031a452007-10-07 02:00:24 +0000740 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000741 SourceLocation Loc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +0000742 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000743 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000744 break;
745 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000746 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000747
Steve Naroff29fe7462007-11-15 12:35:21 +0000748 bool isVariadic = false;
749
Chris Lattnerd031a452007-10-07 02:00:24 +0000750 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000751 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000752 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000753 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000754 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000755 ConsumeToken();
756 break;
757 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000758 DeclSpec DS;
759 ParseDeclarationSpecifiers(DS);
760 // Parse the declarator.
761 Declarator ParmDecl(DS, Declarator::PrototypeContext);
762 ParseDeclarator(ParmDecl);
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000763 CargNames.push_back(ParmDecl);
Chris Lattnerd031a452007-10-07 02:00:24 +0000764 }
765
766 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000767 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000768 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000769 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000770 MethodAttrs = ParseAttributes();
771
772 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
773 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000774 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000775 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000776 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000777 &ArgNames[0], CargNames,
778 MethodAttrs,
Steve Naroff29fe7462007-11-15 12:35:21 +0000779 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000780}
781
Steve Narofffb367882007-08-20 21:31:48 +0000782/// objc-protocol-refs:
783/// '<' identifier-list '>'
784///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000785bool Parser::
Chris Lattner5261d0c2009-03-28 19:18:32 +0000786ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Chris Lattner2bdedd62008-07-26 04:03:38 +0000787 bool WarnOnDeclarations, SourceLocation &EndLoc) {
788 assert(Tok.is(tok::less) && "expected <");
789
790 ConsumeToken(); // the "<"
791
792 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
793
794 while (1) {
795 if (Tok.isNot(tok::identifier)) {
796 Diag(Tok, diag::err_expected_ident);
797 SkipUntil(tok::greater);
798 return true;
799 }
800 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
801 Tok.getLocation()));
802 ConsumeToken();
803
804 if (Tok.isNot(tok::comma))
805 break;
806 ConsumeToken();
807 }
808
809 // Consume the '>'.
810 if (Tok.isNot(tok::greater)) {
811 Diag(Tok, diag::err_expected_greater);
812 return true;
813 }
814
815 EndLoc = ConsumeAnyToken();
816
817 // Convert the list of protocols identifiers into a list of protocol decls.
818 Actions.FindProtocolDeclaration(WarnOnDeclarations,
819 &ProtocolIdents[0], ProtocolIdents.size(),
820 Protocols);
821 return false;
822}
823
Steve Narofffb367882007-08-20 21:31:48 +0000824/// objc-class-instance-variables:
825/// '{' objc-instance-variable-decl-list[opt] '}'
826///
827/// objc-instance-variable-decl-list:
828/// objc-visibility-spec
829/// objc-instance-variable-decl ';'
830/// ';'
831/// objc-instance-variable-decl-list objc-visibility-spec
832/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
833/// objc-instance-variable-decl-list ';'
834///
835/// objc-visibility-spec:
836/// @private
837/// @protected
838/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000839/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000840///
841/// objc-instance-variable-decl:
842/// struct-declaration
843///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000844void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000845 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000846 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5261d0c2009-03-28 19:18:32 +0000847 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000848 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
849
Douglas Gregor5eca99e2009-01-12 18:45:55 +0000850 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +0000851
Steve Naroffc4474992007-08-21 21:17:12 +0000852 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000853
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000854 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000855 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000856 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000857 // Each iteration of this loop reads one objc-instance-variable-decl.
858
859 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000860 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000861 Diag(Tok, diag::ext_extra_struct_semi);
862 ConsumeToken();
863 continue;
864 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000865
Steve Naroffc4474992007-08-21 21:17:12 +0000866 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000867 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000868 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000869 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000870 case tok::objc_private:
871 case tok::objc_public:
872 case tok::objc_protected:
873 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000874 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000875 ConsumeToken();
876 continue;
877 default:
878 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000879 continue;
880 }
881 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000882
883 // Parse all the comma separated declarators.
884 DeclSpec DS;
885 FieldDeclarators.clear();
886 ParseStructDeclaration(DS, FieldDeclarators);
887
888 // Convert them all to fields.
889 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
890 FieldDeclarator &FD = FieldDeclarators[i];
891 // Install the declarator into interfaceDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000892 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
893 DS.getSourceRange().getBegin(),
894 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000895 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000896 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000897
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000898 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000899 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000900 } else {
901 Diag(Tok, diag::err_expected_semi_decl_list);
902 // Skip to end of block or statement
903 SkipUntil(tok::r_brace, true, true);
904 }
905 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000906 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000907 // Call ActOnFields() even if we don't have any decls. This is useful
908 // for code rewriting tools that need to be aware of the empty list.
909 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
910 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000911 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000912 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000913}
Steve Narofffb367882007-08-20 21:31:48 +0000914
915/// objc-protocol-declaration:
916/// objc-protocol-definition
917/// objc-protocol-forward-reference
918///
919/// objc-protocol-definition:
920/// @protocol identifier
921/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000922/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000923/// @end
924///
925/// objc-protocol-forward-reference:
926/// @protocol identifier-list ';'
927///
928/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000929/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000930/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000931Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
932 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000933 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000934 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
935 ConsumeToken(); // the "protocol" identifier
936
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000937 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000938 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000939 return DeclPtrTy();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000940 }
941 // Save the protocol name, then consume it.
942 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
943 SourceLocation nameLoc = ConsumeToken();
944
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000945 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000946 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000947 ConsumeToken();
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000948 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
949 attrList);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000950 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000951
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000952 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000953 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
954 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
955
Steve Naroff72f17fb2007-08-22 22:17:26 +0000956 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000957 while (1) {
958 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000959 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000960 Diag(Tok, diag::err_expected_ident);
961 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000962 return DeclPtrTy();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000963 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000964 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
965 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000966 ConsumeToken(); // the identifier
967
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000968 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000969 break;
970 }
971 // Consume the ';'.
972 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000973 return DeclPtrTy();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000974
Steve Naroff415c1832007-10-10 17:32:04 +0000975 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000976 &ProtocolRefs[0],
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000977 ProtocolRefs.size(),
978 attrList);
Chris Lattnere705e5e2008-07-21 22:17:28 +0000979 }
980
Steve Naroff72f17fb2007-08-22 22:17:26 +0000981 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000982 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000983
Chris Lattner5261d0c2009-03-28 19:18:32 +0000984 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000985 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000986 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000987 return DeclPtrTy();
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000988
Chris Lattner5261d0c2009-03-28 19:18:32 +0000989 DeclPtrTy ProtoType =
Chris Lattner2bdedd62008-07-26 04:03:38 +0000990 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
991 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000992 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000993 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000994 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +0000995}
Steve Narofffb367882007-08-20 21:31:48 +0000996
997/// objc-implementation:
998/// objc-class-implementation-prologue
999/// objc-category-implementation-prologue
1000///
1001/// objc-class-implementation-prologue:
1002/// @implementation identifier objc-superclass[opt]
1003/// objc-class-instance-variables[opt]
1004///
1005/// objc-category-implementation-prologue:
1006/// @implementation identifier ( identifier )
Chris Lattner5261d0c2009-03-28 19:18:32 +00001007Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001008 SourceLocation atLoc) {
1009 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1010 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1011 ConsumeToken(); // the "implementation" identifier
1012
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001013 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001015 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001016 }
1017 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001018 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001019 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1020
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001021 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001022 // we have a category implementation.
1023 SourceLocation lparenLoc = ConsumeParen();
1024 SourceLocation categoryLoc, rparenLoc;
1025 IdentifierInfo *categoryId = 0;
1026
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001027 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001028 categoryId = Tok.getIdentifierInfo();
1029 categoryLoc = ConsumeToken();
1030 } else {
1031 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001032 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001033 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001034 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001035 Diag(Tok, diag::err_expected_rparen);
1036 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner5261d0c2009-03-28 19:18:32 +00001037 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001038 }
1039 rparenLoc = ConsumeParen();
Chris Lattner5261d0c2009-03-28 19:18:32 +00001040 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001041 atLoc, nameId, nameLoc, categoryId,
1042 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001043 ObjCImpDecl = ImplCatType;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001044 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001045 }
1046 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001047 SourceLocation superClassLoc;
1048 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001049 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001050 // We have a super class
1051 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001052 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001053 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001054 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001055 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001056 superClassId = Tok.getIdentifierInfo();
1057 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001058 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001059 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001060 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001061 superClassId, superClassLoc);
1062
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001063 if (Tok.is(tok::l_brace)) // we have ivars
1064 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001065 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001066
Chris Lattner5261d0c2009-03-28 19:18:32 +00001067 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +00001068}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001069
Chris Lattner5261d0c2009-03-28 19:18:32 +00001070Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001071 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1072 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner5261d0c2009-03-28 19:18:32 +00001073 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001074 ConsumeToken(); // the "end" identifier
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +00001075 if (ObjCImpDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001076 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001077 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +00001078 }
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001079 else
1080 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +00001081 return Result;
Steve Narofffb367882007-08-20 21:31:48 +00001082}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001083
1084/// compatibility-alias-decl:
1085/// @compatibility_alias alias-name class-name ';'
1086///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001087Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001088 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1089 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1090 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001092 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001093 return DeclPtrTy();
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001094 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001095 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1096 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001097 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001098 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001099 return DeclPtrTy();
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001100 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001101 IdentifierInfo *classId = Tok.getIdentifierInfo();
1102 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1103 if (Tok.isNot(tok::semi)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001104 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001105 return DeclPtrTy();
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001106 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001107 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1108 classId, classLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001109}
1110
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001111/// property-synthesis:
1112/// @synthesize property-ivar-list ';'
1113///
1114/// property-ivar-list:
1115/// property-ivar
1116/// property-ivar-list ',' property-ivar
1117///
1118/// property-ivar:
1119/// identifier
1120/// identifier '=' identifier
1121///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001122Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001123 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1124 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001125 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001126 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001127 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001128 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001129 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001130
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001131 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 IdentifierInfo *propertyIvar = 0;
1133 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1134 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001135 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001136 // property '=' ivar-name
1137 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001138 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001139 Diag(Tok, diag::err_expected_ident);
1140 break;
1141 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001142 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001143 ConsumeToken(); // consume ivar-name
1144 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001145 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1146 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001147 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001148 break;
1149 ConsumeToken(); // consume ','
1150 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001152 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001153 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +00001154}
1155
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001156/// property-dynamic:
1157/// @dynamic property-list
1158///
1159/// property-list:
1160/// identifier
1161/// property-list ',' identifier
1162///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001163Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001164 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1165 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1166 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001168 Diag(Tok, diag::err_expected_ident);
Chris Lattner5261d0c2009-03-28 19:18:32 +00001169 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001170 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001171 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001172 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1173 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1174 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1175 propertyId, 0);
1176
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001177 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001178 break;
1179 ConsumeToken(); // consume ','
1180 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001182 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner5261d0c2009-03-28 19:18:32 +00001183 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001184}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001185
1186/// objc-throw-statement:
1187/// throw expression[opt];
1188///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001189Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl62261042008-12-09 20:22:58 +00001190 OwningExprResult Res(Actions);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001191 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001192 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001193 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001194 if (Res.isInvalid()) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001195 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001196 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001197 }
1198 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001199 ConsumeToken(); // consume ';'
Steve Naroff590fe482009-02-11 20:05:44 +00001200 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001201}
1202
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001203/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001204/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001205///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001206Parser::OwningStmtResult
1207Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001208 ConsumeToken(); // consume synchronized
1209 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001210 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl15bf1452008-12-11 20:12:42 +00001211 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001212 }
1213 ConsumeParen(); // '('
Sebastian Redl14ca7412008-12-11 21:36:32 +00001214 OwningExprResult Res(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001215 if (Res.isInvalid()) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001216 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001217 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001218 }
1219 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001220 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001221 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001222 }
1223 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001224 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001225 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001226 return StmtError();
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001227 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001228 // Enter a scope to hold everything within the compound stmt. Compound
1229 // statements can always hold declarations.
Douglas Gregor95d40792008-12-10 06:34:36 +00001230 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff70337ac2008-06-04 20:36:13 +00001231
Sebastian Redl10c32952008-12-11 19:30:53 +00001232 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001233
Douglas Gregor95d40792008-12-10 06:34:36 +00001234 BodyScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001235 if (SynchBody.isInvalid())
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001236 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl81db6682009-02-05 15:02:23 +00001237 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001238}
1239
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001240/// objc-try-catch-statement:
1241/// @try compound-statement objc-catch-list[opt]
1242/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1243///
1244/// objc-catch-list:
1245/// @catch ( parameter-declaration ) compound-statement
1246/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1247/// catch-parameter-declaration:
1248/// parameter-declaration
1249/// '...' [OBJC2]
1250///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001251Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001252 bool catch_or_finally_seen = false;
Sebastian Redl15bf1452008-12-11 20:12:42 +00001253
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001254 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001255 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001256 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001257 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001258 }
Sebastian Redl62261042008-12-09 20:22:58 +00001259 OwningStmtResult CatchStmts(Actions);
1260 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor95d40792008-12-10 06:34:36 +00001261 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl10c32952008-12-11 19:30:53 +00001262 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor95d40792008-12-10 06:34:36 +00001263 TryScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001264 if (TryBody.isInvalid())
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001265 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl6008ac32008-11-25 22:21:31 +00001266
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001267 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001268 // At this point, we need to lookahead to determine if this @ is the start
1269 // of an @catch or @finally. We don't want to consume the @ token if this
1270 // is an @try or @encode or something else.
1271 Token AfterAt = GetLookAheadToken(1);
1272 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1273 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1274 break;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001275
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001276 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001277 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner5261d0c2009-03-28 19:18:32 +00001278 DeclPtrTy FirstPart;
Fariborz Jahanian06798362007-11-01 23:59:59 +00001279 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001280 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001281 ConsumeParen();
Steve Naroff590fe482009-02-11 20:05:44 +00001282 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001283 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001284 DeclSpec DS;
1285 ParseDeclarationSpecifiers(DS);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001286 // For some odd reason, the name of the exception variable is
Steve Naroff0e8b96a2009-03-03 19:52:17 +00001287 // optional. As a result, we need to use "PrototypeContext", because
1288 // we must accept either 'declarator' or 'abstract-declarator' here.
1289 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1290 ParseDeclarator(ParmDecl);
1291
1292 // Inform the actions module about the parameter declarator, so it
1293 // gets added to the current scope.
1294 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffc949a462008-02-05 21:27:35 +00001295 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001296 ConsumeToken(); // consume '...'
Steve Naroff61e613c2009-04-07 22:56:58 +00001297
1298 SourceLocation RParenLoc;
Chris Lattner8027be62008-02-14 19:27:54 +00001299
Steve Naroff61e613c2009-04-07 22:56:58 +00001300 if (Tok.is(tok::r_paren))
1301 RParenLoc = ConsumeParen();
1302 else // Skip over garbage, until we get to ')'. Eat the ')'.
1303 SkipUntil(tok::r_paren, true, false);
1304
Sebastian Redl62261042008-12-09 20:22:58 +00001305 OwningStmtResult CatchBody(Actions, true);
Chris Lattner8027be62008-02-14 19:27:54 +00001306 if (Tok.is(tok::l_brace))
1307 CatchBody = ParseCompoundStatementBody();
1308 else
1309 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001310 if (CatchBody.isInvalid())
Fariborz Jahanian06798362007-11-01 23:59:59 +00001311 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001312 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff0e8b96a2009-03-03 19:52:17 +00001313 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl81db6682009-02-05 15:02:23 +00001314 move(CatchStmts));
Steve Naroffc949a462008-02-05 21:27:35 +00001315 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +00001316 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1317 << "@catch clause";
Sebastian Redl15bf1452008-12-11 20:12:42 +00001318 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001319 }
1320 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001321 } else {
1322 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001323 ConsumeToken(); // consume finally
Douglas Gregor95d40792008-12-10 06:34:36 +00001324 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001325
Sebastian Redl62261042008-12-09 20:22:58 +00001326 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner8027be62008-02-14 19:27:54 +00001327 if (Tok.is(tok::l_brace))
1328 FinallyBody = ParseCompoundStatementBody();
1329 else
1330 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001331 if (FinallyBody.isInvalid())
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001332 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001333 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001334 move(FinallyBody));
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001335 catch_or_finally_seen = true;
1336 break;
1337 }
1338 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001339 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001340 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001341 return StmtError();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001342 }
Sebastian Redl81db6682009-02-05 15:02:23 +00001343 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1344 move(FinallyStmt));
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001345}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001346
Steve Naroff81f1bba2007-09-06 21:24:23 +00001347/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001348///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001349Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1350 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner9a9936f2009-03-05 02:03:49 +00001351
Chris Lattnerc309ade2009-03-05 08:00:35 +00001352 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1353 PP.getSourceManager(),
1354 "parsing Objective-C method");
Chris Lattner9a9936f2009-03-05 02:03:49 +00001355
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001356 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001357 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001358 ConsumeToken();
1359
Steve Naroff9191a9e82007-11-11 19:54:21 +00001360 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001361 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001362 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001363
1364 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1365 SkipUntil(tok::l_brace, true, true);
1366
1367 // If we didn't find the '{', bail out.
1368 if (Tok.isNot(tok::l_brace))
Chris Lattner5261d0c2009-03-28 19:18:32 +00001369 return DeclPtrTy();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001370 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001371 SourceLocation BraceLoc = Tok.getLocation();
1372
1373 // Enter a scope for the method body.
Douglas Gregor95d40792008-12-10 06:34:36 +00001374 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001375
1376 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001377 // specified Declarator for the method.
Steve Naroff8df46022009-02-28 16:59:13 +00001378 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl10c32952008-12-11 19:30:53 +00001379
1380 OwningStmtResult FnBody(ParseCompoundStatementBody());
1381
Steve Naroff9191a9e82007-11-11 19:54:21 +00001382 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001383 if (FnBody.isInvalid())
Sebastian Redl76b9ddb2008-12-21 12:04:03 +00001384 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1385 MultiStmtArg(Actions), false);
Sebastian Redl91f9b0a2008-12-13 16:23:55 +00001386
Steve Naroffb50441b2009-03-02 22:00:56 +00001387 // TODO: Pass argument information.
1388 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1389
Steve Naroff9191a9e82007-11-11 19:54:21 +00001390 // Leave the function body scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001391 BodyScope.Exit();
Sebastian Redl91f9b0a2008-12-13 16:23:55 +00001392
Steve Naroff18c83382007-11-13 23:01:27 +00001393 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001394}
Anders Carlssona66cad42007-08-21 17:43:55 +00001395
Sebastian Redl15bf1452008-12-11 20:12:42 +00001396Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffc949a462008-02-05 21:27:35 +00001397 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001398 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001399 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1400 return ParseObjCThrowStmt(AtLoc);
1401 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1402 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001403 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001404 if (Res.isInvalid()) {
Steve Naroffc949a462008-02-05 21:27:35 +00001405 // If the expression is invalid, skip ahead to the next semicolon. Not
1406 // doing this opens us up to the possibility of infinite loops if
1407 // ParseExpression does not consume any tokens.
1408 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001409 return StmtError();
Steve Naroffc949a462008-02-05 21:27:35 +00001410 }
1411 // Otherwise, eat the semicolon.
1412 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl81db6682009-02-05 15:02:23 +00001413 return Actions.ActOnExprStmt(move(Res));
Steve Naroffc949a462008-02-05 21:27:35 +00001414}
1415
Sebastian Redla2deb432008-12-13 15:32:12 +00001416Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001417 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001418 case tok::string_literal: // primary-expression: string-literal
1419 case tok::wide_string_literal:
Sebastian Redla2deb432008-12-13 15:32:12 +00001420 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerddd3e632007-12-12 01:04:12 +00001421 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001422 if (Tok.getIdentifierInfo() == 0)
Sebastian Redla2deb432008-12-13 15:32:12 +00001423 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl14ca7412008-12-11 21:36:32 +00001424
Chris Lattnerf9311a92008-08-05 06:19:09 +00001425 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1426 case tok::objc_encode:
Sebastian Redla2deb432008-12-13 15:32:12 +00001427 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001428 case tok::objc_protocol:
Sebastian Redla2deb432008-12-13 15:32:12 +00001429 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001430 case tok::objc_selector:
Sebastian Redla2deb432008-12-13 15:32:12 +00001431 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001432 default:
Sebastian Redla2deb432008-12-13 15:32:12 +00001433 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001434 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001435 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001436}
1437
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001438/// objc-message-expr:
1439/// '[' objc-receiver objc-message-args ']'
1440///
1441/// objc-receiver:
1442/// expression
1443/// class-name
1444/// type-name
Sebastian Redla2deb432008-12-13 15:32:12 +00001445Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnered27a532008-01-25 18:59:06 +00001446 assert(Tok.is(tok::l_square) && "'[' expected");
1447 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1448
1449 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001450 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001451 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanian00740212009-04-08 19:50:10 +00001452 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1453 SourceLocation NameLoc = ConsumeToken();
1454 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1455 ExprArg(Actions));
1456 }
Chris Lattnered27a532008-01-25 18:59:06 +00001457 }
1458
Sebastian Redl14ca7412008-12-11 21:36:32 +00001459 OwningExprResult Res(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001460 if (Res.isInvalid()) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001461 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001462 return move(Res);
Chris Lattnered27a532008-01-25 18:59:06 +00001463 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001464
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001465 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +00001466 0, move(Res));
Chris Lattnered27a532008-01-25 18:59:06 +00001467}
Sebastian Redla2deb432008-12-13 15:32:12 +00001468
Chris Lattnered27a532008-01-25 18:59:06 +00001469/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1470/// the rest of a message expression.
Sebastian Redla2deb432008-12-13 15:32:12 +00001471///
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001472/// objc-message-args:
1473/// objc-selector
1474/// objc-keywordarg-list
1475///
1476/// objc-keywordarg-list:
1477/// objc-keywordarg
1478/// objc-keywordarg-list objc-keywordarg
1479///
1480/// objc-keywordarg:
1481/// selector-name[opt] ':' objc-keywordexpr
1482///
1483/// objc-keywordexpr:
1484/// nonempty-expr-list
1485///
1486/// nonempty-expr-list:
1487/// assignment-expression
1488/// nonempty-expr-list , assignment-expression
Sebastian Redla2deb432008-12-13 15:32:12 +00001489///
1490Parser::OwningExprResult
Chris Lattnered27a532008-01-25 18:59:06 +00001491Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +00001492 SourceLocation NameLoc,
Chris Lattnered27a532008-01-25 18:59:06 +00001493 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-12-13 15:32:12 +00001494 ExprArg ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001495 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001496 SourceLocation Loc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001497 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001498
Anders Carlssonb42900d2009-02-14 18:21:46 +00001499 SourceLocation SelectorLoc = Loc;
1500
Steve Naroff4ed9d662007-09-27 14:38:14 +00001501 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001502 ExprVector KeyExprs(Actions);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001503
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001504 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001505 while (1) {
1506 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001507 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001508
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001509 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001510 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001511 // We must manually skip to a ']', otherwise the expression skipper will
1512 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1513 // the enclosing expression.
1514 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001515 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001516 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001517
Steve Naroff4ed9d662007-09-27 14:38:14 +00001518 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001519 /// Parse the expression after ':'
Sebastian Redl14ca7412008-12-11 21:36:32 +00001520 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001521 if (Res.isInvalid()) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001522 // We must manually skip to a ']', otherwise the expression skipper will
1523 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1524 // the enclosing expression.
1525 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001526 return move(Res);
Steve Naroff253118b2007-09-17 20:25:27 +00001527 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001528
Steve Naroff253118b2007-09-17 20:25:27 +00001529 // We have a valid expression.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001530 KeyExprs.push_back(Res.release());
Sebastian Redla2deb432008-12-13 15:32:12 +00001531
Steve Naroff253118b2007-09-17 20:25:27 +00001532 // Check for another keyword selector.
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001533 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001534 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001535 break;
1536 // We have a selector or a colon, continue parsing.
1537 }
1538 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001539 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001540 ConsumeToken(); // Eat the ','.
1541 /// Parse the expression after ','
Sebastian Redl14ca7412008-12-11 21:36:32 +00001542 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001543 if (Res.isInvalid()) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001544 // We must manually skip to a ']', otherwise the expression skipper will
1545 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1546 // the enclosing expression.
1547 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001548 return move(Res);
Steve Naroff9f176d12007-11-15 13:05:42 +00001549 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001550
Steve Naroff9f176d12007-11-15 13:05:42 +00001551 // We have a valid expression.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001552 KeyExprs.push_back(Res.release());
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001553 }
1554 } else if (!selIdent) {
1555 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redla2deb432008-12-13 15:32:12 +00001556
Chris Lattnerf9311a92008-08-05 06:19:09 +00001557 // We must manually skip to a ']', otherwise the expression skipper will
1558 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1559 // the enclosing expression.
1560 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001561 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001562 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001563
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001564 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001565 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001566 // We must manually skip to a ']', otherwise the expression skipper will
1567 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1568 // the enclosing expression.
1569 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001570 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001571 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001572
Chris Lattnered27a532008-01-25 18:59:06 +00001573 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redla2deb432008-12-13 15:32:12 +00001574
Steve Narofff9e80db2007-10-05 18:42:47 +00001575 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001576 if (nKeys == 0)
1577 KeyIdents.push_back(selIdent);
1578 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redla2deb432008-12-13 15:32:12 +00001579
Chris Lattnerd031a452007-10-07 02:00:24 +00001580 // We've just parsed a keyword message.
Sebastian Redla2deb432008-12-13 15:32:12 +00001581 if (ReceiverName)
1582 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonb42900d2009-02-14 18:21:46 +00001583 LBracLoc, NameLoc, SelectorLoc,
1584 RBracLoc,
Sebastian Redla2deb432008-12-13 15:32:12 +00001585 KeyExprs.take(), KeyExprs.size()));
1586 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonb42900d2009-02-14 18:21:46 +00001587 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redla2deb432008-12-13 15:32:12 +00001588 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001589}
1590
Sebastian Redla2deb432008-12-13 15:32:12 +00001591Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl39d4f022008-12-11 22:51:44 +00001592 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redla2deb432008-12-13 15:32:12 +00001593 if (Res.isInvalid()) return move(Res);
1594
Chris Lattnerddd3e632007-12-12 01:04:12 +00001595 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1596 // expressions. At this point, we know that the only valid thing that starts
1597 // with '@' is an @"".
1598 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001599 ExprVector AtStrings(Actions);
Chris Lattnerddd3e632007-12-12 01:04:12 +00001600 AtLocs.push_back(AtLoc);
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001601 AtStrings.push_back(Res.release());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001602
Chris Lattnerddd3e632007-12-12 01:04:12 +00001603 while (Tok.is(tok::at)) {
1604 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001605
Sebastian Redl62261042008-12-09 20:22:58 +00001606 // Invalid unless there is a string literal.
Chris Lattnerca7e5cb2009-02-18 05:56:09 +00001607 if (!isTokenStringLiteral())
1608 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerddd3e632007-12-12 01:04:12 +00001609
Chris Lattnerca7e5cb2009-02-18 05:56:09 +00001610 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001611 if (Lit.isInvalid())
Sebastian Redla2deb432008-12-13 15:32:12 +00001612 return move(Lit);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001613
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001614 AtStrings.push_back(Lit.release());
Chris Lattnerddd3e632007-12-12 01:04:12 +00001615 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001616
1617 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1618 AtStrings.size()));
Anders Carlssona66cad42007-08-21 17:43:55 +00001619}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001620
1621/// objc-encode-expression:
1622/// @encode ( type-name )
Sebastian Redla2deb432008-12-13 15:32:12 +00001623Parser::OwningExprResult
1624Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001625 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redla2deb432008-12-13 15:32:12 +00001626
Anders Carlsson8be1d402007-08-22 15:14:15 +00001627 SourceLocation EncLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001628
Chris Lattnerf9311a92008-08-05 06:19:09 +00001629 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001630 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1631
Anders Carlsson8be1d402007-08-22 15:14:15 +00001632 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla2deb432008-12-13 15:32:12 +00001633
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001634 TypeResult Ty = ParseTypeName();
Sebastian Redla2deb432008-12-13 15:32:12 +00001635
Anders Carlsson92faeb82007-08-23 15:31:37 +00001636 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redla2deb432008-12-13 15:32:12 +00001637
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001638 if (Ty.isInvalid())
1639 return ExprError();
1640
1641 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1642 Ty.get(), RParenLoc));
Anders Carlsson8be1d402007-08-22 15:14:15 +00001643}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001644
1645/// objc-protocol-expression
1646/// @protocol ( protocol-name )
Sebastian Redla2deb432008-12-13 15:32:12 +00001647Parser::OwningExprResult
1648Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001649 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001650
Chris Lattnerf9311a92008-08-05 06:19:09 +00001651 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001652 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1653
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001654 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla2deb432008-12-13 15:32:12 +00001655
Chris Lattnerf9311a92008-08-05 06:19:09 +00001656 if (Tok.isNot(tok::identifier))
Sebastian Redla2deb432008-12-13 15:32:12 +00001657 return ExprError(Diag(Tok, diag::err_expected_ident));
1658
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001659 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001660 ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001661
Anders Carlsson92faeb82007-08-23 15:31:37 +00001662 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001663
Sebastian Redla2deb432008-12-13 15:32:12 +00001664 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1665 LParenLoc, RParenLoc));
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001666}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001667
1668/// objc-selector-expression
1669/// @selector '(' objc-keyword-selector ')'
Sebastian Redla2deb432008-12-13 15:32:12 +00001670Parser::OwningExprResult
1671Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001672 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001673
Chris Lattnerf9311a92008-08-05 06:19:09 +00001674 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001675 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1676
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001677 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001678 SourceLocation LParenLoc = ConsumeParen();
1679 SourceLocation sLoc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001680 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redla2deb432008-12-13 15:32:12 +00001681 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1682 return ExprError(Diag(Tok, diag::err_expected_ident));
1683
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001684 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001685 unsigned nColons = 0;
1686 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001687 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001688 if (Tok.isNot(tok::colon))
Sebastian Redla2deb432008-12-13 15:32:12 +00001689 return ExprError(Diag(Tok, diag::err_expected_colon));
1690
Chris Lattner847f5c12007-12-27 19:57:00 +00001691 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001692 ConsumeToken(); // Eat the ':'.
1693 if (Tok.is(tok::r_paren))
1694 break;
1695 // Check for another keyword selector.
1696 SourceLocation Loc;
Chris Lattnerd1b0f3b2009-04-11 18:13:45 +00001697 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001698 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001699 if (!SelIdent && Tok.isNot(tok::colon))
1700 break;
1701 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001702 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001703 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001704 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redla2deb432008-12-13 15:32:12 +00001705 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1706 LParenLoc, RParenLoc));
Gabor Greifa823dd12007-10-19 15:38:32 +00001707 }