blob: f406eabf5669ffbb3b1708b190d5187f52fe7e65 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/Diagnostic.h"
19#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
Chris Lattner891dca62008-12-08 21:53:24 +000023/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000024/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000026/// [OBJC] objc-class-declaration
27/// [OBJC] objc-alias-declaration
28/// [OBJC] objc-protocol-definition
29/// [OBJC] objc-method-definition
30/// [OBJC] '@' 'end'
Steve Naroffdac269b2007-08-20 21:31:48 +000031Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
33
Steve Naroff861cf3e2007-08-23 18:16:40 +000034 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000035 case tok::objc_class:
36 return ParseObjCAtClassDeclaration(AtLoc);
37 case tok::objc_interface:
38 return ParseObjCAtInterfaceDeclaration(AtLoc);
39 case tok::objc_protocol:
40 return ParseObjCAtProtocolDeclaration(AtLoc);
41 case tok::objc_implementation:
42 return ParseObjCAtImplementationDeclaration(AtLoc);
43 case tok::objc_end:
44 return ParseObjCAtEndDeclaration(AtLoc);
45 case tok::objc_compatibility_alias:
46 return ParseObjCAtAliasDeclaration(AtLoc);
47 case tok::objc_synthesize:
48 return ParseObjCPropertySynthesize(AtLoc);
49 case tok::objc_dynamic:
50 return ParseObjCPropertyDynamic(AtLoc);
51 default:
52 Diag(AtLoc, diag::err_unexpected_at);
53 SkipUntil(tok::semi);
54 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Steve Naroffdac269b2007-08-20 21:31:48 +000062Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ConsumeToken(); // the identifier "class"
64 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
65
66 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000067 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 Diag(Tok, diag::err_expected_ident);
69 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000070 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
Reid Spencer5f016e22007-07-11 17:01:13 +000072 ClassNames.push_back(Tok.getIdentifierInfo());
73 ConsumeToken();
74
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000076 break;
77
78 ConsumeToken();
79 }
80
81 // Consume the ';'.
82 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000083 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000084
Steve Naroffe440eb82007-10-10 17:32:04 +000085 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000086 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Steve Naroffdac269b2007-08-20 21:31:48 +000089///
90/// objc-interface:
91/// objc-class-interface-attributes[opt] objc-class-interface
92/// objc-category-interface
93///
94/// objc-class-interface:
95/// '@' 'interface' identifier objc-superclass[opt]
96/// objc-protocol-refs[opt]
97/// objc-class-instance-variables[opt]
98/// objc-interface-decl-list
99/// @end
100///
101/// objc-category-interface:
102/// '@' 'interface' identifier '(' identifier[opt] ')'
103/// objc-protocol-refs[opt]
104/// objc-interface-decl-list
105/// @end
106///
107/// objc-superclass:
108/// ':' identifier
109///
110/// objc-class-interface-attributes:
111/// __attribute__((visibility("default")))
112/// __attribute__((visibility("hidden")))
113/// __attribute__((deprecated))
114/// __attribute__((unavailable))
115/// __attribute__((objc_exception)) - used by NSException on 64-bit
116///
117Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
118 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000119 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000120 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121 ConsumeToken(); // the "interface" identifier
122
Chris Lattnerdf195262007-10-09 17:51:17 +0000123 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000124 Diag(Tok, diag::err_expected_ident); // missing class or category name.
125 return 0;
126 }
127 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
130
Chris Lattnerdf195262007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000150
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000152 SourceLocation EndProtoLoc;
153 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
156 return 0;
157
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 if (attrList) // categories don't support attributes.
159 Diag(Tok, diag::err_objc_no_attributes_on_category);
160
Steve Naroffe440eb82007-10-10 17:32:04 +0000161 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000162 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000163 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000164 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000165
166 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000167 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 // Parse a class interface.
170 IdentifierInfo *superClassId = 0;
171 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000172
Chris Lattnerdf195262007-10-09 17:51:17 +0000173 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing super class name.
177 return 0;
178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattner06036d32008-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 Narofff28b2642007-09-05 23:30:30 +0000194
Chris Lattnerdf195262007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000197
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000199 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000200}
201
Daniel Dunbar4d7da2f2008-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 Lattneredc66f32008-11-19 07:41:27 +0000207 llvm::SmallString<100> SelectorName;
Chris Lattner69d27b92008-11-20 07:09:32 +0000208 SelectorName = "set";
Chris Lattneredc66f32008-11-19 07:41:27 +0000209 SelectorName.append(Name->getName(), Name->getName()+Name->getLength());
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000210 SelectorName[3] = toupper(SelectorName[3]);
Chris Lattneredc66f32008-11-19 07:41:27 +0000211 return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000212}
213
Steve Naroffdac269b2007-08-20 21:31:48 +0000214/// objc-interface-decl-list:
215/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000216/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000217/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000218/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000219/// objc-interface-decl-list declaration
220/// objc-interface-decl-list ';'
221///
Steve Naroff294494e2007-08-22 16:35:03 +0000222/// objc-method-requirement: [OBJC2]
223/// @required
224/// @optional
225///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000226void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000227 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8a779312008-06-06 16:45:15 +0000228 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000229 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000230 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000231
Chris Lattnerbc662af2008-10-20 06:10:06 +0000232 SourceLocation AtEndLoc;
233
Steve Naroff294494e2007-08-22 16:35:03 +0000234 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000235 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000236 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
237 DeclTy *methodPrototype =
238 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000239 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000243 continue;
244 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000245
Chris Lattnere82a10f2008-10-20 05:46:22 +0000246 // Ignore excess semicolons.
247 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000248 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000249 continue;
250 }
251
Chris Lattnerbc662af2008-10-20 06:10:06 +0000252 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000253 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000254 break;
Chris Lattnere82a10f2008-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 Lattner1fd80112009-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 Naroff4985ace2007-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 Naroff3536b442007-09-06 21:24:23 +0000266 ParseDeclarationOrFunctionDefinition();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000267 continue;
268 }
269
270 // Otherwise, we have an @ directive, eat the @.
271 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000272 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000273
Chris Lattnera2449b22008-10-20 05:57:40 +0000274 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000275 AtEndLoc = AtLoc;
276 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000277 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000278
Chris Lattnerbc662af2008-10-20 06:10:06 +0000279 // Eat the identifier.
280 ConsumeToken();
281
Chris Lattnera2449b22008-10-20 05:57:40 +0000282 switch (DirectiveKind) {
283 default:
Chris Lattnerbc662af2008-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 Lattnerf6ed8552008-10-20 07:22:18 +0000288 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000289 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 SkipUntil(tok::r_brace, tok::at);
291 break;
292
293 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000294 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000296 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000297 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000298 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000299 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000300 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000301 break;
302
303 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000304 if (!getLang().ObjC2)
305 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
306
Chris Lattnere82a10f2008-10-20 05:46:22 +0000307 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000308 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000309 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000310 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000311
Chris Lattnere82a10f2008-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 Lattnera1fed7e2008-10-20 06:15:13 +0000317 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
318 tok::at);
319
Chris Lattnere82a10f2008-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 Lattnerda3253d2008-10-20 06:33:53 +0000323 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000324 Diag(AtLoc, diag::err_objc_property_requires_field_name)
325 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000326 continue;
327 }
328
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000330 IdentifierInfo *SelName =
331 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
332
Chris Lattnere82a10f2008-10-20 05:46:22 +0000333 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000334 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000335 IdentifierInfo *SetterName = OCDS.getSetterName();
336 if (!SetterName)
337 SetterName = constructSetterName(PP.getIdentifierTable(),
338 FD.D.getIdentifier());
339 Selector SetterSel =
340 PP.getSelectorTable().getUnarySelector(SetterName);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000341 bool isOverridingProperty = false;
Chris Lattnerda3253d2008-10-20 06:33:53 +0000342 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
343 GetterSel, SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000344 interfaceDecl,
345 &isOverridingProperty,
Chris Lattnerda3253d2008-10-20 06:33:53 +0000346 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000347 if (!isOverridingProperty)
348 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000349 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000350 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000351 }
Steve Naroff294494e2007-08-22 16:35:03 +0000352 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000353
354 // We break out of the big loop in two cases: when we see @end or when we see
355 // EOF. In the former case, eat the @end. In the later case, emit an error.
356 if (Tok.isObjCAtKeyword(tok::objc_end))
357 ConsumeToken(); // the "end" identifier
358 else
359 Diag(Tok, diag::err_objc_missing_end);
360
Chris Lattnera2449b22008-10-20 05:57:40 +0000361 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000362 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000363 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
364 allMethods.empty() ? 0 : &allMethods[0],
365 allMethods.size(),
366 allProperties.empty() ? 0 : &allProperties[0],
367 allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000368}
369
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000370/// Parse property attribute declarations.
371///
372/// property-attr-decl: '(' property-attrlist ')'
373/// property-attrlist:
374/// property-attribute
375/// property-attrlist ',' property-attribute
376/// property-attribute:
377/// getter '=' identifier
378/// setter '=' identifier ':'
379/// readonly
380/// readwrite
381/// assign
382/// retain
383/// copy
384/// nonatomic
385///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000386void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000387 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000388 SourceLocation LHSLoc = ConsumeParen(); // consume '('
389
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000390 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000391 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000392
393 // If this is not an identifier at all, bail out early.
394 if (II == 0) {
395 MatchRHSPunctuation(tok::r_paren, LHSLoc);
396 return;
397 }
398
Chris Lattner156b0612008-10-20 07:37:22 +0000399 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
400
Chris Lattner92e62b02008-11-20 04:42:34 +0000401 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000403 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000404 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000405 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000406 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000407 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000409 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000410 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000411 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000412 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000413 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000414 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000415 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
416 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000417 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000418
Chris Lattner8ca329c2008-10-20 07:24:39 +0000419 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000420 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000421 SkipUntil(tok::r_paren);
422 return;
423 }
424
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000425 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000426 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
427 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000428 ConsumeToken(); // consume method name
429
430 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
431 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000432 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000433 } else {
434 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
435 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000436 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000437 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000438 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000439 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000440 SkipUntil(tok::r_paren);
441 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000442 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000443
Chris Lattner156b0612008-10-20 07:37:22 +0000444 if (Tok.isNot(tok::comma))
445 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000446
Chris Lattner156b0612008-10-20 07:37:22 +0000447 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000448 }
Chris Lattner156b0612008-10-20 07:37:22 +0000449
450 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000451}
452
Steve Naroff3536b442007-09-06 21:24:23 +0000453/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000454/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000455/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000456///
457/// objc-instance-method: '-'
458/// objc-class-method: '+'
459///
Steve Naroff4985ace2007-08-22 18:35:33 +0000460/// objc-method-attributes: [OBJC2]
461/// __attribute__((deprecated))
462///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000463Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000464 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000465 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000466
467 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000468 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000469
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000470 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000471 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000472 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000473 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000474}
475
476/// objc-selector:
477/// identifier
478/// one of
479/// enum struct union if else while do for switch case default
480/// break continue return goto asm sizeof typeof __alignof
481/// unsigned long const short volatile signed restrict _Complex
482/// in out inout bycopy byref oneway int char float double void _Bool
483///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000484IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000485 switch (Tok.getKind()) {
486 default:
487 return 0;
488 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000489 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000490 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000491 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000492 case tok::kw_break:
493 case tok::kw_case:
494 case tok::kw_catch:
495 case tok::kw_char:
496 case tok::kw_class:
497 case tok::kw_const:
498 case tok::kw_const_cast:
499 case tok::kw_continue:
500 case tok::kw_default:
501 case tok::kw_delete:
502 case tok::kw_do:
503 case tok::kw_double:
504 case tok::kw_dynamic_cast:
505 case tok::kw_else:
506 case tok::kw_enum:
507 case tok::kw_explicit:
508 case tok::kw_export:
509 case tok::kw_extern:
510 case tok::kw_false:
511 case tok::kw_float:
512 case tok::kw_for:
513 case tok::kw_friend:
514 case tok::kw_goto:
515 case tok::kw_if:
516 case tok::kw_inline:
517 case tok::kw_int:
518 case tok::kw_long:
519 case tok::kw_mutable:
520 case tok::kw_namespace:
521 case tok::kw_new:
522 case tok::kw_operator:
523 case tok::kw_private:
524 case tok::kw_protected:
525 case tok::kw_public:
526 case tok::kw_register:
527 case tok::kw_reinterpret_cast:
528 case tok::kw_restrict:
529 case tok::kw_return:
530 case tok::kw_short:
531 case tok::kw_signed:
532 case tok::kw_sizeof:
533 case tok::kw_static:
534 case tok::kw_static_cast:
535 case tok::kw_struct:
536 case tok::kw_switch:
537 case tok::kw_template:
538 case tok::kw_this:
539 case tok::kw_throw:
540 case tok::kw_true:
541 case tok::kw_try:
542 case tok::kw_typedef:
543 case tok::kw_typeid:
544 case tok::kw_typename:
545 case tok::kw_typeof:
546 case tok::kw_union:
547 case tok::kw_unsigned:
548 case tok::kw_using:
549 case tok::kw_virtual:
550 case tok::kw_void:
551 case tok::kw_volatile:
552 case tok::kw_wchar_t:
553 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000554 case tok::kw__Bool:
555 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000556 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000557 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000558 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000559 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000560 }
Steve Naroff294494e2007-08-22 16:35:03 +0000561}
562
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000563/// objc-for-collection-in: 'in'
564///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000565bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000566 // FIXME: May have to do additional look-ahead to only allow for
567 // valid tokens following an 'in'; such as an identifier, unary operators,
568 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000569 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000570 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000571}
572
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000574/// qualifier list and builds their bitmask representation in the input
575/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000576///
577/// objc-type-qualifiers:
578/// objc-type-qualifier
579/// objc-type-qualifiers objc-type-qualifier
580///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000581void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000582 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000583 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000584 return;
585
586 const IdentifierInfo *II = Tok.getIdentifierInfo();
587 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000588 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000589 continue;
590
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000592 switch (i) {
593 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
595 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
596 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
597 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
598 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
599 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000600 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000601 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000602 ConsumeToken();
603 II = 0;
604 break;
605 }
606
607 // If this wasn't a recognized qualifier, bail out.
608 if (II) return;
609 }
610}
611
612/// objc-type-name:
613/// '(' objc-type-qualifiers[opt] type-name ')'
614/// '(' objc-type-qualifiers[opt] ')'
615///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000617 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000618
Chris Lattner4a76b292008-10-22 03:52:06 +0000619 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000620 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000621
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000622 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000624
Chris Lattner4a76b292008-10-22 03:52:06 +0000625 TypeTy *Ty = 0;
626 if (isTypeSpecifierQualifier())
Steve Narofff28b2642007-09-05 23:30:30 +0000627 Ty = ParseTypeName();
Chris Lattnere8904e92008-08-23 01:48:03 +0000628
Steve Naroffd7333c22008-10-21 14:15:04 +0000629 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000630 ConsumeParen();
631 else if (Tok.getLocation() == TypeStartLoc) {
632 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000633 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000634 SkipUntil(tok::r_paren);
635 } else {
636 // Otherwise, we found *something*, but didn't get a ')' in the right
637 // place. Emit an error then return what we have as the type.
638 MatchRHSPunctuation(tok::r_paren, LParenLoc);
639 }
Steve Narofff28b2642007-09-05 23:30:30 +0000640 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000641}
642
643/// objc-method-decl:
644/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000645/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000646/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000647/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000648///
649/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000650/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000651/// objc-keyword-selector objc-keyword-decl
652///
653/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000654/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
655/// objc-selector ':' objc-keyword-attributes[opt] identifier
656/// ':' objc-type-name objc-keyword-attributes[opt] identifier
657/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000658///
Steve Naroff4985ace2007-08-22 18:35:33 +0000659/// objc-parmlist:
660/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000661///
Steve Naroff4985ace2007-08-22 18:35:33 +0000662/// objc-parms:
663/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000664///
Steve Naroff4985ace2007-08-22 18:35:33 +0000665/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000666/// , ...
667///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000668/// objc-keyword-attributes: [OBJC2]
669/// __attribute__((unused))
670///
Steve Naroffbef11852007-10-26 20:53:56 +0000671Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000672 tok::TokenKind mType,
673 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000674 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000675{
Chris Lattnere8904e92008-08-23 01:48:03 +0000676 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000677 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000678 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000679 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000680 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000681
Steve Naroffbef11852007-10-26 20:53:56 +0000682 SourceLocation selLoc;
683 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000684
685 if (!SelIdent) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000686 Diag(Tok, diag::err_expected_selector_for_method)
687 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000688 // Skip until we get a ; or {}.
689 SkipUntil(tok::r_brace);
690 return 0;
691 }
692
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000693 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000694 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000695 // If attributes exist after the method, parse them.
696 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000697 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000698 MethodAttrs = ParseAttributes();
699
700 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000701 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000702 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000703 0, 0, 0, CargNames,
704 MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000705 }
Steve Narofff28b2642007-09-05 23:30:30 +0000706
Steve Naroff68d331a2007-09-27 14:38:14 +0000707 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
708 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000709 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000710 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000711
712 Action::TypeTy *TypeInfo;
713 while (1) {
714 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000715
Chris Lattnerff384912007-10-07 02:00:24 +0000716 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000717 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000718 Diag(Tok, diag::err_expected_colon);
719 break;
720 }
721 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000723 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000724 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000725 else
726 TypeInfo = 0;
727 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000728 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000729
Chris Lattnerff384912007-10-07 02:00:24 +0000730 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000731 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000732 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000733
Chris Lattnerdf195262007-10-09 17:51:17 +0000734 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000735 Diag(Tok, diag::err_expected_ident); // missing argument name.
736 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000737 }
Chris Lattnerff384912007-10-07 02:00:24 +0000738 ArgNames.push_back(Tok.getIdentifierInfo());
739 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000740
Chris Lattnerff384912007-10-07 02:00:24 +0000741 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000742 SourceLocation Loc;
743 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000744 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000745 break;
746 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000747 }
Chris Lattnerff384912007-10-07 02:00:24 +0000748
Steve Naroff335eafa2007-11-15 12:35:21 +0000749 bool isVariadic = false;
750
Chris Lattnerff384912007-10-07 02:00:24 +0000751 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000752 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000753 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000754 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000755 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000756 ConsumeToken();
757 break;
758 }
Chris Lattnerff384912007-10-07 02:00:24 +0000759 DeclSpec DS;
760 ParseDeclarationSpecifiers(DS);
761 // Parse the declarator.
762 Declarator ParmDecl(DS, Declarator::PrototypeContext);
763 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000764 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000765 }
766
767 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000768 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000769 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000770 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000771 MethodAttrs = ParseAttributes();
772
773 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
774 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000775 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000776 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000777 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000778 &ArgNames[0], CargNames,
779 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000780 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000781}
782
Steve Naroffdac269b2007-08-20 21:31:48 +0000783/// objc-protocol-refs:
784/// '<' identifier-list '>'
785///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000786bool Parser::
Chris Lattnere13b9592008-07-26 04:03:38 +0000787ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
788 bool WarnOnDeclarations, SourceLocation &EndLoc) {
789 assert(Tok.is(tok::less) && "expected <");
790
791 ConsumeToken(); // the "<"
792
793 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
794
795 while (1) {
796 if (Tok.isNot(tok::identifier)) {
797 Diag(Tok, diag::err_expected_ident);
798 SkipUntil(tok::greater);
799 return true;
800 }
801 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
802 Tok.getLocation()));
803 ConsumeToken();
804
805 if (Tok.isNot(tok::comma))
806 break;
807 ConsumeToken();
808 }
809
810 // Consume the '>'.
811 if (Tok.isNot(tok::greater)) {
812 Diag(Tok, diag::err_expected_greater);
813 return true;
814 }
815
816 EndLoc = ConsumeAnyToken();
817
818 // Convert the list of protocols identifiers into a list of protocol decls.
819 Actions.FindProtocolDeclaration(WarnOnDeclarations,
820 &ProtocolIdents[0], ProtocolIdents.size(),
821 Protocols);
822 return false;
823}
824
Steve Naroffdac269b2007-08-20 21:31:48 +0000825/// objc-class-instance-variables:
826/// '{' objc-instance-variable-decl-list[opt] '}'
827///
828/// objc-instance-variable-decl-list:
829/// objc-visibility-spec
830/// objc-instance-variable-decl ';'
831/// ';'
832/// objc-instance-variable-decl-list objc-visibility-spec
833/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
834/// objc-instance-variable-decl-list ';'
835///
836/// objc-visibility-spec:
837/// @private
838/// @protected
839/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000840/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000841///
842/// objc-instance-variable-decl:
843/// struct-declaration
844///
Steve Naroff60fccee2007-10-29 21:38:07 +0000845void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
846 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000847 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000848 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000849 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
850
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000851 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000852
Steve Naroffddbff782007-08-21 21:17:12 +0000853 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000854
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000855 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000856 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000857 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000858 // Each iteration of this loop reads one objc-instance-variable-decl.
859
860 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000861 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000862 Diag(Tok, diag::ext_extra_struct_semi);
863 ConsumeToken();
864 continue;
865 }
Chris Lattnere1359422008-04-10 06:46:29 +0000866
Steve Naroffddbff782007-08-21 21:17:12 +0000867 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000868 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000869 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000870 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000871 case tok::objc_private:
872 case tok::objc_public:
873 case tok::objc_protected:
874 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000875 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000876 ConsumeToken();
877 continue;
878 default:
879 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000880 continue;
881 }
882 }
Chris Lattnere1359422008-04-10 06:46:29 +0000883
884 // Parse all the comma separated declarators.
885 DeclSpec DS;
886 FieldDeclarators.clear();
887 ParseStructDeclaration(DS, FieldDeclarators);
888
889 // Convert them all to fields.
890 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
891 FieldDeclarator &FD = FieldDeclarators[i];
892 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000893 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000894 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000895 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000896 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000897 }
Steve Naroff3536b442007-09-06 21:24:23 +0000898
Chris Lattnerdf195262007-10-09 17:51:17 +0000899 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000900 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000901 } else {
902 Diag(Tok, diag::err_expected_semi_decl_list);
903 // Skip to end of block or statement
904 SkipUntil(tok::r_brace, true, true);
905 }
906 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000907 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000908 // Call ActOnFields() even if we don't have any decls. This is useful
909 // for code rewriting tools that need to be aware of the empty list.
910 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
911 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000912 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000913 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000914}
Steve Naroffdac269b2007-08-20 21:31:48 +0000915
916/// objc-protocol-declaration:
917/// objc-protocol-definition
918/// objc-protocol-forward-reference
919///
920/// objc-protocol-definition:
921/// @protocol identifier
922/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000923/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000924/// @end
925///
926/// objc-protocol-forward-reference:
927/// @protocol identifier-list ';'
928///
929/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000930/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000931/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000932Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
933 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000934 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000935 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
936 ConsumeToken(); // the "protocol" identifier
937
Chris Lattnerdf195262007-10-09 17:51:17 +0000938 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000939 Diag(Tok, diag::err_expected_ident); // missing protocol name.
940 return 0;
941 }
942 // Save the protocol name, then consume it.
943 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
944 SourceLocation nameLoc = ConsumeToken();
945
Chris Lattnerdf195262007-10-09 17:51:17 +0000946 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000947 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000948 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000949 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
950 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000951 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000952
Chris Lattnerdf195262007-10-09 17:51:17 +0000953 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000954 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
955 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
956
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000957 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000958 while (1) {
959 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000960 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000961 Diag(Tok, diag::err_expected_ident);
962 SkipUntil(tok::semi);
963 return 0;
964 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000965 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
966 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000967 ConsumeToken(); // the identifier
968
Chris Lattnerdf195262007-10-09 17:51:17 +0000969 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000970 break;
971 }
972 // Consume the ';'.
973 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
974 return 0;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000975
Steve Naroffe440eb82007-10-10 17:32:04 +0000976 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000977 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000978 ProtocolRefs.size(),
979 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000980 }
981
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000982 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000983 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000984
Chris Lattnere13b9592008-07-26 04:03:38 +0000985 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000986 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000987 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000988 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000989
Chris Lattnere13b9592008-07-26 04:03:38 +0000990 DeclTy *ProtoType =
991 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
992 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000993 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000994 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000995 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000996}
Steve Naroffdac269b2007-08-20 21:31:48 +0000997
998/// objc-implementation:
999/// objc-class-implementation-prologue
1000/// objc-category-implementation-prologue
1001///
1002/// objc-class-implementation-prologue:
1003/// @implementation identifier objc-superclass[opt]
1004/// objc-class-instance-variables[opt]
1005///
1006/// objc-category-implementation-prologue:
1007/// @implementation identifier ( identifier )
1008
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001009Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1010 SourceLocation atLoc) {
1011 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1012 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1013 ConsumeToken(); // the "implementation" identifier
1014
Chris Lattnerdf195262007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001016 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1017 return 0;
1018 }
1019 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001020 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1022
Chris Lattnerdf195262007-10-09 17:51:17 +00001023 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001024 // we have a category implementation.
1025 SourceLocation lparenLoc = ConsumeParen();
1026 SourceLocation categoryLoc, rparenLoc;
1027 IdentifierInfo *categoryId = 0;
1028
Chris Lattnerdf195262007-10-09 17:51:17 +00001029 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001030 categoryId = Tok.getIdentifierInfo();
1031 categoryLoc = ConsumeToken();
1032 } else {
1033 Diag(Tok, diag::err_expected_ident); // missing category name.
1034 return 0;
1035 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001036 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001037 Diag(Tok, diag::err_expected_rparen);
1038 SkipUntil(tok::r_paren, false); // don't stop at ';'
1039 return 0;
1040 }
1041 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001042 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001043 atLoc, nameId, nameLoc, categoryId,
1044 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001045 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001046 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001047 }
1048 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001049 SourceLocation superClassLoc;
1050 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001051 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001052 // We have a super class
1053 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001054 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001055 Diag(Tok, diag::err_expected_ident); // missing super class name.
1056 return 0;
1057 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001058 superClassId = Tok.getIdentifierInfo();
1059 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001060 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001061 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001062 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001063 superClassId, superClassLoc);
1064
Steve Naroff60fccee2007-10-29 21:38:07 +00001065 if (Tok.is(tok::l_brace)) // we have ivars
1066 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001067 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001068
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001069 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001070}
Steve Naroff60fccee2007-10-29 21:38:07 +00001071
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001072Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1073 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1074 "ParseObjCAtEndDeclaration(): Expected @end");
1075 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001076 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001078 else
1079 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001080 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001081}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001082
1083/// compatibility-alias-decl:
1084/// @compatibility_alias alias-name class-name ';'
1085///
1086Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1087 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1088 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1089 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001091 Diag(Tok, diag::err_expected_ident);
1092 return 0;
1093 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001094 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1095 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001097 Diag(Tok, diag::err_expected_ident);
1098 return 0;
1099 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001100 IdentifierInfo *classId = Tok.getIdentifierInfo();
1101 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1102 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001103 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001104 return 0;
1105 }
1106 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1107 aliasId, aliasLoc,
1108 classId, classLoc);
1109 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001110}
1111
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001112/// property-synthesis:
1113/// @synthesize property-ivar-list ';'
1114///
1115/// property-ivar-list:
1116/// property-ivar
1117/// property-ivar-list ',' property-ivar
1118///
1119/// property-ivar:
1120/// identifier
1121/// identifier '=' identifier
1122///
1123Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1124 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1125 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001126 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001127 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001128 Diag(Tok, diag::err_expected_ident);
1129 return 0;
1130 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001131 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001132 IdentifierInfo *propertyIvar = 0;
1133 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1134 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001135 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001136 // property '=' ivar-name
1137 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001138 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001139 Diag(Tok, diag::err_expected_ident);
1140 break;
1141 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001142 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001143 ConsumeToken(); // consume ivar-name
1144 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001145 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1146 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001147 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001148 break;
1149 ConsumeToken(); // consume ','
1150 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001152 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001153 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001154}
1155
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001156/// property-dynamic:
1157/// @dynamic property-list
1158///
1159/// property-list:
1160/// identifier
1161/// property-list ',' identifier
1162///
1163Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1164 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1165 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1166 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001168 Diag(Tok, diag::err_expected_ident);
1169 return 0;
1170 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001171 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001172 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1173 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1174 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1175 propertyId, 0);
1176
Chris Lattnerdf195262007-10-09 17:51:17 +00001177 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001178 break;
1179 ConsumeToken(); // consume ','
1180 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001182 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001183 return 0;
1184}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001185
1186/// objc-throw-statement:
1187/// throw expression[opt];
1188///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001189Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001190 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001191 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001192 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001193 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001194 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001195 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001196 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001197 }
1198 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001199 ConsumeToken(); // consume ';'
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001200 return Owned(Actions.ActOnObjCAtThrowStmt(atLoc, Res.release()));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001201}
1202
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001203/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001204/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001205///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001206Parser::OwningStmtResult
1207Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001208 ConsumeToken(); // consume synchronized
1209 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001210 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001211 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001212 }
1213 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001214 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001215 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001216 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001217 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001218 }
1219 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001220 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001221 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001222 }
1223 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001224 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001225 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001226 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001227 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001228 // Enter a scope to hold everything within the compound stmt. Compound
1229 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001230 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001231
Sebastian Redl61364dd2008-12-11 19:30:53 +00001232 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001233
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001234 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001235 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001236 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001237 return Owned(Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.release(),
1238 SynchBody.release()));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001239}
1240
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001241/// objc-try-catch-statement:
1242/// @try compound-statement objc-catch-list[opt]
1243/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1244///
1245/// objc-catch-list:
1246/// @catch ( parameter-declaration ) compound-statement
1247/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1248/// catch-parameter-declaration:
1249/// parameter-declaration
1250/// '...' [OBJC2]
1251///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001252Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001253 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001254
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001255 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001256 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001257 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001258 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001259 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001260 OwningStmtResult CatchStmts(Actions);
1261 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001262 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001263 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001264 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001265 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001266 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001267
Chris Lattnerdf195262007-10-09 17:51:17 +00001268 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001269 // At this point, we need to lookahead to determine if this @ is the start
1270 // of an @catch or @finally. We don't want to consume the @ token if this
1271 // is an @try or @encode or something else.
1272 Token AfterAt = GetLookAheadToken(1);
1273 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1274 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1275 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001276
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001277 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001278 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001279 OwningStmtResult FirstPart(Actions);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001280 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001281 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001282 ConsumeParen();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001283 ParseScope CatchScope(this, Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001284 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001285 DeclSpec DS;
1286 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001287 // For some odd reason, the name of the exception variable is
Steve Naroff459a1e22008-06-03 05:36:54 +00001288 // optional. As a result, we need to use PrototypeContext.
1289 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001290 ParseDeclarator(DeclaratorInfo);
Steve Naroff459a1e22008-06-03 05:36:54 +00001291 if (DeclaratorInfo.getIdentifier()) {
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001292 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar914701e2008-08-05 16:28:08 +00001293 DeclaratorInfo, 0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001294 FirstPart =
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001295 Actions.ActOnDeclStmt(aBlockVarDecl,
Steve Naroff459a1e22008-06-03 05:36:54 +00001296 DS.getSourceRange().getBegin(),
1297 DeclaratorInfo.getSourceRange().getEnd());
Steve Naroff459a1e22008-06-03 05:36:54 +00001298 }
Steve Naroff64515f32008-02-05 21:27:35 +00001299 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001300 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001301 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001302
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001303 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001304 if (Tok.is(tok::l_brace))
1305 CatchBody = ParseCompoundStatementBody();
1306 else
1307 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001308 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001309 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001310 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001311 RParenLoc, FirstPart.release(), CatchBody.release(),
1312 CatchStmts.release());
Steve Naroff64515f32008-02-05 21:27:35 +00001313 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001314 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1315 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001316 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001317 }
1318 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001319 } else {
1320 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001321 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001322 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001323
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001324 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001325 if (Tok.is(tok::l_brace))
1326 FinallyBody = ParseCompoundStatementBody();
1327 else
1328 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001329 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001330 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001331 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001332 FinallyBody.release());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001333 catch_or_finally_seen = true;
1334 break;
1335 }
1336 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001337 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001338 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001339 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001340 }
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001341 return Owned(Actions.ActOnObjCAtTryStmt(atLoc, TryBody.release(),
1342 CatchStmts.release(),
1343 FinallyStmt.release()));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001344}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001345
Steve Naroff3536b442007-09-06 21:24:23 +00001346/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347///
Steve Naroff71c0a952007-11-13 23:01:27 +00001348Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001349 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001350 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001351 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352 ConsumeToken();
1353
Steve Naroff409be832007-11-11 19:54:21 +00001354 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001355 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001356 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001357
1358 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1359 SkipUntil(tok::l_brace, true, true);
1360
1361 // If we didn't find the '{', bail out.
1362 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001363 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001364 }
Steve Naroff409be832007-11-11 19:54:21 +00001365 SourceLocation BraceLoc = Tok.getLocation();
1366
1367 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001368 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001369
1370 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001371 // specified Declarator for the method.
1372 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001373
1374 OwningStmtResult FnBody(ParseCompoundStatementBody());
1375
Steve Naroff409be832007-11-11 19:54:21 +00001376 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001377 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001378 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1379 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001380
Steve Naroff409be832007-11-11 19:54:21 +00001381 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001382 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001383
Steve Naroff409be832007-11-11 19:54:21 +00001384 // TODO: Pass argument information.
Sebastian Redl798d1192008-12-13 16:23:55 +00001385 Actions.ActOnFinishFunctionBody(MDecl, move_convert(FnBody));
Steve Naroff71c0a952007-11-13 23:01:27 +00001386 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001387}
Anders Carlsson55085182007-08-21 17:43:55 +00001388
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001389Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001390 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001391 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001392 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1393 return ParseObjCThrowStmt(AtLoc);
1394 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1395 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001396 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001397 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001398 // If the expression is invalid, skip ahead to the next semicolon. Not
1399 // doing this opens us up to the possibility of infinite loops if
1400 // ParseExpression does not consume any tokens.
1401 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001402 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001403 }
1404 // Otherwise, eat the semicolon.
1405 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redla60528c2008-12-21 12:04:03 +00001406 return Actions.ActOnExprStmt(move_convert(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001407}
1408
Sebastian Redl1d922962008-12-13 15:32:12 +00001409Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001410 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001411 case tok::string_literal: // primary-expression: string-literal
1412 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001413 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001414 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001415 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001416 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001417
Chris Lattner4fef81d2008-08-05 06:19:09 +00001418 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1419 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001420 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001421 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001422 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001423 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001424 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001425 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001426 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001427 }
Anders Carlsson55085182007-08-21 17:43:55 +00001428 }
Anders Carlsson55085182007-08-21 17:43:55 +00001429}
1430
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001431/// objc-message-expr:
1432/// '[' objc-receiver objc-message-args ']'
1433///
1434/// objc-receiver:
1435/// expression
1436/// class-name
1437/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001438Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001439 assert(Tok.is(tok::l_square) && "'[' expected");
1440 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1441
1442 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001443 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001444 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001445 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001446 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1447 ExprArg(Actions));
Chris Lattner699b6612008-01-25 18:59:06 +00001448 }
1449
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001450 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001451 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001452 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001453 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001454 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001455
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001456 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl1d922962008-12-13 15:32:12 +00001457 0, move_convert(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001458}
Sebastian Redl1d922962008-12-13 15:32:12 +00001459
Chris Lattner699b6612008-01-25 18:59:06 +00001460/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1461/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001462///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001463/// objc-message-args:
1464/// objc-selector
1465/// objc-keywordarg-list
1466///
1467/// objc-keywordarg-list:
1468/// objc-keywordarg
1469/// objc-keywordarg-list objc-keywordarg
1470///
1471/// objc-keywordarg:
1472/// selector-name[opt] ':' objc-keywordexpr
1473///
1474/// objc-keywordexpr:
1475/// nonempty-expr-list
1476///
1477/// nonempty-expr-list:
1478/// assignment-expression
1479/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001480///
1481Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001482Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001483 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001484 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001485 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001486 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001487 SourceLocation Loc;
1488 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001489
1490 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001491 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001492
Chris Lattnerdf195262007-10-09 17:51:17 +00001493 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001494 while (1) {
1495 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001496 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001497
Chris Lattnerdf195262007-10-09 17:51:17 +00001498 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001499 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001500 // We must manually skip to a ']', otherwise the expression skipper will
1501 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1502 // the enclosing expression.
1503 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001504 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001505 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001506
Steve Naroff68d331a2007-09-27 14:38:14 +00001507 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001508 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001509 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001510 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001511 // We must manually skip to a ']', otherwise the expression skipper will
1512 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1513 // the enclosing expression.
1514 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001515 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001516 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001517
Steve Naroff37387c92007-09-17 20:25:27 +00001518 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001519 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001520
Steve Naroff37387c92007-09-17 20:25:27 +00001521 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001522 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001523 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001524 break;
1525 // We have a selector or a colon, continue parsing.
1526 }
1527 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001528 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001529 ConsumeToken(); // Eat the ','.
1530 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001531 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001532 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001533 // We must manually skip to a ']', otherwise the expression skipper will
1534 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1535 // the enclosing expression.
1536 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001537 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001538 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001539
Steve Naroff49f109c2007-11-15 13:05:42 +00001540 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001541 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001542 }
1543 } else if (!selIdent) {
1544 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001545
Chris Lattner4fef81d2008-08-05 06:19:09 +00001546 // We must manually skip to a ']', otherwise the expression skipper will
1547 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1548 // the enclosing expression.
1549 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001550 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001551 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001552
Chris Lattnerdf195262007-10-09 17:51:17 +00001553 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001554 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001555 // We must manually skip to a ']', otherwise the expression skipper will
1556 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1557 // the enclosing expression.
1558 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001559 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001560 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001561
Chris Lattner699b6612008-01-25 18:59:06 +00001562 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001563
Steve Naroff29238a02007-10-05 18:42:47 +00001564 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001565 if (nKeys == 0)
1566 KeyIdents.push_back(selIdent);
1567 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001568
Chris Lattnerff384912007-10-07 02:00:24 +00001569 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001570 if (ReceiverName)
1571 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
1572 LBracLoc, NameLoc, RBracLoc,
1573 KeyExprs.take(), KeyExprs.size()));
1574 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1575 LBracLoc, RBracLoc,
1576 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001577}
1578
Sebastian Redl1d922962008-12-13 15:32:12 +00001579Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001580 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001581 if (Res.isInvalid()) return move(Res);
1582
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001583 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1584 // expressions. At this point, we know that the only valid thing that starts
1585 // with '@' is an @"".
1586 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001587 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001588 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001589 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001590
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001591 while (Tok.is(tok::at)) {
1592 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001593
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001594 // Invalid unless there is a string literal.
1595 OwningExprResult Lit(Actions, true);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001596 if (isTokenStringLiteral())
Sebastian Redla55e52c2008-11-25 22:21:31 +00001597 Lit = ParseStringLiteralExpression();
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001598 else
1599 Diag(Tok, diag::err_objc_concat_string);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001600
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001601 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001602 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001603
Sebastian Redleffa8d12008-12-10 00:02:53 +00001604 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001605 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001606
1607 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1608 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001609}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001610
1611/// objc-encode-expression:
1612/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001613Parser::OwningExprResult
1614Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001615 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001616
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001617 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001618
Chris Lattner4fef81d2008-08-05 06:19:09 +00001619 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001620 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1621
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001622 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001623
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001624 TypeTy *Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001625
Anders Carlsson4988ae32007-08-23 15:31:37 +00001626 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001627
1628 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
1629 RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001630}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001631
1632/// objc-protocol-expression
1633/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001634Parser::OwningExprResult
1635Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001636 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001637
Chris Lattner4fef81d2008-08-05 06:19:09 +00001638 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001639 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1640
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001641 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001642
Chris Lattner4fef81d2008-08-05 06:19:09 +00001643 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001644 return ExprError(Diag(Tok, diag::err_expected_ident));
1645
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001646 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001647 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001648
Anders Carlsson4988ae32007-08-23 15:31:37 +00001649 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001650
Sebastian Redl1d922962008-12-13 15:32:12 +00001651 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1652 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001653}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001654
1655/// objc-selector-expression
1656/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001657Parser::OwningExprResult
1658Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001659 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001660
Chris Lattner4fef81d2008-08-05 06:19:09 +00001661 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001662 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1663
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001664 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001665 SourceLocation LParenLoc = ConsumeParen();
1666 SourceLocation sLoc;
1667 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001668 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1669 return ExprError(Diag(Tok, diag::err_expected_ident));
1670
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001671 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001672 unsigned nColons = 0;
1673 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001674 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001675 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001676 return ExprError(Diag(Tok, diag::err_expected_colon));
1677
Chris Lattnercb53b362007-12-27 19:57:00 +00001678 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001679 ConsumeToken(); // Eat the ':'.
1680 if (Tok.is(tok::r_paren))
1681 break;
1682 // Check for another keyword selector.
1683 SourceLocation Loc;
1684 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001685 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001686 if (!SelIdent && Tok.isNot(tok::colon))
1687 break;
1688 }
Steve Naroff887407e2007-12-05 22:21:29 +00001689 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001690 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001691 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001692 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1693 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001694 }