blob: 8f4480ac0c66a721a5e98f9e4e882a12e5e30150 [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'
Steve Narofffb367882007-08-20 21:31:48 +000031Parser::DeclTy *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);
54 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Steve Narofffb367882007-08-20 21:31:48 +000062Parser::DeclTy *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);
Steve Narofffb367882007-08-20 21:31:48 +000070 return 0;
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"))
Steve Narofffb367882007-08-20 21:31:48 +000083 return 0;
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///
117Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
118 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.
125 return 0;
126 }
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.
142 return 0;
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 ';'
147 return 0;
148 }
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;
153 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
156 return 0;
157
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
Steve Naroff415c1832007-10-10 17:32:04 +0000161 DeclTy *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.
177 return 0;
178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000183 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
187 return 0;
188
189 DeclTy *ClsType =
190 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
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000202/// constructSetterName - Return the setter name for the given
203/// identifier, i.e. "set" + Name where the initial character of Name
204/// has been capitalized.
205static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
206 const IdentifierInfo *Name) {
Chris Lattner7963c1d2008-11-19 07:41:27 +0000207 llvm::SmallString<100> SelectorName;
Chris Lattner01f03cf2008-11-20 07:09:32 +0000208 SelectorName = "set";
Chris Lattner7963c1d2008-11-19 07:41:27 +0000209 SelectorName.append(Name->getName(), Name->getName()+Name->getLength());
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000210 SelectorName[3] = toupper(SelectorName[3]);
Chris Lattner7963c1d2008-11-19 07:41:27 +0000211 return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000212}
213
Steve Narofffb367882007-08-20 21:31:48 +0000214/// objc-interface-decl-list:
215/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000216/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000217/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000218/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000219/// objc-interface-decl-list declaration
220/// objc-interface-decl-list ';'
221///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000222/// objc-method-requirement: [OBJC2]
223/// @required
224/// @optional
225///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000226void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000227 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000228 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000229 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000231
Chris Lattnera40577e2008-10-20 06:10:06 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000235 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000236 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
237 DeclTy *methodPrototype =
238 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000239 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000243 continue;
244 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000245
Chris Lattnere48b46b2008-10-20 05:46:22 +0000246 // Ignore excess semicolons.
247 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000248 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000249 continue;
250 }
251
Chris Lattnera40577e2008-10-20 06:10:06 +0000252 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000253 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000254 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000255
256 // If we don't have an @ directive, parse it as a function definition.
257 if (Tok.isNot(tok::at)) {
Chris Lattnerd4c2ec72009-01-09 04:34:13 +0000258 // The code below does not consume '}'s because it is afraid of eating the
259 // end of a namespace. Because of the way this code is structured, an
260 // erroneous r_brace would cause an infinite loop if not handled here.
261 if (Tok.is(tok::r_brace))
262 break;
263
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000264 // FIXME: as the name implies, this rule allows function definitions.
265 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000266 ParseDeclarationOrFunctionDefinition();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000267 continue;
268 }
269
270 // Otherwise, we have an @ directive, eat the @.
271 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000272 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000273
Chris Lattnercba730b2008-10-20 05:57:40 +0000274 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000275 AtEndLoc = AtLoc;
276 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000277 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000278
Chris Lattnera40577e2008-10-20 06:10:06 +0000279 // Eat the identifier.
280 ConsumeToken();
281
Chris Lattnercba730b2008-10-20 05:57:40 +0000282 switch (DirectiveKind) {
283 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000284 // FIXME: If someone forgets an @end on a protocol, this loop will
285 // continue to eat up tons of stuff and spew lots of nonsense errors. It
286 // would probably be better to bail out if we saw an @class or @interface
287 // or something like that.
Chris Lattner727fb1f2008-10-20 07:22:18 +0000288 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000289 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000290 SkipUntil(tok::r_brace, tok::at);
291 break;
292
293 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000294 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000295 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000296 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000297 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000298 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000299 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000300 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000301 break;
302
303 case tok::objc_property:
Chris Lattner727fb1f2008-10-20 07:22:18 +0000304 if (!getLang().ObjC2)
305 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
306
Chris Lattnere48b46b2008-10-20 05:46:22 +0000307 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000308 // Parse property attribute list, if any.
Chris Lattner22f9d262008-10-20 07:24:39 +0000309 if (Tok.is(tok::l_paren))
Chris Lattnere48b46b2008-10-20 05:46:22 +0000310 ParseObjCPropertyAttribute(OCDS);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000311
Chris Lattnere48b46b2008-10-20 05:46:22 +0000312 // Parse all the comma separated declarators.
313 DeclSpec DS;
314 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
315 ParseStructDeclaration(DS, FieldDeclarators);
316
Chris Lattner9019ae52008-10-20 06:15:13 +0000317 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
318 tok::at);
319
Chris Lattnere48b46b2008-10-20 05:46:22 +0000320 // Convert them all to property declarations.
321 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
322 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000323 if (FD.D.getIdentifier() == 0) {
Chris Lattner194e7002008-11-18 07:50:21 +0000324 Diag(AtLoc, diag::err_objc_property_requires_field_name)
325 << FD.D.getSourceRange();
Chris Lattnerbf16a972008-10-20 06:33:53 +0000326 continue;
327 }
Fariborz Jahanian8f314552009-01-17 23:21:10 +0000328 if (FD.BitfieldSize) {
329 Diag(AtLoc, diag::err_objc_property_bitfield)
330 << FD.D.getSourceRange();
331 continue;
332 }
Chris Lattnerbf16a972008-10-20 06:33:53 +0000333
Chris Lattnere48b46b2008-10-20 05:46:22 +0000334 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000335 IdentifierInfo *SelName =
336 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
337
Chris Lattnere48b46b2008-10-20 05:46:22 +0000338 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000339 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000340 IdentifierInfo *SetterName = OCDS.getSetterName();
341 if (!SetterName)
342 SetterName = constructSetterName(PP.getIdentifierTable(),
343 FD.D.getIdentifier());
344 Selector SetterSel =
345 PP.getSelectorTable().getUnarySelector(SetterName);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000346 bool isOverridingProperty = false;
Chris Lattnerbf16a972008-10-20 06:33:53 +0000347 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
348 GetterSel, SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000349 interfaceDecl,
350 &isOverridingProperty,
Chris Lattnerbf16a972008-10-20 06:33:53 +0000351 MethodImplKind);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +0000352 if (!isOverridingProperty)
353 allProperties.push_back(Property);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000354 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000355 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000356 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000357 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000358
359 // We break out of the big loop in two cases: when we see @end or when we see
360 // EOF. In the former case, eat the @end. In the later case, emit an error.
361 if (Tok.isObjCAtKeyword(tok::objc_end))
362 ConsumeToken(); // the "end" identifier
363 else
364 Diag(Tok, diag::err_objc_missing_end);
365
Chris Lattnercba730b2008-10-20 05:57:40 +0000366 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000367 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000368 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
369 allMethods.empty() ? 0 : &allMethods[0],
370 allMethods.size(),
371 allProperties.empty() ? 0 : &allProperties[0],
372 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000373}
374
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000375/// Parse property attribute declarations.
376///
377/// property-attr-decl: '(' property-attrlist ')'
378/// property-attrlist:
379/// property-attribute
380/// property-attrlist ',' property-attribute
381/// property-attribute:
382/// getter '=' identifier
383/// setter '=' identifier ':'
384/// readonly
385/// readwrite
386/// assign
387/// retain
388/// copy
389/// nonatomic
390///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000391void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner22f9d262008-10-20 07:24:39 +0000392 assert(Tok.getKind() == tok::l_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000393 SourceLocation LHSLoc = ConsumeParen(); // consume '('
394
Chris Lattner1e5cc722008-10-20 07:15:22 +0000395 while (1) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000396 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner727fb1f2008-10-20 07:22:18 +0000397
398 // If this is not an identifier at all, bail out early.
399 if (II == 0) {
400 MatchRHSPunctuation(tok::r_paren, LHSLoc);
401 return;
402 }
403
Chris Lattner9ba0b222008-10-20 07:37:22 +0000404 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
405
Chris Lattner05fb7c82008-11-20 04:42:34 +0000406 if (II->isStr("readonly"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000407 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000408 else if (II->isStr("assign"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000409 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000410 else if (II->isStr("readwrite"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000412 else if (II->isStr("retain"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000413 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000414 else if (II->isStr("copy"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000415 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000416 else if (II->isStr("nonatomic"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000417 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner05fb7c82008-11-20 04:42:34 +0000418 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner2cc2b872008-10-20 07:39:53 +0000419 // getter/setter require extra treatment.
Chris Lattner9ba0b222008-10-20 07:37:22 +0000420 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
421 tok::r_paren))
Chris Lattner35cd4b92008-10-20 07:00:43 +0000422 return;
Chris Lattner9ba0b222008-10-20 07:37:22 +0000423
Chris Lattner22f9d262008-10-20 07:24:39 +0000424 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000425 Diag(Tok, diag::err_expected_ident);
Chris Lattner22f9d262008-10-20 07:24:39 +0000426 SkipUntil(tok::r_paren);
427 return;
428 }
429
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000430 if (II->getName()[0] == 's') {
Chris Lattner22f9d262008-10-20 07:24:39 +0000431 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
432 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000433 ConsumeToken(); // consume method name
434
435 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
436 tok::r_paren))
Chris Lattner22f9d262008-10-20 07:24:39 +0000437 return;
Chris Lattner22f9d262008-10-20 07:24:39 +0000438 } else {
439 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
440 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000441 ConsumeToken(); // consume method name
Chris Lattner22f9d262008-10-20 07:24:39 +0000442 }
Chris Lattner2cc2b872008-10-20 07:39:53 +0000443 } else {
Chris Lattner5e58ac22008-11-19 07:49:38 +0000444 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000445 SkipUntil(tok::r_paren);
446 return;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000447 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000448
Chris Lattner9ba0b222008-10-20 07:37:22 +0000449 if (Tok.isNot(tok::comma))
450 break;
Chris Lattner35cd4b92008-10-20 07:00:43 +0000451
Chris Lattner9ba0b222008-10-20 07:37:22 +0000452 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000453 }
Chris Lattner9ba0b222008-10-20 07:37:22 +0000454
455 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000456}
457
Steve Naroff81f1bba2007-09-06 21:24:23 +0000458/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000459/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000460/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000461///
462/// objc-instance-method: '-'
463/// objc-class-method: '+'
464///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000465/// objc-method-attributes: [OBJC2]
466/// __attribute__((deprecated))
467///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000468Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000469 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000470 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000471
472 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000473 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000474
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000475 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000476 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000477 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000478 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000479}
480
481/// objc-selector:
482/// identifier
483/// one of
484/// enum struct union if else while do for switch case default
485/// break continue return goto asm sizeof typeof __alignof
486/// unsigned long const short volatile signed restrict _Complex
487/// in out inout bycopy byref oneway int char float double void _Bool
488///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000489IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000490 switch (Tok.getKind()) {
491 default:
492 return 0;
493 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000494 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000495 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000496 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000497 case tok::kw_break:
498 case tok::kw_case:
499 case tok::kw_catch:
500 case tok::kw_char:
501 case tok::kw_class:
502 case tok::kw_const:
503 case tok::kw_const_cast:
504 case tok::kw_continue:
505 case tok::kw_default:
506 case tok::kw_delete:
507 case tok::kw_do:
508 case tok::kw_double:
509 case tok::kw_dynamic_cast:
510 case tok::kw_else:
511 case tok::kw_enum:
512 case tok::kw_explicit:
513 case tok::kw_export:
514 case tok::kw_extern:
515 case tok::kw_false:
516 case tok::kw_float:
517 case tok::kw_for:
518 case tok::kw_friend:
519 case tok::kw_goto:
520 case tok::kw_if:
521 case tok::kw_inline:
522 case tok::kw_int:
523 case tok::kw_long:
524 case tok::kw_mutable:
525 case tok::kw_namespace:
526 case tok::kw_new:
527 case tok::kw_operator:
528 case tok::kw_private:
529 case tok::kw_protected:
530 case tok::kw_public:
531 case tok::kw_register:
532 case tok::kw_reinterpret_cast:
533 case tok::kw_restrict:
534 case tok::kw_return:
535 case tok::kw_short:
536 case tok::kw_signed:
537 case tok::kw_sizeof:
538 case tok::kw_static:
539 case tok::kw_static_cast:
540 case tok::kw_struct:
541 case tok::kw_switch:
542 case tok::kw_template:
543 case tok::kw_this:
544 case tok::kw_throw:
545 case tok::kw_true:
546 case tok::kw_try:
547 case tok::kw_typedef:
548 case tok::kw_typeid:
549 case tok::kw_typename:
550 case tok::kw_typeof:
551 case tok::kw_union:
552 case tok::kw_unsigned:
553 case tok::kw_using:
554 case tok::kw_virtual:
555 case tok::kw_void:
556 case tok::kw_volatile:
557 case tok::kw_wchar_t:
558 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000559 case tok::kw__Bool:
560 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000561 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000562 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000563 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000564 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000565 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000566}
567
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000568/// objc-for-collection-in: 'in'
569///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000570bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000571 // FIXME: May have to do additional look-ahead to only allow for
572 // valid tokens following an 'in'; such as an identifier, unary operators,
573 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000574 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000575 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000576}
577
Ted Kremenek42730c52008-01-07 19:49:32 +0000578/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000579/// qualifier list and builds their bitmask representation in the input
580/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000581///
582/// objc-type-qualifiers:
583/// objc-type-qualifier
584/// objc-type-qualifiers objc-type-qualifier
585///
Ted Kremenek42730c52008-01-07 19:49:32 +0000586void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000587 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000588 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000589 return;
590
591 const IdentifierInfo *II = Tok.getIdentifierInfo();
592 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000593 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000594 continue;
595
Ted Kremenek42730c52008-01-07 19:49:32 +0000596 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000597 switch (i) {
598 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000599 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
600 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
601 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
602 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
603 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
604 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000605 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000606 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000607 ConsumeToken();
608 II = 0;
609 break;
610 }
611
612 // If this wasn't a recognized qualifier, bail out.
613 if (II) return;
614 }
615}
616
617/// objc-type-name:
618/// '(' objc-type-qualifiers[opt] type-name ')'
619/// '(' objc-type-qualifiers[opt] ')'
620///
Ted Kremenek42730c52008-01-07 19:49:32 +0000621Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000622 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000623
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000624 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnerb5769332008-08-23 01:48:03 +0000625 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000626
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000627 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000629
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000630 TypeTy *Ty = 0;
631 if (isTypeSpecifierQualifier())
Steve Naroff304ed392007-09-05 23:30:30 +0000632 Ty = ParseTypeName();
Chris Lattnerb5769332008-08-23 01:48:03 +0000633
Steve Naroffc6235e82008-10-21 14:15:04 +0000634 if (Tok.is(tok::r_paren))
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000635 ConsumeParen();
636 else if (Tok.getLocation() == TypeStartLoc) {
637 // If we didn't eat any tokens, then this isn't a type.
Chris Lattnerf006a222008-11-18 07:48:38 +0000638 Diag(Tok, diag::err_expected_type);
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000639 SkipUntil(tok::r_paren);
640 } else {
641 // Otherwise, we found *something*, but didn't get a ')' in the right
642 // place. Emit an error then return what we have as the type.
643 MatchRHSPunctuation(tok::r_paren, LParenLoc);
644 }
Steve Naroff304ed392007-09-05 23:30:30 +0000645 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000646}
647
648/// objc-method-decl:
649/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000650/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000651/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000652/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000653///
654/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000655/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000656/// objc-keyword-selector objc-keyword-decl
657///
658/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000659/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
660/// objc-selector ':' objc-keyword-attributes[opt] identifier
661/// ':' objc-type-name objc-keyword-attributes[opt] identifier
662/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000663///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000664/// objc-parmlist:
665/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000666///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000667/// objc-parms:
668/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000669///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000670/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000671/// , ...
672///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000673/// objc-keyword-attributes: [OBJC2]
674/// __attribute__((unused))
675///
Steve Naroff3774dd92007-10-26 20:53:56 +0000676Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000677 tok::TokenKind mType,
678 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000679 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000680{
Chris Lattnerb5769332008-08-23 01:48:03 +0000681 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000682 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000683 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000684 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000685 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000686
Steve Naroff3774dd92007-10-26 20:53:56 +0000687 SourceLocation selLoc;
688 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000689
Steve Naroff1a781092009-02-11 20:43:13 +0000690 // An unnamed colon is valid.
691 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattnerf006a222008-11-18 07:48:38 +0000692 Diag(Tok, diag::err_expected_selector_for_method)
693 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnerb5769332008-08-23 01:48:03 +0000694 // Skip until we get a ; or {}.
695 SkipUntil(tok::r_brace);
696 return 0;
697 }
698
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000699 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000700 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000701 // If attributes exist after the method, parse them.
702 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000703 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 MethodAttrs = ParseAttributes();
705
706 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000707 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000708 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000709 0, 0, 0, CargNames,
710 MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000711 }
Steve Naroff304ed392007-09-05 23:30:30 +0000712
Steve Naroff4ed9d662007-09-27 14:38:14 +0000713 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
714 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000715 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000716 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000717
718 Action::TypeTy *TypeInfo;
719 while (1) {
720 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000721
Chris Lattnerd031a452007-10-07 02:00:24 +0000722 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000723 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000724 Diag(Tok, diag::err_expected_colon);
725 break;
726 }
727 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000728 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000729 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000730 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000731 else
732 TypeInfo = 0;
733 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000734 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000735
Chris Lattnerd031a452007-10-07 02:00:24 +0000736 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000737 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000738 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000739
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000740 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000741 Diag(Tok, diag::err_expected_ident); // missing argument name.
742 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000743 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000744 ArgNames.push_back(Tok.getIdentifierInfo());
745 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000746
Chris Lattnerd031a452007-10-07 02:00:24 +0000747 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000748 SourceLocation Loc;
749 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000750 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000751 break;
752 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000753 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000754
Steve Naroff29fe7462007-11-15 12:35:21 +0000755 bool isVariadic = false;
756
Chris Lattnerd031a452007-10-07 02:00:24 +0000757 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000758 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000759 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000760 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000761 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000762 ConsumeToken();
763 break;
764 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000765 DeclSpec DS;
766 ParseDeclarationSpecifiers(DS);
767 // Parse the declarator.
768 Declarator ParmDecl(DS, Declarator::PrototypeContext);
769 ParseDeclarator(ParmDecl);
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000770 CargNames.push_back(ParmDecl);
Chris Lattnerd031a452007-10-07 02:00:24 +0000771 }
772
773 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000774 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000775 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000776 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000777 MethodAttrs = ParseAttributes();
778
779 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
780 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000781 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000782 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000783 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian89d53042009-01-09 00:38:19 +0000784 &ArgNames[0], CargNames,
785 MethodAttrs,
Steve Naroff29fe7462007-11-15 12:35:21 +0000786 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000787}
788
Steve Narofffb367882007-08-20 21:31:48 +0000789/// objc-protocol-refs:
790/// '<' identifier-list '>'
791///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000792bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000793ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
794 bool WarnOnDeclarations, SourceLocation &EndLoc) {
795 assert(Tok.is(tok::less) && "expected <");
796
797 ConsumeToken(); // the "<"
798
799 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
800
801 while (1) {
802 if (Tok.isNot(tok::identifier)) {
803 Diag(Tok, diag::err_expected_ident);
804 SkipUntil(tok::greater);
805 return true;
806 }
807 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
808 Tok.getLocation()));
809 ConsumeToken();
810
811 if (Tok.isNot(tok::comma))
812 break;
813 ConsumeToken();
814 }
815
816 // Consume the '>'.
817 if (Tok.isNot(tok::greater)) {
818 Diag(Tok, diag::err_expected_greater);
819 return true;
820 }
821
822 EndLoc = ConsumeAnyToken();
823
824 // Convert the list of protocols identifiers into a list of protocol decls.
825 Actions.FindProtocolDeclaration(WarnOnDeclarations,
826 &ProtocolIdents[0], ProtocolIdents.size(),
827 Protocols);
828 return false;
829}
830
Steve Narofffb367882007-08-20 21:31:48 +0000831/// objc-class-instance-variables:
832/// '{' objc-instance-variable-decl-list[opt] '}'
833///
834/// objc-instance-variable-decl-list:
835/// objc-visibility-spec
836/// objc-instance-variable-decl ';'
837/// ';'
838/// objc-instance-variable-decl-list objc-visibility-spec
839/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
840/// objc-instance-variable-decl-list ';'
841///
842/// objc-visibility-spec:
843/// @private
844/// @protected
845/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000846/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000847///
848/// objc-instance-variable-decl:
849/// struct-declaration
850///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000851void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
852 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000853 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000854 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000855 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
856
Douglas Gregor5eca99e2009-01-12 18:45:55 +0000857 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +0000858
Steve Naroffc4474992007-08-21 21:17:12 +0000859 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000860
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000861 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000862 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000863 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000864 // Each iteration of this loop reads one objc-instance-variable-decl.
865
866 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000867 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000868 Diag(Tok, diag::ext_extra_struct_semi);
869 ConsumeToken();
870 continue;
871 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000872
Steve Naroffc4474992007-08-21 21:17:12 +0000873 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000874 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000875 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000876 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000877 case tok::objc_private:
878 case tok::objc_public:
879 case tok::objc_protected:
880 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000881 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000882 ConsumeToken();
883 continue;
884 default:
885 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000886 continue;
887 }
888 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000889
890 // Parse all the comma separated declarators.
891 DeclSpec DS;
892 FieldDeclarators.clear();
893 ParseStructDeclaration(DS, FieldDeclarators);
894
895 // Convert them all to fields.
896 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
897 FieldDeclarator &FD = FieldDeclarators[i];
898 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000899 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000900 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000901 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000902 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000903 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000904
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000905 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000906 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000907 } else {
908 Diag(Tok, diag::err_expected_semi_decl_list);
909 // Skip to end of block or statement
910 SkipUntil(tok::r_brace, true, true);
911 }
912 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000913 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000914 // Call ActOnFields() even if we don't have any decls. This is useful
915 // for code rewriting tools that need to be aware of the empty list.
916 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
917 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000918 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000919 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000920}
Steve Narofffb367882007-08-20 21:31:48 +0000921
922/// objc-protocol-declaration:
923/// objc-protocol-definition
924/// objc-protocol-forward-reference
925///
926/// objc-protocol-definition:
927/// @protocol identifier
928/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000929/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000930/// @end
931///
932/// objc-protocol-forward-reference:
933/// @protocol identifier-list ';'
934///
935/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000936/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000937/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000938Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
939 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000940 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000941 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
942 ConsumeToken(); // the "protocol" identifier
943
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000944 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000945 Diag(Tok, diag::err_expected_ident); // missing protocol name.
946 return 0;
947 }
948 // Save the protocol name, then consume it.
949 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
950 SourceLocation nameLoc = ConsumeToken();
951
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000952 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000953 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000954 ConsumeToken();
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000955 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
956 attrList);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000957 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000958
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000959 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000960 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
961 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
962
Steve Naroff72f17fb2007-08-22 22:17:26 +0000963 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000964 while (1) {
965 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000966 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000967 Diag(Tok, diag::err_expected_ident);
968 SkipUntil(tok::semi);
969 return 0;
970 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000971 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
972 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000973 ConsumeToken(); // the identifier
974
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000975 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000976 break;
977 }
978 // Consume the ';'.
979 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
980 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000981
Steve Naroff415c1832007-10-10 17:32:04 +0000982 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000983 &ProtocolRefs[0],
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000984 ProtocolRefs.size(),
985 attrList);
Chris Lattnere705e5e2008-07-21 22:17:28 +0000986 }
987
Steve Naroff72f17fb2007-08-22 22:17:26 +0000988 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000989 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000990
Chris Lattner2bdedd62008-07-26 04:03:38 +0000991 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000992 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000993 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000994 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000995
Chris Lattner2bdedd62008-07-26 04:03:38 +0000996 DeclTy *ProtoType =
997 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
998 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000999 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001000 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +00001001 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +00001002}
Steve Narofffb367882007-08-20 21:31:48 +00001003
1004/// objc-implementation:
1005/// objc-class-implementation-prologue
1006/// objc-category-implementation-prologue
1007///
1008/// objc-class-implementation-prologue:
1009/// @implementation identifier objc-superclass[opt]
1010/// objc-class-instance-variables[opt]
1011///
1012/// objc-category-implementation-prologue:
1013/// @implementation identifier ( identifier )
1014
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001015Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1016 SourceLocation atLoc) {
1017 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1018 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1019 ConsumeToken(); // the "implementation" identifier
1020
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001021 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001022 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1023 return 0;
1024 }
1025 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001026 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001027 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1028
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001029 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001030 // we have a category implementation.
1031 SourceLocation lparenLoc = ConsumeParen();
1032 SourceLocation categoryLoc, rparenLoc;
1033 IdentifierInfo *categoryId = 0;
1034
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001035 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001036 categoryId = Tok.getIdentifierInfo();
1037 categoryLoc = ConsumeToken();
1038 } else {
1039 Diag(Tok, diag::err_expected_ident); // missing category name.
1040 return 0;
1041 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001042 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001043 Diag(Tok, diag::err_expected_rparen);
1044 SkipUntil(tok::r_paren, false); // don't stop at ';'
1045 return 0;
1046 }
1047 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001048 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001049 atLoc, nameId, nameLoc, categoryId,
1050 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001052 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001053 }
1054 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001055 SourceLocation superClassLoc;
1056 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001058 // We have a super class
1059 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001060 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001061 Diag(Tok, diag::err_expected_ident); // missing super class name.
1062 return 0;
1063 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001064 superClassId = Tok.getIdentifierInfo();
1065 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001066 }
Steve Naroff415c1832007-10-10 17:32:04 +00001067 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001068 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001069 superClassId, superClassLoc);
1070
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001071 if (Tok.is(tok::l_brace)) // we have ivars
1072 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001073 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001074
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001075 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001076}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001077
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001078Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1079 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1080 "ParseObjCAtEndDeclaration(): Expected @end");
1081 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001082 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001083 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001084 else
1085 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001087}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001088
1089/// compatibility-alias-decl:
1090/// @compatibility_alias alias-name class-name ';'
1091///
1092Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1093 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1094 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1095 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001097 Diag(Tok, diag::err_expected_ident);
1098 return 0;
1099 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001100 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1101 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001102 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001103 Diag(Tok, diag::err_expected_ident);
1104 return 0;
1105 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001106 IdentifierInfo *classId = Tok.getIdentifierInfo();
1107 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1108 if (Tok.isNot(tok::semi)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001109 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001110 return 0;
1111 }
1112 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1113 aliasId, aliasLoc,
1114 classId, classLoc);
1115 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001116}
1117
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001118/// property-synthesis:
1119/// @synthesize property-ivar-list ';'
1120///
1121/// property-ivar-list:
1122/// property-ivar
1123/// property-ivar-list ',' property-ivar
1124///
1125/// property-ivar:
1126/// identifier
1127/// identifier '=' identifier
1128///
1129Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1130 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1131 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001133 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001134 Diag(Tok, diag::err_expected_ident);
1135 return 0;
1136 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001137 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001138 IdentifierInfo *propertyIvar = 0;
1139 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1140 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001141 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001142 // property '=' ivar-name
1143 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001144 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001145 Diag(Tok, diag::err_expected_ident);
1146 break;
1147 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001148 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001149 ConsumeToken(); // consume ivar-name
1150 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001151 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1152 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001153 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001154 break;
1155 ConsumeToken(); // consume ','
1156 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001157 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001158 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001159 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001160}
1161
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001162/// property-dynamic:
1163/// @dynamic property-list
1164///
1165/// property-list:
1166/// identifier
1167/// property-list ',' identifier
1168///
1169Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1170 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1171 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1172 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001173 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001174 Diag(Tok, diag::err_expected_ident);
1175 return 0;
1176 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001177 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001178 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1179 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1180 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1181 propertyId, 0);
1182
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001183 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001184 break;
1185 ConsumeToken(); // consume ','
1186 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001187 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001188 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001189 return 0;
1190}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001191
1192/// objc-throw-statement:
1193/// throw expression[opt];
1194///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001195Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl62261042008-12-09 20:22:58 +00001196 OwningExprResult Res(Actions);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001197 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001198 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001199 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001200 if (Res.isInvalid()) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001201 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001202 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001203 }
1204 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001205 ConsumeToken(); // consume ';'
Steve Naroff590fe482009-02-11 20:05:44 +00001206 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001207}
1208
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001209/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001210/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001211///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001212Parser::OwningStmtResult
1213Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001214 ConsumeToken(); // consume synchronized
1215 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001216 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl15bf1452008-12-11 20:12:42 +00001217 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001218 }
1219 ConsumeParen(); // '('
Sebastian Redl14ca7412008-12-11 21:36:32 +00001220 OwningExprResult Res(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001221 if (Res.isInvalid()) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001222 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001223 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001224 }
1225 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001226 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001227 return StmtError();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001228 }
1229 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001230 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001231 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001232 return StmtError();
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001233 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001234 // Enter a scope to hold everything within the compound stmt. Compound
1235 // statements can always hold declarations.
Douglas Gregor95d40792008-12-10 06:34:36 +00001236 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff70337ac2008-06-04 20:36:13 +00001237
Sebastian Redl10c32952008-12-11 19:30:53 +00001238 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001239
Douglas Gregor95d40792008-12-10 06:34:36 +00001240 BodyScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001241 if (SynchBody.isInvalid())
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001242 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl81db6682009-02-05 15:02:23 +00001243 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001244}
1245
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001246/// objc-try-catch-statement:
1247/// @try compound-statement objc-catch-list[opt]
1248/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1249///
1250/// objc-catch-list:
1251/// @catch ( parameter-declaration ) compound-statement
1252/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1253/// catch-parameter-declaration:
1254/// parameter-declaration
1255/// '...' [OBJC2]
1256///
Sebastian Redl15bf1452008-12-11 20:12:42 +00001257Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001258 bool catch_or_finally_seen = false;
Sebastian Redl15bf1452008-12-11 20:12:42 +00001259
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001260 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001261 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001262 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001263 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001264 }
Sebastian Redl62261042008-12-09 20:22:58 +00001265 OwningStmtResult CatchStmts(Actions);
1266 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor95d40792008-12-10 06:34:36 +00001267 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl10c32952008-12-11 19:30:53 +00001268 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor95d40792008-12-10 06:34:36 +00001269 TryScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001270 if (TryBody.isInvalid())
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001271 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl6008ac32008-11-25 22:21:31 +00001272
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001273 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001274 // At this point, we need to lookahead to determine if this @ is the start
1275 // of an @catch or @finally. We don't want to consume the @ token if this
1276 // is an @try or @encode or something else.
1277 Token AfterAt = GetLookAheadToken(1);
1278 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1279 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1280 break;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001281
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001282 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001283 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Sebastian Redl62261042008-12-09 20:22:58 +00001284 OwningStmtResult FirstPart(Actions);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001285 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001286 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001287 ConsumeParen();
Steve Naroff590fe482009-02-11 20:05:44 +00001288 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001289 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001290 DeclSpec DS;
1291 ParseDeclarationSpecifiers(DS);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001292 // For some odd reason, the name of the exception variable is
Steve Narofffd8f76c2008-06-03 05:36:54 +00001293 // optional. As a result, we need to use PrototypeContext.
1294 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001295 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001296 if (DeclaratorInfo.getIdentifier()) {
Sebastian Redl15bf1452008-12-11 20:12:42 +00001297 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001298 DeclaratorInfo, 0);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001299 FirstPart =
Sebastian Redl15bf1452008-12-11 20:12:42 +00001300 Actions.ActOnDeclStmt(aBlockVarDecl,
Steve Narofffd8f76c2008-06-03 05:36:54 +00001301 DS.getSourceRange().getBegin(),
1302 DeclaratorInfo.getSourceRange().getEnd());
Steve Narofffd8f76c2008-06-03 05:36:54 +00001303 }
Steve Naroffc949a462008-02-05 21:27:35 +00001304 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001305 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001306 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001307
Sebastian Redl62261042008-12-09 20:22:58 +00001308 OwningStmtResult CatchBody(Actions, true);
Chris Lattner8027be62008-02-14 19:27:54 +00001309 if (Tok.is(tok::l_brace))
1310 CatchBody = ParseCompoundStatementBody();
1311 else
1312 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001313 if (CatchBody.isInvalid())
Fariborz Jahanian06798362007-11-01 23:59:59 +00001314 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001315 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001316 RParenLoc, move(FirstPart), move(CatchBody),
1317 move(CatchStmts));
Steve Naroffc949a462008-02-05 21:27:35 +00001318 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +00001319 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1320 << "@catch clause";
Sebastian Redl15bf1452008-12-11 20:12:42 +00001321 return StmtError();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001322 }
1323 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001324 } else {
1325 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001326 ConsumeToken(); // consume finally
Douglas Gregor95d40792008-12-10 06:34:36 +00001327 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001328
Sebastian Redl62261042008-12-09 20:22:58 +00001329 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner8027be62008-02-14 19:27:54 +00001330 if (Tok.is(tok::l_brace))
1331 FinallyBody = ParseCompoundStatementBody();
1332 else
1333 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001334 if (FinallyBody.isInvalid())
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001335 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001336 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001337 move(FinallyBody));
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001338 catch_or_finally_seen = true;
1339 break;
1340 }
1341 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001342 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001343 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001344 return StmtError();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001345 }
Sebastian Redl81db6682009-02-05 15:02:23 +00001346 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1347 move(FinallyStmt));
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001348}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001349
Steve Naroff81f1bba2007-09-06 21:24:23 +00001350/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001351///
Steve Naroff18c83382007-11-13 23:01:27 +00001352Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001353 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001354 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001355 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001356 ConsumeToken();
1357
Steve Naroff9191a9e82007-11-11 19:54:21 +00001358 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001359 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001360 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001361
1362 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1363 SkipUntil(tok::l_brace, true, true);
1364
1365 // If we didn't find the '{', bail out.
1366 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001367 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001368 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001369 SourceLocation BraceLoc = Tok.getLocation();
1370
1371 // Enter a scope for the method body.
Douglas Gregor95d40792008-12-10 06:34:36 +00001372 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001373
1374 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001375 // specified Declarator for the method.
1376 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Sebastian Redl10c32952008-12-11 19:30:53 +00001377
1378 OwningStmtResult FnBody(ParseCompoundStatementBody());
1379
Steve Naroff9191a9e82007-11-11 19:54:21 +00001380 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001381 if (FnBody.isInvalid())
Sebastian Redl76b9ddb2008-12-21 12:04:03 +00001382 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1383 MultiStmtArg(Actions), false);
Sebastian Redl91f9b0a2008-12-13 16:23:55 +00001384
Steve Naroff9191a9e82007-11-11 19:54:21 +00001385 // Leave the function body scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001386 BodyScope.Exit();
Sebastian Redl91f9b0a2008-12-13 16:23:55 +00001387
Steve Naroff9191a9e82007-11-11 19:54:21 +00001388 // TODO: Pass argument information.
Sebastian Redl81db6682009-02-05 15:02:23 +00001389 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Steve Naroff18c83382007-11-13 23:01:27 +00001390 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001391}
Anders Carlssona66cad42007-08-21 17:43:55 +00001392
Sebastian Redl15bf1452008-12-11 20:12:42 +00001393Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffc949a462008-02-05 21:27:35 +00001394 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001395 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001396 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1397 return ParseObjCThrowStmt(AtLoc);
1398 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1399 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001400 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001401 if (Res.isInvalid()) {
Steve Naroffc949a462008-02-05 21:27:35 +00001402 // If the expression is invalid, skip ahead to the next semicolon. Not
1403 // doing this opens us up to the possibility of infinite loops if
1404 // ParseExpression does not consume any tokens.
1405 SkipUntil(tok::semi);
Sebastian Redl15bf1452008-12-11 20:12:42 +00001406 return StmtError();
Steve Naroffc949a462008-02-05 21:27:35 +00001407 }
1408 // Otherwise, eat the semicolon.
1409 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl81db6682009-02-05 15:02:23 +00001410 return Actions.ActOnExprStmt(move(Res));
Steve Naroffc949a462008-02-05 21:27:35 +00001411}
1412
Sebastian Redla2deb432008-12-13 15:32:12 +00001413Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001414 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001415 case tok::string_literal: // primary-expression: string-literal
1416 case tok::wide_string_literal:
Sebastian Redla2deb432008-12-13 15:32:12 +00001417 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerddd3e632007-12-12 01:04:12 +00001418 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001419 if (Tok.getIdentifierInfo() == 0)
Sebastian Redla2deb432008-12-13 15:32:12 +00001420 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl14ca7412008-12-11 21:36:32 +00001421
Chris Lattnerf9311a92008-08-05 06:19:09 +00001422 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1423 case tok::objc_encode:
Sebastian Redla2deb432008-12-13 15:32:12 +00001424 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001425 case tok::objc_protocol:
Sebastian Redla2deb432008-12-13 15:32:12 +00001426 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001427 case tok::objc_selector:
Sebastian Redla2deb432008-12-13 15:32:12 +00001428 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001429 default:
Sebastian Redla2deb432008-12-13 15:32:12 +00001430 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattnerf9311a92008-08-05 06:19:09 +00001431 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001432 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001433}
1434
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001435/// objc-message-expr:
1436/// '[' objc-receiver objc-message-args ']'
1437///
1438/// objc-receiver:
1439/// expression
1440/// class-name
1441/// type-name
Sebastian Redla2deb432008-12-13 15:32:12 +00001442Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnered27a532008-01-25 18:59:06 +00001443 assert(Tok.is(tok::l_square) && "'[' expected");
1444 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1445
1446 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001447 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001448 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroffc64a53d2008-11-19 15:54:23 +00001449 SourceLocation NameLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001450 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1451 ExprArg(Actions));
Chris Lattnered27a532008-01-25 18:59:06 +00001452 }
1453
Sebastian Redl14ca7412008-12-11 21:36:32 +00001454 OwningExprResult Res(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001455 if (Res.isInvalid()) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001456 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001457 return move(Res);
Chris Lattnered27a532008-01-25 18:59:06 +00001458 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001459
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001460 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +00001461 0, move(Res));
Chris Lattnered27a532008-01-25 18:59:06 +00001462}
Sebastian Redla2deb432008-12-13 15:32:12 +00001463
Chris Lattnered27a532008-01-25 18:59:06 +00001464/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1465/// the rest of a message expression.
Sebastian Redla2deb432008-12-13 15:32:12 +00001466///
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001467/// objc-message-args:
1468/// objc-selector
1469/// objc-keywordarg-list
1470///
1471/// objc-keywordarg-list:
1472/// objc-keywordarg
1473/// objc-keywordarg-list objc-keywordarg
1474///
1475/// objc-keywordarg:
1476/// selector-name[opt] ':' objc-keywordexpr
1477///
1478/// objc-keywordexpr:
1479/// nonempty-expr-list
1480///
1481/// nonempty-expr-list:
1482/// assignment-expression
1483/// nonempty-expr-list , assignment-expression
Sebastian Redla2deb432008-12-13 15:32:12 +00001484///
1485Parser::OwningExprResult
Chris Lattnered27a532008-01-25 18:59:06 +00001486Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +00001487 SourceLocation NameLoc,
Chris Lattnered27a532008-01-25 18:59:06 +00001488 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-12-13 15:32:12 +00001489 ExprArg ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001490 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001491 SourceLocation Loc;
1492 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001493
Anders Carlssonb42900d2009-02-14 18:21:46 +00001494 SourceLocation SelectorLoc = Loc;
1495
Steve Naroff4ed9d662007-09-27 14:38:14 +00001496 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001497 ExprVector KeyExprs(Actions);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001498
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001499 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001500 while (1) {
1501 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001502 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001503
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001504 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001505 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001506 // We must manually skip to a ']', otherwise the expression skipper will
1507 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1508 // the enclosing expression.
1509 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001510 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001511 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001512
Steve Naroff4ed9d662007-09-27 14:38:14 +00001513 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001514 /// Parse the expression after ':'
Sebastian Redl14ca7412008-12-11 21:36:32 +00001515 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001516 if (Res.isInvalid()) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001517 // We must manually skip to a ']', otherwise the expression skipper will
1518 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1519 // the enclosing expression.
1520 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001521 return move(Res);
Steve Naroff253118b2007-09-17 20:25:27 +00001522 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001523
Steve Naroff253118b2007-09-17 20:25:27 +00001524 // We have a valid expression.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001525 KeyExprs.push_back(Res.release());
Sebastian Redla2deb432008-12-13 15:32:12 +00001526
Steve Naroff253118b2007-09-17 20:25:27 +00001527 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001528 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001529 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001530 break;
1531 // We have a selector or a colon, continue parsing.
1532 }
1533 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001534 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001535 ConsumeToken(); // Eat the ','.
1536 /// Parse the expression after ','
Sebastian Redl14ca7412008-12-11 21:36:32 +00001537 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001538 if (Res.isInvalid()) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001539 // We must manually skip to a ']', otherwise the expression skipper will
1540 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1541 // the enclosing expression.
1542 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001543 return move(Res);
Steve Naroff9f176d12007-11-15 13:05:42 +00001544 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001545
Steve Naroff9f176d12007-11-15 13:05:42 +00001546 // We have a valid expression.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001547 KeyExprs.push_back(Res.release());
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001548 }
1549 } else if (!selIdent) {
1550 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redla2deb432008-12-13 15:32:12 +00001551
Chris Lattnerf9311a92008-08-05 06:19:09 +00001552 // We must manually skip to a ']', otherwise the expression skipper will
1553 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1554 // the enclosing expression.
1555 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001556 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001557 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001558
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001559 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001560 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001561 // We must manually skip to a ']', otherwise the expression skipper will
1562 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1563 // the enclosing expression.
1564 SkipUntil(tok::r_square);
Sebastian Redla2deb432008-12-13 15:32:12 +00001565 return ExprError();
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001566 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001567
Chris Lattnered27a532008-01-25 18:59:06 +00001568 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redla2deb432008-12-13 15:32:12 +00001569
Steve Narofff9e80db2007-10-05 18:42:47 +00001570 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001571 if (nKeys == 0)
1572 KeyIdents.push_back(selIdent);
1573 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redla2deb432008-12-13 15:32:12 +00001574
Chris Lattnerd031a452007-10-07 02:00:24 +00001575 // We've just parsed a keyword message.
Sebastian Redla2deb432008-12-13 15:32:12 +00001576 if (ReceiverName)
1577 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonb42900d2009-02-14 18:21:46 +00001578 LBracLoc, NameLoc, SelectorLoc,
1579 RBracLoc,
Sebastian Redla2deb432008-12-13 15:32:12 +00001580 KeyExprs.take(), KeyExprs.size()));
1581 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonb42900d2009-02-14 18:21:46 +00001582 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redla2deb432008-12-13 15:32:12 +00001583 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001584}
1585
Sebastian Redla2deb432008-12-13 15:32:12 +00001586Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl39d4f022008-12-11 22:51:44 +00001587 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redla2deb432008-12-13 15:32:12 +00001588 if (Res.isInvalid()) return move(Res);
1589
Chris Lattnerddd3e632007-12-12 01:04:12 +00001590 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1591 // expressions. At this point, we know that the only valid thing that starts
1592 // with '@' is an @"".
1593 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001594 ExprVector AtStrings(Actions);
Chris Lattnerddd3e632007-12-12 01:04:12 +00001595 AtLocs.push_back(AtLoc);
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001596 AtStrings.push_back(Res.release());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001597
Chris Lattnerddd3e632007-12-12 01:04:12 +00001598 while (Tok.is(tok::at)) {
1599 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001600
Sebastian Redl62261042008-12-09 20:22:58 +00001601 // Invalid unless there is a string literal.
1602 OwningExprResult Lit(Actions, true);
Chris Lattnerddd3e632007-12-12 01:04:12 +00001603 if (isTokenStringLiteral())
Sebastian Redl6008ac32008-11-25 22:21:31 +00001604 Lit = ParseStringLiteralExpression();
Chris Lattnerddd3e632007-12-12 01:04:12 +00001605 else
1606 Diag(Tok, diag::err_objc_concat_string);
Chris Lattnerddd3e632007-12-12 01:04:12 +00001607
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001608 if (Lit.isInvalid())
Sebastian Redla2deb432008-12-13 15:32:12 +00001609 return move(Lit);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001610
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001611 AtStrings.push_back(Lit.release());
Chris Lattnerddd3e632007-12-12 01:04:12 +00001612 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001613
1614 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1615 AtStrings.size()));
Anders Carlssona66cad42007-08-21 17:43:55 +00001616}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001617
1618/// objc-encode-expression:
1619/// @encode ( type-name )
Sebastian Redla2deb432008-12-13 15:32:12 +00001620Parser::OwningExprResult
1621Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001622 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redla2deb432008-12-13 15:32:12 +00001623
Anders Carlsson8be1d402007-08-22 15:14:15 +00001624 SourceLocation EncLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001625
Chris Lattnerf9311a92008-08-05 06:19:09 +00001626 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001627 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1628
Anders Carlsson8be1d402007-08-22 15:14:15 +00001629 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla2deb432008-12-13 15:32:12 +00001630
Anders Carlsson8be1d402007-08-22 15:14:15 +00001631 TypeTy *Ty = ParseTypeName();
Sebastian Redla2deb432008-12-13 15:32:12 +00001632
Anders Carlsson92faeb82007-08-23 15:31:37 +00001633 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redla2deb432008-12-13 15:32:12 +00001634
1635 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
1636 RParenLoc));
Anders Carlsson8be1d402007-08-22 15:14:15 +00001637}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001638
1639/// objc-protocol-expression
1640/// @protocol ( protocol-name )
Sebastian Redla2deb432008-12-13 15:32:12 +00001641Parser::OwningExprResult
1642Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001643 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001644
Chris Lattnerf9311a92008-08-05 06:19:09 +00001645 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001646 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1647
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001648 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla2deb432008-12-13 15:32:12 +00001649
Chris Lattnerf9311a92008-08-05 06:19:09 +00001650 if (Tok.isNot(tok::identifier))
Sebastian Redla2deb432008-12-13 15:32:12 +00001651 return ExprError(Diag(Tok, diag::err_expected_ident));
1652
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001653 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001654 ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001655
Anders Carlsson92faeb82007-08-23 15:31:37 +00001656 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001657
Sebastian Redla2deb432008-12-13 15:32:12 +00001658 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1659 LParenLoc, RParenLoc));
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001660}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001661
1662/// objc-selector-expression
1663/// @selector '(' objc-keyword-selector ')'
Sebastian Redla2deb432008-12-13 15:32:12 +00001664Parser::OwningExprResult
1665Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001666 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001667
Chris Lattnerf9311a92008-08-05 06:19:09 +00001668 if (Tok.isNot(tok::l_paren))
Sebastian Redla2deb432008-12-13 15:32:12 +00001669 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1670
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001671 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001672 SourceLocation LParenLoc = ConsumeParen();
1673 SourceLocation sLoc;
1674 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redla2deb432008-12-13 15:32:12 +00001675 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1676 return ExprError(Diag(Tok, diag::err_expected_ident));
1677
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001678 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001679 unsigned nColons = 0;
1680 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001681 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001682 if (Tok.isNot(tok::colon))
Sebastian Redla2deb432008-12-13 15:32:12 +00001683 return ExprError(Diag(Tok, diag::err_expected_colon));
1684
Chris Lattner847f5c12007-12-27 19:57:00 +00001685 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001686 ConsumeToken(); // Eat the ':'.
1687 if (Tok.is(tok::r_paren))
1688 break;
1689 // Check for another keyword selector.
1690 SourceLocation Loc;
1691 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001692 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001693 if (!SelIdent && Tok.isNot(tok::colon))
1694 break;
1695 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001696 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001697 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001698 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redla2deb432008-12-13 15:32:12 +00001699 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1700 LParenLoc, RParenLoc));
Gabor Greifa823dd12007-10-19 15:38:32 +00001701 }